summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/createStore.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-09-05 11:04:36 +0200
committerSamy Pessé <samypesse@gmail.com>2016-09-05 11:04:36 +0200
commitb170b4d197822f3949b63df69e063d07cbb8e5c9 (patch)
tree556d9919e09eb12bea462a3d3a7801a1cea0611e /packages/gitbook-core/src/createStore.js
parenta14ca3e268e95a7eab59fb205b41da7331d57631 (diff)
downloadgitbook-b170b4d197822f3949b63df69e063d07cbb8e5c9.zip
gitbook-b170b4d197822f3949b63df69e063d07cbb8e5c9.tar.gz
gitbook-b170b4d197822f3949b63df69e063d07cbb8e5c9.tar.bz2
Start basis of gitbook-core
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;