Skip to content

Commit

Permalink
Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
kuu12 committed Sep 5, 2018
1 parent d32137b commit 942d1ce
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/router.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class Router extends React.Component {
this.registry = {};
this.cache = {};
this.state = locationState(this.basename);
this.start = window.history && history.length;
this.end = this.start;

this.point = history.length;
if (history.replaceState)
history.replaceState({ i: this.point }, '');

proxy.router = this;
}
componentWillUnmount() {
Expand All @@ -34,35 +37,32 @@ class Router extends React.Component {
}

push(pathname, cb) {
return this.__change__('pushState', pathname, () => {
this.end = window.history && history.length;
if (cb) cb();
});
return this.__change__(1, pathname, cb);
}
replace(pathname, cb) {
return this.__change__('replaceState', pathname, cb);
return this.__change__(0, pathname, cb);
}
__change__(method, pathname, cb) {
__change__(step, pathname, cb) {
if (!pathname.startsWith('/')) {
console.error(new Error(PATH_START + pathname));
}
const href = this.basename + pathname;

if (!window.history || !history[method]) {
if (!history.replaceState) {
location.href = href;
return cb && cb();
}
history[method]({}, null, href);
history[step ? 'pushState' : 'replaceState'](
{ i: history.state.i + step }, '', href);
return this.__updateState__(separate(pathname), cb);
}

back(pathname, cb) {
return history.length > this.start
return !history.state || (history.state.i > this.point)
? this.go(-1, cb)
: this.replace(pathname, cb);
}
forward(pathname, cb) {
return history.length < this.end
return !history.state || (history.state.i < history.length)
? this.go(1, cb)
: this.push(pathname, cb);
}
Expand Down

0 comments on commit 942d1ce

Please sign in to comment.