1 | "use strict";
|
2 | var __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 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | var redux_1 = require("redux");
|
15 | var features_1 = require("./reducers/embeddedApp/features");
|
16 | var utilities_1 = require("./reducers/embeddedApp/utilities");
|
17 | var actions_1 = require("../actions");
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | exports.APP_BRIDGE_KEY = 'appBridge';
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function 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 | }
|
50 | exports.createReducers = createReducers;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function 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 | }
|
66 | exports.createStore = createStore;
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function 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 | }
|
89 | exports.createAddReducer = createAddReducer;
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function 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 | }
|
123 | exports.createActionsQueue = createActionsQueue;
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | function groupToFeatureKey(group) {
|
129 | return group[0].toLowerCase().concat(group.substr(1).replace('_', ''));
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | function isReducerLoaded(state, action) {
|
136 | var key = groupToFeatureKey(action.group);
|
137 | return state.hasOwnProperty(key);
|
138 | }
|
139 | exports.isReducerLoaded = isReducerLoaded;
|