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/reducers | |
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/reducers')
-rw-r--r-- | packages/gitbook-core/src/reducers/navigation.js | 30 |
1 files changed, 29 insertions, 1 deletions
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) |