UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

180 lines 5.52 kB
"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; const prelude_ts_1 = require("@3fv/prelude-ts"); const logger_proxy_1 = require("@3fv/logger-proxy"); const IdGenerator_1 = require("../util/IdGenerator"); const constants_1 = require("../constants"); // const // _cloneDeep = require('lodash.cloneDeep') const 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 = {}) { return { type, fullName: createLeafActionType(leaf, type), leaf, options, actionFactoryCtor, action: (decorator, ...args) => { let actions = decorator ? decorator(actionFactoryCtor) : null; if (!actions) { const newFactory = options.factoryCtor || actionFactoryCtor; actions = new newFactory(); } return action.apply(actions, args); } }; } exports.createActionRegistration = createActionRegistration; let globalStore; // /** // * Reference to a dispatcher // */ // let dispatch:DispatchState // // /** // * Reference to store state // */ // let getStoreState:GetStoreState const getGlobalStore = () => globalStore; exports.getGlobalStore = getGlobalStore; const getGlobalStoreState = () => { var _a; return (_a = exports.getGlobalStore()) === null || _a === void 0 ? void 0 : _a.getState(); }; exports.getGlobalStoreState = getGlobalStoreState; const globalDispatchProvider = ((action) => prelude_ts_1.Option.ofNullable(globalStore) .map(store => 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(state => 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; class ActionContainer { constructor(store) { this.store = store; this.registeredActions = {}; this.actionInterceptors = []; } /** * Add an interceptor * * @param interceptor * @returns {()=>undefined} */ addActionInterceptor(interceptor) { const { actionInterceptors } = this; actionInterceptors.push(interceptor); return () => { const index = actionInterceptors.findIndex(o => 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} */ executeActionInterceptor(index, reg, actionId, action, args) { const { actionInterceptors, store } = this; if (actionInterceptors.length > index) { return actionInterceptors[index](reg, () => { return this.executeActionInterceptor(index + 1, reg, actionId, action, args); }, ...args); } else { return action(actionId, ...args); } } /** * Execute a given action chain * * @param reg * @param actionFn * @param args * @returns {any|any} */ executeActionChain(reg, actionFn, ...args) { return this.executeActionInterceptor(0, reg, IdGenerator_1.makeId(), actionFn, args); } /** * Register an action from a decoration usually * * @param reg * @return {ActionRegistration} */ registerAction(reg) { this.registeredActions[reg.fullName] = reg; return reg; } /** * Retrieve a registered leaf action * * @param leaf * @param type * @returns {ActionRegistration} */ getAction(leaf, type) { return this.registeredActions[createLeafActionType(leaf, type)]; } getAllActions() { return this.registeredActions; // _cloneDeep(registeredActions) } } exports.ActionContainer = ActionContainer; //# sourceMappingURL=Actions.js.map