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 fe7487a commit 4d5e97a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 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,6 +77,11 @@ 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;
Expand All @@ -87,14 +94,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 4d5e97a

Please sign in to comment.