Skip to content
This repository has been archived by the owner on Oct 26, 2018. It is now read-only.

Commit

Permalink
Get devtools working
Browse files Browse the repository at this point in the history
  • Loading branch information
kimjoar committed Dec 4, 2015
1 parent ff0f82c commit 6fc0287
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -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';

Expand Down

0 comments on commit 6fc0287

Please sign in to comment.