Skip to content

Commit

Permalink
refactor(router): strict browser-navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
jwx committed Aug 24, 2019
1 parent 4d6f5c5 commit f86024d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions packages/router/src/browser-navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Call {

interface ForwardedState {
suppressPopstate?: boolean;
resolve?: ((value?: void | PromiseLike<void>) => void);
resolve?: ((value: void | PromiseLike<void>) => void) | null;
}
export class BrowserNavigator implements INavigatorStore, INavigatorViewer {
public static readonly inject: readonly Key[] = [ILifecycle, IDOM];
Expand All @@ -28,7 +28,7 @@ export class BrowserNavigator implements INavigatorStore, INavigatorViewer {

private readonly pendingCalls: Queue<Call>;
private isActive: boolean = false;
private callback: (ev?: INavigatorViewerEvent) => void = null;
private callback: ((ev: INavigatorViewerEvent) => void) | null = null;

private forwardedState: ForwardedState = {};

Expand Down Expand Up @@ -91,21 +91,23 @@ export class BrowserNavigator implements INavigatorStore, INavigatorViewer {
return this.enqueue(this, 'popState', []);
}

public readonly handlePopstate = (ev: PopStateEvent): Promise<void> => {
public readonly handlePopstate = (ev: PopStateEvent | null): Promise<void> => {
return this.enqueue(this, 'popstate', [ev]);
}

private popstate(ev: PopStateEvent, resolve: ((value?: void | PromiseLike<void>) => void), suppressPopstate: boolean = false): void {
if (!suppressPopstate) {
const { pathname, search, hash } = this.location;
this.callback({
event: ev,
state: this.history.state,
path: pathname,
data: search,
hash,
instruction: this.useHash ? hash.slice(1) : pathname,
});
if (this.callback) {
this.callback({
event: ev,
state: this.history.state,
path: pathname,
data: search,
hash,
instruction: this.useHash ? hash.slice(1) : pathname,
});
}
}
if (resolve) {
resolve();
Expand Down Expand Up @@ -136,7 +138,7 @@ export class BrowserNavigator implements INavigatorStore, INavigatorViewer {

if (suppressPopstate !== undefined) {
// Due to (browser) events not having a promise, we create and propagate one
let resolve: ((value?: void | PromiseLike<void>) => void);
let resolve: ((value: void | PromiseLike<void>) => void) | null = null;
// tslint:disable-next-line:promise-must-complete
promises.push(new Promise(_resolve => {
resolve = _resolve;
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Queue<T> {
private lifecycle: ILifecycle;

constructor(
private readonly callback: (item?: QueueItem<T>) => void
private readonly callback: (item: QueueItem<T>) => void
) { }

public get length(): number {
Expand Down

0 comments on commit f86024d

Please sign in to comment.