UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

300 lines 13.1 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RootReducer = void 0; const Immutable = __importStar(require("immutable")); const logger_proxy_1 = require("@3fv/logger-proxy"); const util_1 = require("../util"); // import {getGlobalStateProvider} from '../actions/Actions' const shallowequal_1 = __importDefault(require("shallowequal")); const get_1 = __importDefault(require("lodash/get")); const clone_1 = __importDefault(require("lodash/clone")); const constants_1 = require("../constants"); const prelude_ts_1 = require("@3fv/prelude-ts"); const dev_1 = require("../dev"); const guard_1 = require("@3fv/guard"); const actions_1 = require("../actions"); const ActionIdCacheMax = 500, log = logger_proxy_1.getLogger(__filename); /** * Get leaf value *} * @param rootValue * @param leaf * @return {any} */ function getLeafValue(rootValue, leaf) { if (Immutable.Map.isMap(rootValue)) { return rootValue.get(leaf); } else { return get_1.default(rootValue, leaf); } } /** * RootReducer for typedux apps * * Maps leaf reducers and decorated reducers * to the appropriate state functions */ class RootReducer { /** * Create reducer * * @param rootStateType - type of root state, must be immutable map or record * @param reducers - list of all child reducers * @param store */ constructor(store, rootStateType = null, ...reducers) { this.store = store; this.rootStateType = rootStateType; // Internal list of all leaf reducers this.reducers = []; // handled actions ids to avoid duplication this.handledActionIds = []; const leafs = []; reducers .filter(reducer => util_1.isFunction(reducer.leaf)) .forEach(reducer => { const leaf = reducer.leaf(); if (leafs.includes(leaf)) { return; } leafs.push(leaf); this.reducers.push(reducer); }); reducers .filter(reducer => !util_1.isFunction(reducer.leaf)) .forEach(reducer => this.reducers.push(reducer)); } setOnError(onError) { this.onError = onError; return this; } /** * Create default state * * @param defaultStateValue - if provided then its used as base for inflation * @returns {State} */ defaultState(defaultStateValue = null) { // LOAD THE STATE AND VERIFY IT IS Immutable.Map/Record let state = defaultStateValue || { type: "ROOT" }; // ITERATE REDUCERS & CREATE LEAF STATES this.reducers .filter(reducer => util_1.isFunction(reducer.leaf)) .forEach(reducer => { const leaf = reducer.leaf(), leafDefaultState = getLeafValue(defaultStateValue, leaf); state = Object.assign(Object.assign({}, state), { [leaf]: reducer.defaultState(leafDefaultState || {}) }); }); return state; } /** * Create a generic handler for dispatches * * @returns {(state:S, action:ReduxAction)=>S} */ makeGenericHandler() { return (state, action) => this.handle(state, action); } /** * Handle action message * * @param state * @param action * @returns {State} */ handle(state, action) { var _a, _b; // Check if action has already been processed if (action.id && this.handledActionIds.includes(action.id)) { if (typeof console !== "undefined" && console.trace) { console.trace(`Duplicate action received: ${action.leaf}/${action.type}, ${action.id}`, action); } return state; } // Push action id to the handled list else if (action.id) { this.handledActionIds.unshift(action.id); if (this.handledActionIds.length > ActionIdCacheMax) { this.handledActionIds.length = ActionIdCacheMax; } } try { /** * Tracks whether the overall state has changed */ let hasChanged = false; // Guard state type as immutable if (!state) { state = this.defaultState(state); hasChanged = true; } /** * Create a change detector func that evaluates * a leaf for changes and updates (and sets changed flags) * as need for tracking * * @param leaf * @param currentState * @param updateState * @param changed */ const createChangeDetector = (leaf, currentState, updateState, changed) => (newReducerState) => { if (!newReducerState) { throw new Error(`New reducer state is null for leaf ${leaf}`); } const noMatch = !shallowequal_1.default(currentState, newReducerState); if (noMatch) { changed.set(); updateState(clone_1.default(newReducerState)); } }; // Store a ref to the original state object const stateMap = state; // Hold the interim state in `tempState` let tempState = Object.assign({}, stateMap); // Find the action registration const actionReg = (_b = (_a = this.store) === null || _a === void 0 ? void 0 : _a.actionContainer) === null || _b === void 0 ? void 0 : _b.getAction(action.leaf, action.type); // Is the reg invalid, i.e. reg not found and leaf + type set const actionRegInvalid = !actionReg && [action.leaf, action.type].every(guard_1.isString); if (dev_1.isDev && log.isDebugEnabled() && actionRegInvalid) { log.warn(`Unable to find action registration for: ${actions_1.createLeafActionType(action.leaf, action.type)}`, action); } if (util_1.isFunction(actionReg === null || actionReg === void 0 ? void 0 : actionReg.action)) { prelude_ts_1.Option.ofNullable(tempState[action.leaf]).ifSome(reducerState => { var _a; const { leaf } = action, changed = new util_1.Flag(), checkReducerStateChange = createChangeDetector(leaf, reducerState, newState => { tempState = Object.assign(Object.assign({}, tempState), { [leaf]: newState }); hasChanged = true; }, changed); // ActionMessage.reducers PROVIDED if (log.isDebugEnabled() && dev_1.isDev) { log.debug("Action type supported", action.leaf, action.type); } if (action.stateType && reducerState instanceof action.stateType) { get_1.default(action, "reducers", []).forEach(actionReducer => checkReducerStateChange(actionReducer(reducerState, action))); } // IF @ActionReducer REGISTERED if (((_a = actionReg === null || actionReg === void 0 ? void 0 : actionReg.options) === null || _a === void 0 ? void 0 : _a.isReducer) === true) { prelude_ts_1.Option.ofNullable(actionReg.action(null, ...action.args)) .filter(util_1.isFunction) .match({ None: () => { throw new Error(`Action reducer did not return a function: ${actionReg.type}`); }, Some: reducerFn => { if (log.isDebugEnabled() && dev_1.isDev) { log.debug(`Calling action reducer: ${actionReg.fullName}`); } checkReducerStateChange(reducerFn(reducerState, tempState)); } }); } }); } // Iterate leaves and execute actions for (let reducer of this.reducers) { if (util_1.isFunction(reducer)) { const simpleReducer = reducer; const simpleState = simpleReducer(tempState, action); if (simpleState !== tempState) { tempState = simpleState; hasChanged = true; } continue; } const // Get the reducer leaf leaf = reducer.leaf(), // Get Current RAW state rawLeafState = tempState[leaf], // Shape it for the reducer startReducerState = rawLeafState, stateChangeDetected = new util_1.Flag(); let reducerState = startReducerState; try { /** * Check the returned state from every handler for changes * * @param newReducerState */ const checkReducerStateChange = createChangeDetector(leaf, reducerState, newState => { reducerState = newState; }, stateChangeDetected); // Check internal actions if (constants_1.INTERNAL_ACTIONS.includes(action.type)) { if (log.isDebugEnabled() && dev_1.isDev) { log.debug(`Sending init event to ${leaf} - internal action received ${action.type}`); } if (constants_1.INTERNAL_ACTION.INIT === action.type && reducer.init) { checkReducerStateChange(reducer.init(startReducerState)); } } // Check leaf of reducer and action to see if this reducer handles the supplied action if (action.leaf && action.leaf !== leaf) { continue; } // CHECK REDUCER.HANDLE if (reducer.handle) { checkReducerStateChange(reducer.handle(reducerState, action)); } // CHECK ACTUAL REDUCER FOR SUPPORT if (util_1.isFunction(reducer[action.type])) { checkReducerStateChange(reducer[action.type](reducerState, ...action.args)); } } catch (err) { log.error(`Error occurred on reducer leaf ${leaf}`, err); if (reducer.handleError) { reducer.handleError(startReducerState, action, err); } this.onError && this.onError(err, reducer); } if (stateChangeDetected) { tempState = Object.assign(Object.assign({}, tempState), { [leaf]: reducerState }); hasChanged = true; } } if (log.isDebugEnabled() && dev_1.isDev) { log.debug("Has changed after all reducers", hasChanged, "states equal", shallowequal_1.default(tempState, state)); } return (hasChanged ? tempState : state); } catch (err) { log.error("Error bubbled to root reducer", err); // If error handler exists then use it if (this.onError) { this.onError && this.onError(err); return state; } // Otherwise throw throw err; } } } exports.RootReducer = RootReducer; // Export the RootReducer class as the default exports.default = RootReducer; // export default (state:any,action:any):any => { // return rootReducer.handle(state as DefaultStateType, action as ActionMessage<any>) // } //# sourceMappingURL=RootReducer.js.map