UNPKG

reactant-last-action

Version:

A Reactant plugin for staging last action

147 lines (133 loc) 7.21 kB
import { injectable, optional, storeKey, PluginModule, actionIdentifier } from 'reactant-module'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; var LastActionOptions = Symbol('LastActionOptions'); 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, LastActionOptions };