1 | /**
|
2 | @license
|
3 | Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
|
4 | This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
|
5 | The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
|
6 | The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
|
7 | Code distributed by Google as part of the polymer project is also
|
8 | subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
9 | */
|
10 | /**
|
11 | A Redux store enhancer that lets you lazy-install reducers after the store
|
12 | has booted up. Use this if your application lazy-loads routes that are connected
|
13 | to a Redux store.
|
14 |
|
15 | Example:
|
16 |
|
17 | import { combineReducers } from 'redux';
|
18 | import { lazyReducerEnhancer } from 'pwa-helpers/lazy-reducer-enhancer.js';
|
19 | import someReducer from './reducers/someReducer.js';
|
20 |
|
21 | export const store = createStore(
|
22 | (state, action) => state,
|
23 | compose(lazyReducerEnhancer(combineReducers))
|
24 | );
|
25 |
|
26 | Then, in your page/element, you can lazy load a specific reducer with:
|
27 |
|
28 | store.addReducers({
|
29 | someReducer
|
30 | });
|
31 | */
|
32 | export const lazyReducerEnhancer = (combineReducers) => {
|
33 | const enhancer = (nextCreator) => {
|
34 | return (origReducer, preloadedState) => {
|
35 | let lazyReducers = {};
|
36 | const nextStore = nextCreator(origReducer, preloadedState);
|
37 | return Object.assign({}, nextStore, { addReducers(newReducers) {
|
38 | const combinedReducerMap = Object.assign({}, lazyReducers, newReducers);
|
39 | this.replaceReducer(combineReducers(lazyReducers = combinedReducerMap));
|
40 | } });
|
41 | };
|
42 | };
|
43 | return enhancer;
|
44 | };
|
45 | //# sourceMappingURL=lazy-reducer-enhancer.js.map |
\ | No newline at end of file |