UNPKG

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