Skip to content

Commit

Permalink
fix(router): wait RAF
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Mar 26, 2018
1 parent a718f7e commit b26a563
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions core/src/components/router/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Router {
private busy = false;
private init = false;
private state = 0;
private lastState = 0;
private timer: any;

@Element() el: HTMLElement;
Expand All @@ -36,7 +37,13 @@ export class Router {
const tree = readRoutes(this.el);
this.routes = flattenRouterTree(tree);
this.redirects = readRedirects(this.el);
this.writeNavStateRoot(this.getPath(), RouterDirection.None);

this.historyDirection();

// TODO: use something else
requestAnimationFrame(() => {
this.writeNavStateRoot(this.getPath(), RouterDirection.None);
});
}

@Listen('ionRouteRedirectChanged')
Expand Down Expand Up @@ -71,17 +78,29 @@ export class Router {

@Listen('window:popstate')
protected onPopState() {
const direction = this.historyDirection();
const path = this.getPath();
console.debug('[ion-router] URL changed -> update nav', path, direction);
return this.writeNavStateRoot(path, direction);
}

private historyDirection() {
if (window.history.state === null) {
this.state++;
window.history.replaceState(this.state, document.title, document.location.href);
}
const direction = window.history.state >= this.state
? RouterDirection.Forward
: RouterDirection.Back;

const path = this.getPath();
console.debug('[ion-router] URL changed -> update nav', path, direction);
return this.writeNavStateRoot(path, direction);
const state = window.history.state;
const lastState = this.lastState;
this.lastState = state;

if (state > lastState) {
return RouterDirection.Forward;
} else if (state < lastState) {
return RouterDirection.Back;
} else {
return RouterDirection.None;
}
}

@Method()
Expand Down

0 comments on commit b26a563

Please sign in to comment.