From 6fc02876d5fa4f042ab00e4ff417900d3f291cc0 Mon Sep 17 00:00:00 2001 From: Kim Joar Bekkelund Date: Fri, 4 Dec 2015 06:54:44 +0100 Subject: [PATCH] Get devtools working --- src/index.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 8d62e53..febb209 100644 --- a/src/index.js +++ b/src/index.js @@ -61,6 +61,8 @@ function locationsAreEqual(a, b) { function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) { const getRouterState = () => selectRouterState(store.getState()); let lastChangeId = 0; + let firstRoute = undefined; + let lastRoute = {}; if(!getRouterState()) { throw new Error( @@ -75,10 +77,17 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) { state: location.state }; + if (firstRoute === undefined) { + firstRoute = route; + } + console.log('HISTORY', route); + // Avoid dispatching an action if the store is already up-to-date, // even if `history` wouldn't do anything if the location is the same if(locationsAreEqual(getRouterState(), route)) return; + lastRoute = route; + const updatePath = location.action === 'REPLACE' ? replacePath : pushPath; @@ -87,14 +96,22 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) { }); const unsubscribeStore = store.subscribe(() => { - const routing = getRouterState(); + let routing = getRouterState(); + console.log('STORE', routing); + + if (routing === initialState) { + routing = firstRoute; + } // Only update the router once per `pushPath` call. This is // indicated by the `changeId` state; when that number changes, we // should update the history. - if(lastChangeId === routing.changeId) return; + if(lastChangeId === routing.changeId) { + if(locationsAreEqual(routing, lastRoute)) return; + } lastChangeId = routing.changeId; + lastRoute = routing; const method = routing.replace ? 'replaceState' : 'pushState';