summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/createStore.js
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-core/src/createStore.js')
-rw-r--r--packages/gitbook-core/src/createStore.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/createStore.js b/packages/gitbook-core/src/createStore.js
new file mode 100644
index 0000000..ecc351f
--- /dev/null
+++ b/packages/gitbook-core/src/createStore.js
@@ -0,0 +1,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);
+ const reducer = Redux.combine(reducers, ...pluginReducers);
+
+ const store = Redux.createStore(
+ reducer,
+ initialState,
+ Redux.applyMiddleware(ReduxThunk)
+ );
+
+ // Initialize the plugins
+ plugins.forEach(plugin => {
+ plugin.onInitialState(store.dispatch, store.getState);
+ });
+
+ return store;
+}
+
+module.exports = createStore;