UNPKG

reactant-last-action

Version:

A Reactant plugin for staging last action

74 lines (71 loc) 3.84 kB
import { __extends, __rest, __assign, __decorate, __param, __metadata } from './node_modules/tslib/tslib.es6.js'; import { storeKey, injectable, optional, actionIdentifier, PluginModule } from 'reactant-module'; import { LastActionOptions } from './options.js'; var ReactantLastAction = /** @class */ (function (_super) { __extends(ReactantLastAction, _super); function ReactantLastAction(options) { var _a, _b; var _this = _super.call(this) || this; _this.options = options; _this.stateKey = (_b = (_a = _this.options) === null || _a === void 0 ? void 0 : _a.stateKey) !== null && _b !== void 0 ? _b : 'lastAction'; return _this; } ReactantLastAction.prototype.beforeCombineRootReducers = function (reducers) { var _a; var _this = this; if (Object.prototype.hasOwnProperty.call(reducers, this.stateKey)) { throw new Error("The identifier '".concat(this.stateKey, "' has a duplicate name, please reset the option 'stateKey' of 'LastAction' module.")); } return Object.assign(reducers, (_a = {}, _a[this.stateKey] = function (_state, _a) { var _b, _c, _d; if (_state === void 0) { _state = null; } var _inversePatches = _a._inversePatches, state = _a.state, rehydrate = _a.rehydrate, register = _a.register, action = __rest(_a, ["_inversePatches", "state", "rehydrate", "register"]); // ignore redux-persist property function in the action var rehydrateObj = typeof rehydrate === 'function' || typeof rehydrate === 'undefined' || rehydrate === null ? {} : { rehydrate: rehydrate }; var registerObj = typeof register === 'function' || typeof register === 'undefined' || register === null ? {} : { register: register }; // ignore inversePatches and state var reactantObj = action._reactant === actionIdentifier ? {} : { state: state, _inversePatches: _inversePatches }; var sequence = (_b = _state === null || _state === void 0 ? void 0 : _state._sequence) !== null && _b !== void 0 ? _b : 0; var shouldIgnore = (_d = (_c = _this.options) === null || _c === void 0 ? void 0 : _c.ignoreAction) === null || _d === void 0 ? void 0 : _d.call(_c, action); return __assign(__assign(__assign(__assign(__assign({}, action), rehydrateObj), registerObj), reactantObj), { _sequence: shouldIgnore ? sequence : sequence + 1 }); }, _a)); }; Object.defineProperty(ReactantLastAction.prototype, "sequence", { get: function () { var _a, _b, _c; return (_c = (_a = this._sequence) !== null && _a !== void 0 ? _a : (_b = this.action) === null || _b === void 0 ? void 0 : _b._sequence) !== null && _c !== void 0 ? _c : 0; }, set: function (value) { this._sequence = value; }, enumerable: false, configurable: true }); Object.defineProperty(ReactantLastAction.prototype, "action", { get: function () { var _a, _b; return (_b = (_a = this[storeKey]) === null || _a === void 0 ? void 0 : _a.getState()[this.stateKey]) !== null && _b !== void 0 ? _b : null; }, enumerable: false, configurable: true }); ReactantLastAction = __decorate([ injectable(), __param(0, optional(LastActionOptions)), __metadata("design:paramtypes", [Object]) ], ReactantLastAction); return ReactantLastAction; }(PluginModule)); export { ReactantLastAction as LastAction };