diff options
Diffstat (limited to 'packages/gitbook-core/src/lib/createPlugin.js')
-rw-r--r-- | packages/gitbook-core/src/lib/createPlugin.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/lib/createPlugin.js b/packages/gitbook-core/src/lib/createPlugin.js new file mode 100644 index 0000000..7a49053 --- /dev/null +++ b/packages/gitbook-core/src/lib/createPlugin.js @@ -0,0 +1,31 @@ + +const defaultInit = ((dispatch, getState) => {}); +const defaultReduce = ((state, action) => state); + +/** + * Create a plugin to extend the state and the views. + * + * @param {Function(dispatch, state)} onInitialState + * @param {Funciton(state, action)} onReduceState + * @return {Plugin} + */ +function createPlugin({ init, reduce, actions }) { + init = init || defaultInit; + reduce = reduce || defaultReduce; + actions = actions || {}; + + const plugin = { + init, + reduce, + actions + }; + + if (typeof window !== 'undefined') { + window.gitbookPlugins = window.gitbookPlugins || []; + window.gitbookPlugins.push(plugin); + } + + return plugin; +} + +module.exports = createPlugin; |