Skip to content

Commit 761ffa4

Browse files
authored
fix(history): provide location and use named parameters (#2877)
* refactor(history): provide location and use named parameters * refactor(history): use routeState instead of uiState This is to avoid confusion with the object that is manipulated in the state mapping
1 parent c7e41cb commit 761ffa4

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/lib/routers/history.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import qs from 'qs';
22

3-
function defaultCreateURL(qsModule, uiState) {
4-
const { protocol, hostname, port = '', pathname, hash } = window.location;
5-
const queryString = qsModule.stringify(uiState);
3+
function defaultCreateURL({ qsModule, routeState, location }) {
4+
const { protocol, hostname, port = '', pathname, hash } = location;
5+
const queryString = qsModule.stringify(routeState);
66
const portWithPrefix = port === '' ? '' : `:${port}`;
77
// IE <= 11 has no location.origin or buggy. Therefore we don't rely on it
8-
if (!uiState || Object.keys(uiState).length === 0)
8+
if (!routeState || Object.keys(routeState).length === 0)
99
return `${protocol}//${hostname}${portWithPrefix}${pathname}${hash}`;
1010
else
1111
return `${protocol}//${hostname}${portWithPrefix}${pathname}?${queryString}${hash}`;
1212
}
1313

14-
function defaultParseURL(qsModule, location) {
14+
function defaultParseURL({ qsModule, location }) {
1515
return qsModule.parse(location.search.slice(1));
1616
}
1717

@@ -48,20 +48,20 @@ class BrowserHistory {
4848

4949
/**
5050
* This method pushes a search state into the URL.
51-
* @param {object} uiState a syncable UI state
51+
* @param {object} routeState a syncable UI state
5252
* @return {undefined}
5353
*/
54-
write(uiState) {
55-
const url = this.createURL(uiState);
56-
const title = this.windowTitle && this.windowTitle(uiState);
54+
write(routeState) {
55+
const url = this.createURL(routeState);
56+
const title = this.windowTitle && this.windowTitle(routeState);
5757

5858
if (this.writeTimer) {
5959
window.clearTimeout(this.writeTimer);
6060
}
6161

6262
this.writeTimer = setTimeout(() => {
6363
if (title) window.document.title = title;
64-
window.history.pushState(uiState, title || '', url);
64+
window.history.pushState(routeState, title || '', url);
6565
this.writeTimer = undefined;
6666
}, this.writeDelay);
6767
}
@@ -71,13 +71,13 @@ class BrowserHistory {
7171
* @return {object} the equivalent to what is store in the URL as an object
7272
*/
7373
read() {
74-
return this.parseURL(qs, window.location);
74+
return this.parseURL({ qsModule: qs, location: window.location });
7575
}
7676

7777
/**
7878
* This methods sets a callback on the `onpopstate` event of the history API
7979
* of the current page. This way, the URL sync can keep track of the changes.
80-
* @param {function(object)} cb the callback that will receive the latest uiState.
80+
* @param {function(object)} cb the callback that will receive the latest routeState.
8181
* It is called when the URL is updated.
8282
* @returns {undefined}
8383
*/
@@ -87,14 +87,14 @@ class BrowserHistory {
8787
window.clearTimeout(this.writeTimer);
8888
this.writeTimer = undefined;
8989
}
90-
const uiState = event.state;
90+
const routeState = event.state;
9191
// at initial load, the state is read from the URL without
9292
// update. Therefore the state object is not there. In this
9393
// case we fallback and read the URL.
94-
if (!uiState) {
94+
if (!routeState) {
9595
cb(this.read());
9696
} else {
97-
cb(uiState);
97+
cb(routeState);
9898
}
9999
};
100100
window.addEventListener('popstate', this._onPopState);
@@ -107,11 +107,15 @@ class BrowserHistory {
107107
* This way we can handle cases like using a <base href>, see
108108
* https://github.com/algolia/instantsearch.js/issues/790 for the original issue
109109
*
110-
* @param {object} uiState a syncable UI state
110+
* @param {object} routeState a syncable UI state
111111
* @returns {string} the full URL for the provided syncable state
112112
*/
113-
createURL(uiState) {
114-
return this._createURL(qs, uiState);
113+
createURL(routeState) {
114+
return this._createURL({
115+
qsModule: qs,
116+
routeState,
117+
location: window.location,
118+
});
115119
}
116120

117121
/**

src/lib/stateMappings/simple.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class SimpleUIStateMapping {
22
stateToRoute(uiState) {
33
return uiState;
44
}
5-
routeToState(syncable) {
6-
return syncable;
5+
routeToState(routeState) {
6+
return routeState;
77
}
88
}
99

0 commit comments

Comments
 (0)