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