UNPKG

process-controller

Version:

A process controller base on promise that can be used in both browser and node

253 lines (209 loc) 6.35 kB
/** * Bundle of process-controller * Generated: 2020-04-14 * Version: 2.1.0 * License: MIT * Author: 2631541504@qq.com */ import simpleUniqueId from '@livelybone/simple-unique-id'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } var ProcessStatus; (function (ProcessStatus) { ProcessStatus[ProcessStatus["Waiting"] = 0] = "Waiting"; ProcessStatus[ProcessStatus["Running"] = 1] = "Running"; ProcessStatus[ProcessStatus["Pausing"] = 2] = "Pausing"; })(ProcessStatus || (ProcessStatus = {})); var ProcessController = /*#__PURE__*/ function () { /** * The currently running step queue * * 当前正在运行的流程的步骤队列 * */ /** * Process history generated in the whole running time * * 程序运行产生的流程历史 * */ /** * The result of the previous step * * 上一个步骤的处理结果 * */ /** * The final result of the current process * * 当前流程的最后结果 * */ function ProcessController(options) { _classCallCheck(this, ProcessController); _defineProperty(this, "currSteps", []); _defineProperty(this, "history", []); _defineProperty(this, "status", ProcessStatus.Waiting); _defineProperty(this, "currStepResult", Promise.resolve()); _defineProperty(this, "currProcessResult", Promise.resolve()); _defineProperty(this, "options", void 0); this.options = _objectSpread2({}, options, { autoRun: options && options.autoRun || function () { return false; } }); } /** * Correct the order of steps * * 校正流程的顺序 * */ _createClass(ProcessController, [{ key: "correctOrder", value: function correctOrder() { this.currSteps.sort(function (a, b) { return a.order - b.order; }); } /** * Add step * * 添加流程 * */ }, { key: "addStep", value: function addStep(callback, order, extraInfo) { var _this = this; var id = simpleUniqueId(); var step = _objectSpread2({}, extraInfo, { callback: callback, order: order, id: id }); this.currSteps.push(step); this.correctOrder(); Promise.resolve(this.options.autoRun(step, this)).then(function (run) { if (run) _this.run(); }); return step; } /** * Prioritize the step with smaller order,return the final result of the current process * * 运行,order 值越小越先执行,返回当前流程的最后处理结果 * */ }, { key: "run", value: function run() { var _this2 = this; if (this.status === ProcessStatus.Running || this.currSteps.length < 1) { return this.currProcessResult; } if (this.status !== ProcessStatus.Pausing) { this.history.push([]); var resolveFn; this.currProcessResult = new Promise(function (res) { resolveFn = res; }); this.currProcessResult.resolveFn = resolveFn; } var currHistorySteps = this.history[this.history.length - 1]; var runOne = function runOne(step) { _this2.currStepResult = _this2.currStepResult.then(function () { return step.callback.apply(step, arguments); }); return _this2.currStepResult.then(function (data) { currHistorySteps.push(step); var stepItem = _this2.currSteps[0]; if (stepItem && step.id === stepItem.id) { _this2.currSteps.shift(); } if (_this2.currSteps.length < 1) _this2.currProcessResult.resolveFn(data); }); }; var fn = function fn() { if (_this2.status === ProcessStatus.Pausing || _this2.currSteps.length < 1) { return Promise.resolve(); } return runOne(_this2.currSteps[0]).then(function () { return fn(); }); }; this.status = ProcessStatus.Running; return fn().then(function () { _this2.status = ProcessStatus.Waiting; return _this2.currProcessResult; }); } }, { key: "pause", value: function pause() { this.status = ProcessStatus.Pausing; } }, { key: "stop", value: function stop() { this.currSteps = []; this.status = ProcessStatus.Waiting; } }]); return ProcessController; }(); export default ProcessController; export { ProcessStatus };