UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

223 lines 8.27 kB
var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "@3fv/prelude-ts", "@3fv/logger-proxy", "../util/IdGenerator", "../constants"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionContainer = exports.setGlobalStore = exports.getGlobalStoreInternalState = exports.getGlobalDispatchProvider = exports.getGlobalStateProvider = exports.getGlobalStoreState = exports.getGlobalStore = exports.createActionRegistration = exports.createLeafActionType = void 0; var prelude_ts_1 = require("@3fv/prelude-ts"); var logger_proxy_1 = require("@3fv/logger-proxy"); var IdGenerator_1 = require("../util/IdGenerator"); var constants_1 = require("../constants"); // const // _cloneDeep = require('lodash.cloneDeep') var log = logger_proxy_1.getLogger(__filename); /** * Create a fully qualified action type * * @param leaf * @param type * * @returns {string} */ function createLeafActionType(leaf, type) { return type.indexOf(".") > -1 ? type : leaf + "." + type; } exports.createLeafActionType = createLeafActionType; function createActionRegistration(actionFactoryCtor, leaf, type, action, options) { if (options === void 0) { options = {}; } return { type: type, fullName: createLeafActionType(leaf, type), leaf: leaf, options: options, actionFactoryCtor: actionFactoryCtor, action: function (decorator) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var actions = decorator ? decorator(actionFactoryCtor) : null; if (!actions) { var newFactory = options.factoryCtor || actionFactoryCtor; actions = new newFactory(); } return action.apply(actions, args); } }; } exports.createActionRegistration = createActionRegistration; var globalStore; // /** // * Reference to a dispatcher // */ // let dispatch:DispatchState // // /** // * Reference to store state // */ // let getStoreState:GetStoreState var getGlobalStore = function () { return globalStore; }; exports.getGlobalStore = getGlobalStore; var getGlobalStoreState = function () { var _a; return (_a = exports.getGlobalStore()) === null || _a === void 0 ? void 0 : _a.getState(); }; exports.getGlobalStoreState = getGlobalStoreState; var globalDispatchProvider = (function (action) { return prelude_ts_1.Option.ofNullable(globalStore) .map(function (store) { return store.dispatch(action); }) .getOrThrow("Invalid store"); }); /** * Get the current store state get * function - usually set when a new state is created * * @returns {GetStoreState} */ function getGlobalStateProvider() { return exports.getGlobalStoreState; } exports.getGlobalStateProvider = getGlobalStateProvider; /** * Get the current store * dispatch function * * @returns {DispatchState} */ function getGlobalDispatchProvider() { return globalDispatchProvider; } exports.getGlobalDispatchProvider = getGlobalDispatchProvider; /** * Get the stores internal state * * @returns {GetStoreState|any} */ function getGlobalStoreInternalState() { return prelude_ts_1.Option.ofNullable(exports.getGlobalStoreState()) .map(function (state) { return state[constants_1.INTERNAL_KEY]; }) .getOrUndefined(); } exports.getGlobalStoreInternalState = getGlobalStoreInternalState; /** * Set the global store provider * * @param newStore */ function setGlobalStore(newStore) { if (!newStore && process.env.NODE_ENV === "development") { console.warn("You are setting the global store to null"); } // Cast the guarded type globalStore = newStore; } exports.setGlobalStore = setGlobalStore; var ActionContainer = /** @class */ (function () { function ActionContainer(store) { this.store = store; this.registeredActions = {}; this.actionInterceptors = []; } /** * Add an interceptor * * @param interceptor * @returns {()=>undefined} */ ActionContainer.prototype.addActionInterceptor = function (interceptor) { var actionInterceptors = this.actionInterceptors; actionInterceptors.push(interceptor); return function () { var index = actionInterceptors.findIndex(function (o) { return interceptor === o; }); if (index > -1) actionInterceptors.splice(index, 1); }; }; /** * Execute an interceptor at a specific index * * @param index * @param reg * @param actionId * @param action * @param args * @returns {any} */ ActionContainer.prototype.executeActionInterceptor = function (index, reg, actionId, action, args) { var _this = this; var _a = this, actionInterceptors = _a.actionInterceptors, store = _a.store; if (actionInterceptors.length > index) { return actionInterceptors[index].apply(actionInterceptors, __spread([reg, function () { return _this.executeActionInterceptor(index + 1, reg, actionId, action, args); }], args)); } else { return action.apply(void 0, __spread([actionId], args)); } }; /** * Execute a given action chain * * @param reg * @param actionFn * @param args * @returns {any|any} */ ActionContainer.prototype.executeActionChain = function (reg, actionFn) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } return this.executeActionInterceptor(0, reg, IdGenerator_1.makeId(), actionFn, args); }; /** * Register an action from a decoration usually * * @param reg * @return {ActionRegistration} */ ActionContainer.prototype.registerAction = function (reg) { this.registeredActions[reg.fullName] = reg; return reg; }; /** * Retrieve a registered leaf action * * @param leaf * @param type * @returns {ActionRegistration} */ ActionContainer.prototype.getAction = function (leaf, type) { return this.registeredActions[createLeafActionType(leaf, type)]; }; ActionContainer.prototype.getAllActions = function () { return this.registeredActions; // _cloneDeep(registeredActions) }; return ActionContainer; }()); exports.ActionContainer = ActionContainer; }); //# sourceMappingURL=Actions.js.map