Skip to content

Commit d48c9fb

Browse files
committed
feat(UIRouterReact): throw if start is called more than once
fixes #65
1 parent 70c65af commit d48c9fb

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/__tests__/core.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ describe('UIRouterReact class', () => {
2727
expect(stub1.calledOnce).toBe(true);
2828
expect(stub2.calledOnce).toBe(true);
2929
});
30+
31+
it('should throw if \`start\` is called more than once', () => {
32+
expect(() => {
33+
router.start();
34+
router.start();
35+
}).toThrow();
36+
});
3037
});

src/core.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ import {ReactViewConfig, reactViewsBuilder} from './reactViews';
2222
let viewConfigFactory = (node: [PathNode], config: ReactViewDeclaration) =>
2323
new ReactViewConfig(node, config);
2424

25+
/** @hidden */
26+
const StartMethodCalledMoreThanOnceError = new Error(`
27+
The Router.start() method has been called more than once.
28+
29+
The <UIRouter> component calls start() as final step of the initialization and you shouldn't need to call it manually.
30+
`);
31+
2532
/**
2633
* The main UIRouter object
2734
*
@@ -31,6 +38,7 @@ let viewConfigFactory = (node: [PathNode], config: ReactViewDeclaration) =>
3138
* This class has references to all the other UIRouter services.
3239
*/
3340
export class UIRouterReact extends UIRouter {
41+
started = false;
3442
/**
3543
* Creates a new UIRouter instance
3644
*
@@ -56,8 +64,14 @@ export class UIRouterReact extends UIRouter {
5664
* It also performs the initial state synchronization from the URL.
5765
*/
5866
start(): void {
59-
this.urlMatcherFactory.$get();
60-
this.urlRouter.listen();
61-
this.urlRouter.sync();
67+
// Throw error if user calls `start` more than once
68+
if (this.started) {
69+
throw StartMethodCalledMoreThanOnceError;
70+
} else {
71+
this.urlMatcherFactory.$get();
72+
this.urlRouter.listen();
73+
this.urlRouter.sync();
74+
this.started = true;
75+
}
6276
}
6377
}

0 commit comments

Comments
 (0)