UNPKG

abb-core

Version:

Application Build Butler Core

99 lines (98 loc) 4.05 kB
import { ABBTimedPromiseFactory, ABBActionTypes } from "../../.."; import { applyMiddleware, createStore } from 'redux'; import thunk from "redux-thunk"; import { Map } from 'immutable'; var ABBCoreAPI = /** @class */ (function () { function ABBCoreAPI() { this.coreIsInitializedPromise = ABBTimedPromiseFactory.create(); this.routerIsInitializedPromise = ABBTimedPromiseFactory.create(); this.translatorIsInitializedPromise = ABBTimedPromiseFactory.create(); this._store = createStore(function (globalState, action) { var mutableState = Object.assign({}, globalState); var reducersMapObject = { pluginReducer: ABBCoreAPI.createPluginReducer() }; var stateToReducerMap = { pluginReducer: { plugins: globalState.plugins } }; globalState.plugins.forEach(function (pluginData) { var actionCreator = pluginData.actionCreator; if (actionCreator) { reducersMapObject = Object.assign({}, reducersMapObject, actionCreator.getReducersMapObject()); stateToReducerMap = Object.assign({}, stateToReducerMap, actionCreator.getStateToReducerMapObject(globalState)); } }); for (var _i = 0, _a = Object.entries(reducersMapObject); _i < _a.length; _i++) { var _b = _a[_i], reducerName = _b[0], reducer = _b[1]; if (action.type === ABBActionTypes.INIT_STATE) { mutableState = ABBCoreAPI.initState(reducer, action, mutableState); } else { mutableState = ABBCoreAPI.executeReducer(reducer, stateToReducerMap[reducerName], action, mutableState); } } return mutableState; }, ABBCoreAPI.getPreloadedState(), applyMiddleware(thunk)); } Object.defineProperty(ABBCoreAPI.prototype, "Store", { get: function () { return { dispatch: this._store.dispatch, getState: this._store.getState, subscribe: this._store.subscribe }; }, enumerable: true, configurable: true }); ABBCoreAPI.initState = function (reducer, action, state) { var reducerResult = reducer(undefined, action); return Object.assign({}, reducerResult, state); // make sure initialState will not override existing }; ABBCoreAPI.executeReducer = function (reducer, reducerState, action, state) { var reducerResult = reducer(reducerState, action); return Object.assign({}, state, reducerResult); }; ABBCoreAPI.createPluginReducer = function () { return function (state, action) { switch (action.type) { case ABBActionTypes.ADD_PLUGIN: return Object.assign({}, state, { plugins: state.plugins.set(action.plugin.name, action.plugin) }); default: return state; } }; }; ABBCoreAPI.getPreloadedState = function () { return { plugins: Map(), }; }; ABBCoreAPI.initStateAction = function (actionCreator) { return function (dispatch) { dispatch({ type: ABBActionTypes.INIT_STATE, actionCreator: actionCreator }); }; }; ABBCoreAPI.addPluginAction = function (name, instancePromise, actionCreator) { return function (dispatch) { dispatch({ type: ABBActionTypes.ADD_PLUGIN, plugin: { name: name, instancePromise: instancePromise, actionCreator: actionCreator } }); dispatch(ABBCoreAPI.initStateAction(actionCreator)); }; }; return ABBCoreAPI; }()); export { ABBCoreAPI };