UNPKG

@turbox3d/reactivity

Version:

Large-scale reactive state management library

180 lines 5.62 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import { remove, includes, isPromise, nextTick, fail } from '@turbox3d/shared'; import { EMPTY_ACTION_NAME, TURBOX_PREFIX } from '../const/symbol'; import { store } from './store'; import { ActionStatus, EMaterialType } from '../const/enums'; import { TimeTravel } from './time-travel'; import { triggerCollector } from './collector'; import { mutation } from '../decorators/mutation'; export var actionPool = []; export var Action = /*#__PURE__*/function () { function Action() { _classCallCheck(this, Action); this.displayName = EMPTY_ACTION_NAME; this.status = ActionStatus.WORKING; this.historyNode = { actionChain: [], history: new Map() }; } return _createClass(Action, [{ key: "execute", value: function execute(runner) { var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var isWrapMutation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var options = arguments.length > 3 ? arguments[3] : undefined; if (this.status === ActionStatus.ABORT) { return; } Action.context = this; var wp; if (isWrapMutation) { wp = mutation(this.name, runner, options); } else { wp = runner; } var result = wp.apply(void 0, _toConsumableArray(payload)); if (isPromise(result)) { return result.then(function () { Action.context = void 0; }, function () { Action.context = void 0; }); } Action.context = void 0; } }, { key: "complete", value: function complete() { if (this.status === ActionStatus.ABORT) { return; } this.status = ActionStatus.COMPLETED; triggerCollector.save(this); triggerCollector.endBatch(true, this); remove(actionPool, this); } }, { key: "undo", value: function undo() { var _this = this; var keepHistory = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var original = function original() { TimeTravel.undoHandler(_this.historyNode.history); }; if (!store) { fail('store is not ready, please init first.'); } var action = this.historyNode.actionChain[0] || { name: '', displayName: '' }; store.dispatch({ name: "".concat(TURBOX_PREFIX, "UNDO_").concat(action.name), displayName: "UNDO_".concat(action.displayName), payload: [], original: original, type: EMaterialType.UNDO, isInner: true }); if (keepHistory) { return; } this.historyNode = { actionChain: [], history: new Map() }; nextTick(function () { var ctt = TimeTravel.currentTimeTravel; ctt && ctt.onChange(ctt.undoable, ctt.redoable, 'undo', _this); }); } }, { key: "redo", value: function redo() { var _this2 = this; var keepHistory = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var original = function original() { TimeTravel.redoHandler(_this2.historyNode.history); }; if (!store) { fail('store is not ready, please init first.'); } var action = this.historyNode.actionChain[0] || { name: '', displayName: '' }; store.dispatch({ name: "".concat(TURBOX_PREFIX, "REDO_").concat(action.name), displayName: "REDO_".concat(action.displayName), payload: [], original: original, type: EMaterialType.REDO, isInner: true }); if (keepHistory) { return; } this.historyNode = { actionChain: [], history: new Map() }; nextTick(function () { var ctt = TimeTravel.currentTimeTravel; ctt && ctt.onChange(ctt.undoable, ctt.redoable, 'redo', _this2); }); } }, { key: "abort", value: function abort() { var revert = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (revert) { this.undo(); } this.status = ActionStatus.ABORT; triggerCollector.endBatch(true, this); remove(actionPool, this); } }], [{ key: "create", value: function create(name, displayName) { var action = new Action(); action.name = name; displayName && (action.displayName = displayName); action.historyNode.actionChain.push({ name: name, displayName: action.displayName }); actionPool.push(action); return action; } }, { key: "abortAll", value: function abortAll() { var revert = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; actionPool.forEach(function (action) { action.abort(revert); }); } }, { key: "get", value: function get() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } if (!args.length) { return actionPool; } if (args.length === 1) { return actionPool.filter(function (action) { return action.name === args[0]; }); } return actionPool.filter(function (action) { return includes(args, action.name); }); } }]); }();