Skip to content

Dispatch even if path has not changed & normalize Locations #1031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions modules/__tests__/Router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ describe('Router', function () {
location.pop();
expect(div.innerHTML).toMatch(/Bar/);
}, Async.delay / 2);
});

setTimeout(function () {
expect(div.innerHTML).toMatch(/Bar/);
done();
}, Async.delay + 10);
steps.push(function () {
expect(div.innerHTML).toMatch(/Bar/);
done();
});

router = Router.create({
Expand Down Expand Up @@ -314,11 +314,11 @@ describe('Router', function () {
location.pop();
expect(div.innerHTML).toMatch(/Bar/);
}, RedirectToFooAsync.delay / 2);
});

setTimeout(function () {
expect(div.innerHTML).toMatch(/Bar/);
done();
}, RedirectToFooAsync.delay + 10);
steps.push(function () {
expect(div.innerHTML).toMatch(/Bar/);
done();
});

router = Router.create({
Expand Down Expand Up @@ -421,9 +421,14 @@ describe('Router', function () {

var div = document.createElement('div');

var runCount = 0;
Router.run(routes, location, function (Handler) {
runCount += 1;
React.render(<Handler/>, div, function () {
location.push('/abort');
if (runCount === 1){
location.push('/abort');
return;
}
expect(div.innerHTML).toMatch(/Foo/);
expect(location.getCurrentPath()).toEqual('/foo');
done();
Expand Down Expand Up @@ -482,11 +487,11 @@ describe('Router', function () {
location.pop();
expect(div.innerHTML).toMatch(/Bar/);
}, Async.delay / 2);
});

setTimeout(function () {
expect(div.innerHTML).toMatch(/Bar/);
done();
}, Async.delay + 10);
steps.push(function () {
expect(div.innerHTML).toMatch(/Bar/);
done();
});

router = Router.create({
Expand Down Expand Up @@ -741,7 +746,8 @@ describe('Router', function () {

Router.run(routes, location, function (Handler, state) {
React.render(<Handler/>, div, function () {
location.push('/baz');
if (state.path !== '/baz')
location.push('/baz');
});
});
});
Expand Down
7 changes: 6 additions & 1 deletion modules/actions/LocationActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ var LocationActions = {
/**
* Indicates the most recent entry should be removed from the history stack.
*/
POP: 'pop'
POP: 'pop',

/**
* Indicates the current location should be refreshed.
*/
REFRESH: 'refresh'

};

Expand Down
6 changes: 1 addition & 5 deletions modules/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,6 @@ function createRouter(options) {
Router.cancelPendingTransition();

var prevPath = state.path;
var isRefreshing = action == null;

if (prevPath === path && !isRefreshing)
return; // Nothing to do!

// Record the scroll position as early as possible to
// get it before browsers try update it automatically.
Expand Down Expand Up @@ -433,7 +429,7 @@ function createRouter(options) {
},

refresh: function () {
Router.dispatch(location.getCurrentPath(), null);
Router.dispatch(location.getCurrentPath(), LocationActions.REFRESH);
},

stop: function () {
Expand Down
27 changes: 21 additions & 6 deletions modules/locations/HashLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function onHashChange() {
}
}

function refresh() {
_actionType = LocationActions.REFRESH;
onHashChange();
}

/**
* A Location that uses `window.location.hash`.
*/
Expand Down Expand Up @@ -81,15 +86,25 @@ var HashLocation = {
},

push(path) {
_actionType = LocationActions.PUSH;
window.location.hash = path;
// ensure change event if path has not changed
if (path !== HashLocation.getCurrentPath()) {
_actionType = LocationActions.PUSH;
window.location.hash = path;
} else {
refresh();
}
},

replace(path) {
_actionType = LocationActions.REPLACE;
window.location.replace(
window.location.pathname + window.location.search + '#' + path
);
// ensure change event if path has not changed
if (path !== HashLocation.getCurrentPath()) {
_actionType = LocationActions.REPLACE;
window.location.replace(
window.location.pathname + window.location.search + '#' + path
);
} else {
refresh();
}
},

pop() {
Expand Down
20 changes: 15 additions & 5 deletions modules/locations/HistoryLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,24 @@ var HistoryLocation = {
},

push(path) {
window.history.pushState({ path: path }, '', path);
History.length += 1;
notifyChange(LocationActions.PUSH);
// don't push state if path has not changed
if (path !== HistoryLocation.getCurrentPath()) {
window.history.pushState({ path: path }, '', path);
History.length += 1;
notifyChange(LocationActions.PUSH);
} else {
notifyChange(LocationActions.REFRESH);
}
},

replace(path) {
window.history.replaceState({ path: path }, '', path);
notifyChange(LocationActions.REPLACE);
// don't replace state if path has not changed
if (path !== HistoryLocation.getCurrentPath()) {
window.history.replaceState({ path: path }, '', path);
notifyChange(LocationActions.REPLACE);
} else {
notifyChange(LocationActions.REFRESH);
}
},

pop: History.back,
Expand Down
18 changes: 13 additions & 5 deletions modules/locations/TestLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ class TestLocation {
}

push(path) {
this.history.push(path);
this._updateHistoryLength();
this._notifyChange(LocationActions.PUSH);
if (!this.history.length || path !== this.getCurrentPath()) {
this.history.push(path);
this._updateHistoryLength();
this._notifyChange(LocationActions.PUSH);
} else {
this._notifyChange(LocationActions.REFRESH);
}
}

replace(path) {
Expand All @@ -53,9 +57,13 @@ class TestLocation {
'You cannot replace the current path with no history'
);

this.history[this.history.length - 1] = path;
if (path !== this.getCurrentPath()) {
this.history[this.history.length - 1] = path;
this._notifyChange(LocationActions.REPLACE);
} else {
this._notifyChange(LocationActions.REFRESH);
}

this._notifyChange(LocationActions.REPLACE);
}

pop() {
Expand Down