Skip to content

Commit

Permalink
fix(router): change events when URL changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed May 10, 2018
1 parent 23d86eb commit ece86ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
27 changes: 13 additions & 14 deletions core/src/components/router/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { chainToPath, generatePath, parsePath, readPath, writePath } from './uti
})
export class Router {

private previousPath: string|null = null;
private previousPath: string | null = null;
private busy = false;
private state = 0;
private lastState = 0;
Expand Down Expand Up @@ -187,14 +187,18 @@ export class Router {
this.busy = true;

// generate route event and emit will change
const event = this.routeChangeEvent(path, redirectFrom);
this.ionRouteWillChange.emit(event);
const routeEvent = this.routeChangeEvent(path, redirectFrom);
routeEvent && this.ionRouteWillChange.emit(routeEvent);

const changed = await writeNavState(node, chain, direction, index);
this.busy = false;

if (changed) {
console.debug('[ion-router] route changed', path);
}

// emit did change
this.emitRouteDidChange(event, changed);
routeEvent && this.ionRouteDidChange.emit(routeEvent);

return changed;
}
Expand All @@ -208,18 +212,13 @@ export class Router {
return readPath(this.win.location, this.root, this.useHash);
}

private emitRouteDidChange(event: RouterEventDetail, changed: boolean) {
console.debug('[ion-router] route changed', event.to);
this.previousPath = event.to;
this.ionRouteDidChange.emit({
...event,
changed
});
}

private routeChangeEvent(path: string[], redirectFromPath: string[]|null) {
private routeChangeEvent(path: string[], redirectFromPath: string[]|null): RouterEventDetail | null {
const from = this.previousPath;
const to = generatePath(path);
this.previousPath = to;
if (to === from) {
return null;
}
const redirectedFrom = redirectFromPath ? generatePath(redirectFromPath) : null;
return {
from,
Expand Down
1 change: 0 additions & 1 deletion core/src/components/router/utils/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface RouterEventDetail {
from: string|null;
redirectedFrom: string|null;
to: string;
changed?: boolean;
}

export const enum RouterDirection {
Expand Down

0 comments on commit ece86ee

Please sign in to comment.