summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/createStore.js
blob: 487319087540c0a08a8c688bad762151b26344e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const Redux = require('redux');
const ReduxThunk = require('redux-thunk').default;

const reducers = require('./reducers');

/**
 * Create a new redux store from an initial state and a list of plugins.
 * Each plugin entry is the result of {createPlugin}.
 *
 * @param  {Array<Plugin>} plugins
 * @param  {Object} initialState
 * @return {ReduxStore} store
 */
function createStore(plugins, initialState) {
    const pluginReducers = plugins.map(plugin => plugin.onReduceState);
    console.log(pluginReducers);
    const reducer = Redux.compose(reducers, ...pluginReducers);
    const store = Redux.createStore(
        reducers,
        initialState,
        Redux.compose(Redux.applyMiddleware(ReduxThunk))
    );

    // Initialize the plugins
    plugins.forEach(plugin => {
        plugin.onInitialState(store.dispatch, store.getState);
    });

    return store;
}

module.exports = createStore;