diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-03 14:56:55 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-03 14:56:55 +0200 |
commit | d82032298dcbfc5c1a6d011a5afe72af9e21ab42 (patch) | |
tree | 8c8197be2527f1f3c0f72bc1a4ef711e26241622 /packages/gitbook-core/src | |
parent | 34eb35699d951783837faf026a60abadc888a010 (diff) | |
download | gitbook-d82032298dcbfc5c1a6d011a5afe72af9e21ab42.zip gitbook-d82032298dcbfc5c1a6d011a5afe72af9e21ab42.tar.gz gitbook-d82032298dcbfc5c1a6d011a5afe72af9e21ab42.tar.bz2 |
Fix new api for activate/deactivate
Diffstat (limited to 'packages/gitbook-core/src')
-rw-r--r-- | packages/gitbook-core/src/actions/TYPES.js | 16 | ||||
-rw-r--r-- | packages/gitbook-core/src/actions/navigation.js | 30 | ||||
-rw-r--r-- | packages/gitbook-core/src/components/I18nProvider.js | 3 | ||||
-rw-r--r-- | packages/gitbook-core/src/components/InjectedComponent.js | 6 | ||||
-rw-r--r-- | packages/gitbook-core/src/lib/createContext.js | 14 | ||||
-rw-r--r-- | packages/gitbook-core/src/lib/createPlugin.js | 5 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Context.js | 33 | ||||
-rw-r--r-- | packages/gitbook-core/src/reducers/navigation.js | 30 |
8 files changed, 99 insertions, 38 deletions
diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js index 43eaac0..6dcab4f 100644 --- a/packages/gitbook-core/src/actions/TYPES.js +++ b/packages/gitbook-core/src/actions/TYPES.js @@ -1,13 +1,15 @@ module.exports = { // Components - REGISTER_COMPONENT: 'components/register', - UNREGISTER_COMPONENT: 'components/unregister', + REGISTER_COMPONENT: 'components/register', + UNREGISTER_COMPONENT: 'components/unregister', // Navigation - NAVIGATION_LISTEN: 'navigation/listen', - PAGE_FETCH_START: 'navigation/fetch:start', - PAGE_FETCH_END: 'navigation/fetch:end', - PAGE_FETCH_ERROR: 'navigation/fetch:error', + NAVIGATION_ACTIVATE: 'navigation/activate', + NAVIGATION_DEACTIVATE: 'navigation/deactivate', + NAVIGATION_LISTEN: 'navigation/listen', + PAGE_FETCH_START: 'navigation/fetch:start', + PAGE_FETCH_END: 'navigation/fetch:end', + PAGE_FETCH_ERROR: 'navigation/fetch:error', // i18n - I18N_REGISTER_LOCALE: 'i18n/register:locale' + I18N_REGISTER_LOCALE: 'i18n/register:locale' }; diff --git a/packages/gitbook-core/src/actions/navigation.js b/packages/gitbook-core/src/actions/navigation.js index fa82bcb..f90f5a2 100644 --- a/packages/gitbook-core/src/actions/navigation.js +++ b/packages/gitbook-core/src/actions/navigation.js @@ -1,11 +1,7 @@ -const { createBrowserHistory, createMemoryHistory } = require('history'); - const ACTION_TYPES = require('./TYPES'); const getPayload = require('../lib/getPayload'); const Location = require('../models/Location'); -const isServerSide = (typeof window === 'undefined'); - const SUPPORTED = ( typeof window !== 'undefined' && window.history && window.history.pushState && window.history.replaceState && @@ -13,23 +9,21 @@ const SUPPORTED = ( !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/) ); -// Create tge history instance -const history = isServerSide ? createMemoryHistory() : createBrowserHistory(); - /** * Initialize the navigation */ function activate() { return (dispatch, getState) => { - const { listeners } = getState().navigation; - - history.listen(location => { + const listener = (location) => { + const { listeners } = getState().navigation; location = Location.fromNative(location); - listeners.forEach(listener => { - listener(location, dispatch, getState); + listeners.forEach(handler => { + handler(location, dispatch, getState); }); - }); + }; + + dispatch({ type: ACTION_TYPES.NAVIGATION_ACTIVATE, listener }); }; } @@ -37,9 +31,7 @@ function activate() { * Cleanup the navigation */ function deactivate() { - return (dispatch, getState) => { - - }; + return { type: ACTION_TYPES.NAVIGATION_DEACTIVATE }; } /** @@ -48,7 +40,8 @@ function deactivate() { * @return {Action} action */ function pushURI(location) { - return () => { + return (dispatch, getState) => { + const { history } = getState().navigation; location = Location.fromNative(location); if (SUPPORTED) { @@ -65,7 +58,8 @@ function pushURI(location) { * @return {Action} action */ function replaceURI(location) { - return () => { + return (dispatch, getState) => { + const { history } = getState().navigation; location = Location.fromNative(location); if (SUPPORTED) { diff --git a/packages/gitbook-core/src/components/I18nProvider.js b/packages/gitbook-core/src/components/I18nProvider.js index 89bf3a0..b6b2d0f 100644 --- a/packages/gitbook-core/src/components/I18nProvider.js +++ b/packages/gitbook-core/src/components/I18nProvider.js @@ -1,3 +1,4 @@ +const { Map } = require('immutable'); const React = require('react'); const intl = require('react-intl'); const ReactRedux = require('react-redux'); @@ -10,7 +11,7 @@ const I18nProvider = React.createClass({ render() { let { messages } = this.props; - messages = messages.get('en').toJS(); + messages = messages.get('en', Map()).toJS(); return ( <intl.IntlProvider locale={'en'} messages={messages}> diff --git a/packages/gitbook-core/src/components/InjectedComponent.js b/packages/gitbook-core/src/components/InjectedComponent.js index cb50e02..ca6e0b0 100644 --- a/packages/gitbook-core/src/components/InjectedComponent.js +++ b/packages/gitbook-core/src/components/InjectedComponent.js @@ -78,7 +78,11 @@ const InjectedComponent = React.createClass({ }, render() { - const { components, props, children } = this.props; + let { components, props, children } = this.props; + + if (!children) { + children = null; + } return components.reduce((inner, Comp) => { return ( diff --git a/packages/gitbook-core/src/lib/createContext.js b/packages/gitbook-core/src/lib/createContext.js index 2a826d1..c88baad 100644 --- a/packages/gitbook-core/src/lib/createContext.js +++ b/packages/gitbook-core/src/lib/createContext.js @@ -66,15 +66,17 @@ function createContext(plugins, initialState) { Redux.compose(Redux.applyMiddleware(thunk)) ); - // Initialize the plugins - plugins.forEach(plugin => { - plugin.init(store.dispatch, store.getState, actions); - }); - - return new Context({ + // Create the context + const context = new Context({ store, + plugins, actions }); + + // Initialize the plugins + context.activate(); + + return context; } module.exports = createContext; diff --git a/packages/gitbook-core/src/lib/createPlugin.js b/packages/gitbook-core/src/lib/createPlugin.js index a2a3f42..cb5d2be 100644 --- a/packages/gitbook-core/src/lib/createPlugin.js +++ b/packages/gitbook-core/src/lib/createPlugin.js @@ -8,9 +8,10 @@ const Plugin = require('../models/Plugin'); * @param {Object} plugin.actions * @return {Plugin} */ -function createPlugin({ init, reduce, actions }) { +function createPlugin({ activate, deactivate, reduce, actions }) { const plugin = new Plugin({ - init, + activate, + deactivate, reduce, actions }); diff --git a/packages/gitbook-core/src/models/Context.js b/packages/gitbook-core/src/models/Context.js index fa68bd8..f4b0d4c 100644 --- a/packages/gitbook-core/src/models/Context.js +++ b/packages/gitbook-core/src/models/Context.js @@ -1,11 +1,18 @@ -const { Record } = require('immutable'); +const { Record, List } = require('immutable'); const DEFAULTS = { store: null, - actions: {} + actions: {}, + plugins: List() }; class Context extends Record(DEFAULTS) { + constructor(...args) { + super(...args); + + this.dispatch = this.dispatch.bind(this); + this.getState = this.getState.bind(this); + } /** * Return current state @@ -24,6 +31,28 @@ class Context extends Record(DEFAULTS) { const { store } = this; return store.dispatch(action); } + + /** + * Deactivate the context, cleanup resources from plugins. + */ + deactivate() { + const { plugins, actions } = this; + + plugins.forEach(plugin => { + plugin.deactivate(this.dispatch, this.getState, actions); + }); + } + + /** + * Activate the context and the plugins. + */ + activate() { + const { plugins, actions } = this; + + plugins.forEach(plugin => { + plugin.activate(this.dispatch, this.getState, actions); + }); + } } module.exports = Context; diff --git a/packages/gitbook-core/src/reducers/navigation.js b/packages/gitbook-core/src/reducers/navigation.js index 439ba21..a6d71f8 100644 --- a/packages/gitbook-core/src/reducers/navigation.js +++ b/packages/gitbook-core/src/reducers/navigation.js @@ -1,13 +1,20 @@ const { Record, List } = require('immutable'); +const { createBrowserHistory, createMemoryHistory } = require('history'); const ACTION_TYPES = require('../actions/TYPES'); +const isServerSide = (typeof window === 'undefined'); + const NavigationState = Record({ // Are we loading a new page loading: Boolean(false), // Did we fail loading a page? error: null, // Listener for history changes - listeners: List() + listeners: List(), + // Function to call to stop listening + unlisten: null, + // History instance + history: null }); function reduceNavigation(state, action) { @@ -30,6 +37,27 @@ function reduceNavigation(state, action) { error: action.error }); + case ACTION_TYPES.NAVIGATION_ACTIVATE: + const history = isServerSide ? createMemoryHistory() : createBrowserHistory(); + const unlisten = history.listen(action.listener); + + // We can't use .merge since it convert history to an immutable + const newState = state + .set('history', history) + .set('unlisten', unlisten); + + return newState; + + case ACTION_TYPES.NAVIGATION_DEACTIVATE: + if (state.unlisten) { + state.unlisten(); + } + + return state.merge({ + history: null, + unlisten: null + }); + case ACTION_TYPES.NAVIGATION_LISTEN: return state.merge({ listeners: state.listeners.push(action.listener) |