diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-12-22 10:18:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-22 10:18:38 +0100 |
commit | 194ebc3da9641ff96f083f9d8ab43c2d27944f9a (patch) | |
tree | c50988f32ccf18df93ae7ab40be78e9459642818 /packages/gitbook-core/src/reducers/history.js | |
parent | 64ccb6b00b4b63fa0e516d4e35351275b34f8c07 (diff) | |
parent | 16af264360e48e8a833e9efa9ab8d194574dbc70 (diff) | |
download | gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.zip gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.tar.gz gitbook-194ebc3da9641ff96f083f9d8ab43c2d27944f9a.tar.bz2 |
Merge pull request #1543 from GitbookIO/dream
React for rendering website with plugins
Diffstat (limited to 'packages/gitbook-core/src/reducers/history.js')
-rw-r--r-- | packages/gitbook-core/src/reducers/history.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/reducers/history.js b/packages/gitbook-core/src/reducers/history.js new file mode 100644 index 0000000..be8fe42 --- /dev/null +++ b/packages/gitbook-core/src/reducers/history.js @@ -0,0 +1,82 @@ +const { Record, List } = require('immutable'); +const { createBrowserHistory, createMemoryHistory } = require('history'); +const ACTION_TYPES = require('../actions/TYPES'); +const Location = require('../models/Location'); + +const isServerSide = (typeof window === 'undefined'); + +const HistoryState = Record({ + // Current location + location: new Location(), + // Are we loading a new page + loading: Boolean(false), + // Did we fail loading a page? + error: null, + // Listener for history changes + listeners: List(), + // Function to call to stop listening + unlisten: null, + // HistoryJS instance + client: null +}); + +function reduceHistory(state, action) { + state = state || HistoryState(); + switch (action.type) { + + case ACTION_TYPES.PAGE_FETCH_START: + return state.merge({ + loading: true + }); + + case ACTION_TYPES.PAGE_FETCH_END: + return state.merge({ + loading: false + }); + + case ACTION_TYPES.PAGE_FETCH_ERROR: + return state.merge({ + loading: false, + error: action.error + }); + + case ACTION_TYPES.HISTORY_ACTIVATE: + const client = isServerSide ? createMemoryHistory() : createBrowserHistory(); + const unlisten = client.listen(action.listener); + + // We can't use .merge since it convert history to an immutable + const newState = state + // TODO: we should find a way to have the correct location on server side + .set('location', isServerSide ? new Location() : Location.fromNative(window.location)) + .set('client', client) + .set('unlisten', unlisten); + + return newState; + + case ACTION_TYPES.HISTORY_DEACTIVATE: + if (state.unlisten) { + state.unlisten(); + } + + return state.merge({ + client: null, + unlisten: null + }); + + case ACTION_TYPES.HISTORY_UPDATE: + return state.merge({ + location: action.location + }); + + case ACTION_TYPES.HISTORY_LISTEN: + return state.merge({ + listeners: state.listeners.push(action.listener) + }); + + default: + return state; + + } +} + +module.exports = reduceHistory; |