diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-03 15:48:28 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-03 15:48:28 +0200 |
commit | 4049279269ede7cdc4887ffc41a4ba78f878af2f (patch) | |
tree | c6bf9a40cda9df9bfadee2145815485a39a031b0 /packages/gitbook-core/src | |
parent | b04befd67cb0a7a1a5935bd749d9f08851166db6 (diff) | |
download | gitbook-4049279269ede7cdc4887ffc41a4ba78f878af2f.zip gitbook-4049279269ede7cdc4887ffc41a4ba78f878af2f.tar.gz gitbook-4049279269ede7cdc4887ffc41a4ba78f878af2f.tar.bz2 |
Emit current location when listening
Diffstat (limited to 'packages/gitbook-core/src')
-rw-r--r-- | packages/gitbook-core/src/actions/navigation.js | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/packages/gitbook-core/src/actions/navigation.js b/packages/gitbook-core/src/actions/navigation.js index bb58595..bcb4619 100644 --- a/packages/gitbook-core/src/actions/navigation.js +++ b/packages/gitbook-core/src/actions/navigation.js @@ -14,16 +14,35 @@ const SUPPORTED = ( */ function activate() { return (dispatch, getState) => { - const listener = (location) => { - const { listeners } = getState().navigation; - location = Location.fromNative(location); + dispatch({ + type: ACTION_TYPES.NAVIGATION_ACTIVATE, + listener: () => dispatch(emit()) + }); - listeners.forEach(handler => { - handler(location, dispatch, getState); - }); - }; + // Trigger for existing listeners + dispatch(emit()); + }; +} - dispatch({ type: ACTION_TYPES.NAVIGATION_ACTIVATE, listener }); +/** + * Emit current location + * @param {List|Array<Function>} to? + */ +function emit(to) { + return (dispatch, getState) => { + const { listeners, history } = getState().navigation; + + if (!history) { + return; + } + + const location = Location.fromNative(history.location); + + to = to || listeners; + + to.forEach(handler => { + handler(location, dispatch, getState); + }); }; } @@ -87,7 +106,12 @@ function redirect(uri) { * @return {Action} action */ function listen(listener) { - return { type: ACTION_TYPES.NAVIGATION_LISTEN, listener }; + return (dispatch, getState) => { + dispatch({ type: ACTION_TYPES.NAVIGATION_LISTEN, listener }); + + // Trigger for existing listeners + dispatch(emit([ listener ])); + }; } /** |