UNPKG

5.33 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14var redux_1 = require("redux");
15var features_1 = require("./reducers/embeddedApp/features");
16var utilities_1 = require("./reducers/embeddedApp/utilities");
17var actions_1 = require("../actions");
18/**
19 * The constant key `appBridge`
20 * @public
21 */
22exports.APP_BRIDGE_KEY = 'appBridge';
23/**
24 * Returns a combined reducer for the `appBridge` key
25 * Always includes the `features` reducer
26 * @public
27 * @param stateReducers - a reducer map for the dynamic app state
28 * @param initialState - an optional default value for the store
29 */
30function createReducers(stateReducers, initialState) {
31 if (stateReducers === void 0) { stateReducers = {}; }
32 if (initialState === void 0) { initialState = {}; }
33 var _a;
34 var allReducers = redux_1.combineReducers((_a = {},
35 _a[exports.APP_BRIDGE_KEY] = redux_1.combineReducers(utilities_1.wrapReducers(__assign({ features: features_1.asyncFeaturesReducer }, stateReducers), utilities_1.resetAppReducer, initialState)),
36 _a));
37 return function (state, action) {
38 if (state === void 0) { state = (_a = {}, _a[exports.APP_BRIDGE_KEY] = { features: {} }, _a); }
39 var _a, _b, _c;
40 if (actions_1.isLoadReducerAction(action)) {
41 var feature = action.payload.feature;
42 var currenState = state[exports.APP_BRIDGE_KEY];
43 return _b = {},
44 _b[exports.APP_BRIDGE_KEY] = __assign({}, currenState, (_c = {}, _c[feature] = initialState[feature], _c)),
45 _b;
46 }
47 return allReducers(state, action);
48 };
49}
50exports.createReducers = createReducers;
51/**
52 * Creates a store containing only the default `features` reducer
53 * @public
54 */
55function createStore(middleware, initialState, debug) {
56 if (middleware === void 0) { middleware = []; }
57 if (initialState === void 0) { initialState = {}; }
58 if (debug === void 0) { debug = false; }
59 var _a;
60 var defaultState = (_a = {}, _a[exports.APP_BRIDGE_KEY] = initialState, _a);
61 var composeEnhancers = debug && typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
62 ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ name: 'App Bridge', shouldHotReload: false })
63 : redux_1.compose;
64 return redux_1.createStore(createReducers({}, initialState), defaultState, composeEnhancers(redux_1.applyMiddleware.apply(void 0, middleware)));
65}
66exports.createStore = createStore;
67/**
68 * Creates a method that when called, dynamically adds a reducer to
69 * the provided store
70 * @internal
71 * @param store - a Redux store
72 * @param globalInitialState - custom overrides for resolving the app state when adding a new reducer
73 * */
74function createAddReducer(store, globalInitialState) {
75 if (globalInitialState === void 0) { globalInitialState = {}; }
76 var asyncReducers = {};
77 return function addReducer(_a) {
78 var key = _a.key, reducer = _a.reducer, initialState = _a.initialState;
79 var _b;
80 if (asyncReducers.hasOwnProperty(key)) {
81 return;
82 }
83 asyncReducers[key] = reducer;
84 store.replaceReducer(createReducers(asyncReducers, __assign((_b = {}, _b[key] = initialState, _b), globalInitialState)));
85 store.dispatch(actions_1.hostLoadReducer(key));
86 store.dispatch(actions_1.hostLoadCompleteReducer(key));
87 };
88}
89exports.createAddReducer = createAddReducer;
90/**
91 * Creates an action queue for the provided store
92 * @internal
93 * */
94function createActionsQueue(store) {
95 var queue = new Map();
96 return {
97 add: function (context, action) {
98 if (!queue.has(context)) {
99 queue.set(context, new Set());
100 }
101 var contextQueue = queue.get(context);
102 contextQueue.add(action);
103 },
104 clear: function (context) {
105 var contextQueue = queue.get(context);
106 if (contextQueue) {
107 contextQueue.clear();
108 }
109 },
110 resolve: function (feature) {
111 queue.forEach(function (contextQueue) {
112 contextQueue.forEach(function (action) {
113 var actionKey = groupToFeatureKey(action.group);
114 if (actionKey === feature) {
115 store.dispatch(action);
116 contextQueue.delete(action);
117 }
118 });
119 });
120 },
121 };
122}
123exports.createActionsQueue = createActionsQueue;
124/**
125 * Returns the reducer key from a group
126 * @internal
127 */
128function groupToFeatureKey(group) {
129 return group[0].toLowerCase().concat(group.substr(1).replace('_', ''));
130}
131/**
132 * Predicate to determine if a reducer for the associated action is already loaded
133 * @internal
134 */
135function isReducerLoaded(state, action) {
136 var key = groupToFeatureKey(action.group);
137 return state.hasOwnProperty(key);
138}
139exports.isReducerLoaded = isReducerLoaded;