Skip to content

Commit 4baf7af

Browse files
authored
Add start method to router (#402)
* start method for router * Update tests and remove queuing evented * Type the options
1 parent 6b3cc6b commit 4baf7af

File tree

6 files changed

+24
-168
lines changed

6 files changed

+24
-168
lines changed

src/core/QueuingEvented.ts

-71
This file was deleted.

src/routing/Router.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import QueuingEvented from '../core/QueuingEvented';
1+
import Evented from '../core/Evented';
22
import { RouteConfig, History, OutletContext, Params, RouterInterface, Route, RouterOptions } from './interfaces';
33
import { HashHistory } from './history/HashHistory';
44
import { EventObject } from '../core/Evented';
@@ -36,25 +36,22 @@ function matchingParams({ params: previousParams }: OutletContext, { params }: O
3636
return Object.keys(params).every((key) => previousParams[key] === params[key]);
3737
}
3838

39-
export class Router extends QueuingEvented<{ nav: NavEvent; outlet: OutletEvent }> implements RouterInterface {
39+
export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> implements RouterInterface {
4040
private _routes: Route[] = [];
4141
private _outletMap: { [index: string]: Route } = Object.create(null);
4242
private _matchedOutlets: { [index: string]: OutletContext } = Object.create(null);
4343
private _currentParams: Params = {};
4444
private _currentQueryParams: Params = {};
4545
private _defaultOutlet: string | undefined;
46-
private _history: History;
46+
private _history!: History;
47+
private _options: RouterOptions;
4748

4849
constructor(config: RouteConfig[], options: RouterOptions = {}) {
4950
super();
50-
const { HistoryManager = HashHistory, base, window } = options;
51+
this._options = options;
5152
this._register(config);
52-
this._history = new HistoryManager({ onChange: this._onChange, base, window });
53-
if (this._matchedOutlets.errorOutlet && this._defaultOutlet) {
54-
const path = this.link(this._defaultOutlet);
55-
if (path) {
56-
this.setPath(path);
57-
}
53+
if (options.autostart || true) {
54+
this.start();
5855
}
5956
}
6057

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

67+
public start() {
68+
const { HistoryManager = HashHistory, base, window } = this._options;
69+
this._history = new HistoryManager({ onChange: this._onChange, base, window });
70+
if (this._matchedOutlets.errorOutlet && this._defaultOutlet) {
71+
const path = this.link(this._defaultOutlet);
72+
if (path) {
73+
this.setPath(path);
74+
}
75+
}
76+
}
77+
7078
/**
7179
* Generate a link for a given outlet identifier and optional params.
7280
*

src/routing/interfaces.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export interface RouterInterface {
104104
* The current params for matched routes
105105
*/
106106
readonly currentParams: Params;
107+
108+
start: () => void;
107109
}
108110

109111
export interface MatchDetails {
@@ -194,6 +196,7 @@ export interface History {
194196
}
195197

196198
export interface RouterOptions {
199+
autostart?: boolean;
197200
window?: Window;
198201
base?: string;
199202
HistoryManager?: HistoryConstructor;

tests/core/unit/QueuingEvented.ts

-84
This file was deleted.

tests/core/unit/all.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import './Destroyable';
22
import './Evented';
3-
import './QueuingEvented';
43
import './util';
54
import './Container';
65
import './WidgetBase';

tests/routing/unit/Router.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,9 @@ describe('Router', () => {
515515
assert.isUndefined(link);
516516
});
517517

518-
it('Queues the first event for the first registered listener', () => {
518+
it('The router will not start automatically if autostart is set to false', () => {
519519
let initialNavEvent = false;
520-
const router = new Router(routeConfigDefaultRoute, { HistoryManager });
520+
const router = new Router(routeConfigDefaultRoute, { HistoryManager, autostart: false });
521521
let navCount = 0;
522522
router.on('nav', (event) => {
523523
navCount++;
@@ -532,6 +532,7 @@ describe('Router', () => {
532532
initialNavEvent = true;
533533
}
534534
});
535+
router.start();
535536
assert.isTrue(initialNavEvent);
536537
});
537538
});

0 commit comments

Comments
 (0)