Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add start method to router #402

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions src/core/QueuingEvented.ts

This file was deleted.

28 changes: 18 additions & 10 deletions src/routing/Router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import QueuingEvented from '../core/QueuingEvented';
import Evented from '../core/Evented';
import { RouteConfig, History, OutletContext, Params, RouterInterface, Route, RouterOptions } from './interfaces';
import { HashHistory } from './history/HashHistory';
import { EventObject } from '../core/Evented';
Expand Down Expand Up @@ -36,25 +36,22 @@ function matchingParams({ params: previousParams }: OutletContext, { params }: O
return Object.keys(params).every((key) => previousParams[key] === params[key]);
}

export class Router extends QueuingEvented<{ nav: NavEvent; outlet: OutletEvent }> implements RouterInterface {
export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> implements RouterInterface {
private _routes: Route[] = [];
private _outletMap: { [index: string]: Route } = Object.create(null);
private _matchedOutlets: { [index: string]: OutletContext } = Object.create(null);
private _currentParams: Params = {};
private _currentQueryParams: Params = {};
private _defaultOutlet: string | undefined;
private _history: History;
private _history!: History;
private _options: RouterOptions;

constructor(config: RouteConfig[], options: RouterOptions = {}) {
super();
const { HistoryManager = HashHistory, base, window } = options;
this._options = options;
this._register(config);
this._history = new HistoryManager({ onChange: this._onChange, base, window });
if (this._matchedOutlets.errorOutlet && this._defaultOutlet) {
const path = this.link(this._defaultOutlet);
if (path) {
this.setPath(path);
}
if (options.autostart || true) {
this.start();
}
}

Expand All @@ -67,6 +64,17 @@ export class Router extends QueuingEvented<{ nav: NavEvent; outlet: OutletEvent
this._history.set(path);
}

public start() {
const { HistoryManager = HashHistory, base, window } = this._options;
this._history = new HistoryManager({ onChange: this._onChange, base, window });
if (this._matchedOutlets.errorOutlet && this._defaultOutlet) {
const path = this.link(this._defaultOutlet);
if (path) {
this.setPath(path);
}
}
}

/**
* Generate a link for a given outlet identifier and optional params.
*
Expand Down
3 changes: 3 additions & 0 deletions src/routing/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export interface RouterInterface {
* The current params for matched routes
*/
readonly currentParams: Params;

start: () => void;
}

export interface MatchDetails {
Expand Down Expand Up @@ -194,6 +196,7 @@ export interface History {
}

export interface RouterOptions {
autostart?: boolean;
window?: Window;
base?: string;
HistoryManager?: HistoryConstructor;
Expand Down
84 changes: 0 additions & 84 deletions tests/core/unit/QueuingEvented.ts

This file was deleted.

1 change: 0 additions & 1 deletion tests/core/unit/all.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import './Destroyable';
import './Evented';
import './QueuingEvented';
import './util';
import './Container';
import './WidgetBase';
Expand Down
5 changes: 3 additions & 2 deletions tests/routing/unit/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ describe('Router', () => {
assert.isUndefined(link);
});

it('Queues the first event for the first registered listener', () => {
it('The router will not start automatically if autostart is set to false', () => {
let initialNavEvent = false;
const router = new Router(routeConfigDefaultRoute, { HistoryManager });
const router = new Router(routeConfigDefaultRoute, { HistoryManager, autostart: false });
let navCount = 0;
router.on('nav', (event) => {
navCount++;
Expand All @@ -532,6 +532,7 @@ describe('Router', () => {
initialNavEvent = true;
}
});
router.start();
assert.isTrue(initialNavEvent);
});
});