Skip to content

Commit b9f4541

Browse files
feat(plugin): Allow registration by ES6 class, JS constructor fn, JS factory fn
1 parent 241d972 commit b9f4541

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/router.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ export class UIRouter {
6969
*
7070
* #### Example:
7171
* ```js
72-
* export class MyAuthPlugin {
72+
* export class MyAuthPlugin implements UIRouterPlugin {
7373
* constructor(router: UIRouter, options: any) {
74+
* this.name = "MyAuthPlugin";
7475
* let $transitions = router.transitionService;
7576
* let $state = router.stateService;
7677
*
@@ -90,12 +91,20 @@ export class UIRouter {
9091
* }
9192
* ```
9293
*
93-
* @param pluginFactory a function which accepts a [[UIRouter]] instance and returns a UI-Router Plugin instance
94-
* @param options options to pass to the plugin
95-
* @returns {T}
94+
* @param plugin one of:
95+
* - a plugin class which implements [[UIRouterPlugin]]
96+
* - a constructor function for a [[UIRouterPlugin]] which accepts a [[UIRouter]] instance
97+
* - a factory function which accepts a [[UIRouter]] instance and returns a [[UIRouterPlugin]] instance
98+
* @param options options to pass to the plugin class/factory
99+
* @returns the registered plugin instance
96100
*/
97-
plugin<T extends UIRouterPlugin>(pluginFactory: PluginFactory<T>, options: any = {}): T {
98-
let pluginInstance = pluginFactory(this, options);
101+
plugin<T extends UIRouterPlugin>(plugin: { new(router: UIRouter, options?: any): T }, options?: any): T;
102+
/** Allow javascript constructor function */
103+
plugin<T extends UIRouterPlugin>(plugin: { (router: UIRouter, options?: any): void }, options?: any): T;
104+
/** Allow javascript factory function */
105+
plugin<T extends UIRouterPlugin>(plugin: PluginFactory<T>, options?: any): T;
106+
plugin<T extends UIRouterPlugin>(plugin: any, options: any = {}): T {
107+
let pluginInstance = new plugin(this, options);
99108
if (!pluginInstance.name) throw new Error("Required property `name` missing on plugin: " + pluginInstance);
100109
return this._plugins[pluginInstance.name] = pluginInstance;
101110
}

test/pluginSpec.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,37 @@ describe('plugin api', function () {
2222
router.stateRegistry.stateQueue.autoFlush($state);
2323
});
2424

25-
class FancyPlugin implements UIRouterPlugin {
25+
class FancyPluginClass implements UIRouterPlugin {
2626
constructor(public router: UIRouter) { }
2727
name = "fancyplugin"
2828
}
2929

30+
function FancyPluginConstructor(router: UIRouter, options: any) {
31+
this.name = "fancyplugin";
32+
}
33+
3034
describe('initialization', () => {
35+
it('should accept a plugin class', () => {
36+
let plugin = router.plugin(FancyPluginClass);
37+
expect(plugin instanceof FancyPluginClass).toBeTruthy();
38+
});
39+
40+
it('should accept a constructor function', () => {
41+
let plugin = router.plugin(FancyPluginConstructor);
42+
expect(plugin instanceof FancyPluginConstructor).toBeTruthy();
43+
});
44+
45+
it('should accept a factory function', () => {
46+
function factoryFn(router: UIRouter, options: any) {
47+
return new FancyPluginClass(router);
48+
}
49+
let plugin = router.plugin(factoryFn);
50+
expect(plugin instanceof FancyPluginClass).toBeTruthy();
51+
});
52+
3153
it('should return an instance of the plugin', () => {
32-
let plugin = router.plugin(() => new FancyPlugin(router));
33-
expect(plugin instanceof FancyPlugin).toBeTruthy();
54+
let plugin = router.plugin(() => new FancyPluginClass(router));
55+
expect(plugin instanceof FancyPluginClass).toBeTruthy();
3456
});
3557

3658
it('should pass the router instance to the plugin constructor', () => {
@@ -55,9 +77,9 @@ describe('plugin api', function () {
5577

5678
describe('getPlugin', () => {
5779
it('should return the plugin instance', () => {
58-
router.plugin(() => new FancyPlugin(router));
80+
router.plugin(() => new FancyPluginClass(router));
5981
let plugin = router.getPlugin('fancyplugin');
60-
expect(plugin instanceof FancyPlugin).toBeTruthy();
82+
expect(plugin instanceof FancyPluginClass).toBeTruthy();
6183
});
6284
})
6385
});

0 commit comments

Comments
 (0)