diff options
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) |