1
+ /** @internalapi @module vanilla */ /** */
1
2
import {
2
3
extend , assertPredicate , isFunction , isArray , isInjectable , $InjectorLike , IInjectable
3
4
} from "../common/module" ;
4
5
5
- /** angular1-like injector api */
6
6
// globally available injectables
7
7
let globals = { } ;
8
8
let STRIP_COMMENTS = / ( ( \/ \/ .* $ ) | ( \/ \* [ \s \S ] * ?\* \/ ) ) / mg;
9
9
let ARGUMENT_NAMES = / ( [ ^ \s , ] + ) / g;
10
10
11
+ /**
12
+ * A basic angular1-like injector api
13
+ *
14
+ * This object implements four methods similar to the
15
+ * [angular 1 dependency injector](https://docs.angularjs.org/api/auto/service/$injector)
16
+ *
17
+ * UI-Router evolved from an angular 1 library to a framework agnostic library.
18
+ * However, some of the `ui-router-core` code uses these ng1 style APIs to support ng1 style dependency injection.
19
+ *
20
+ * This object provides a naive implementation of a globally scoped dependency injection system.
21
+ * It supports the following DI approaches:
22
+ *
23
+ * ### Function parameter names
24
+ *
25
+ * A function's `.toString()` is called, and the parameter names are parsed.
26
+ * This only works when the parameter names aren't "mangled" by a minifier such as UglifyJS.
27
+ *
28
+ * ```js
29
+ * function injectedFunction(FooService, BarService) {
30
+ * // FooService and BarService are injected
31
+ * }
32
+ * ```
33
+ *
34
+ * ### Function annotation
35
+ *
36
+ * A function may be annotated with an array of dependency names as the `$inject` property.
37
+ *
38
+ * ```js
39
+ * injectedFunction.$inject = [ 'FooService', 'BarService' ];
40
+ * function injectedFunction(fs, bs) {
41
+ * // FooService and BarService are injected as fs and bs parameters
42
+ * }
43
+ * ```
44
+ *
45
+ * ### Array notation
46
+ *
47
+ * An array provides the names of the dependencies to inject (as strings).
48
+ * The function is the last element of the array.
49
+ *
50
+ * ```js
51
+ * [ 'FooService', 'BarService', function (fs, bs) {
52
+ * // FooService and BarService are injected as fs and bs parameters
53
+ * }]
54
+ * ```
55
+ *
56
+ * @type {$InjectorLike }
57
+ */
11
58
export const $injector = {
59
+ /** Gets an object from DI based on a string token */
12
60
get : name => globals [ name ] ,
13
61
62
+ /** Returns true if an object named `name` exists in global DI */
14
63
has : ( name ) => $injector . get ( name ) != null ,
15
64
65
+ /**
66
+ * Injects a function
67
+ *
68
+ * @param fn the function to inject
69
+ * @param context the function's `this` binding
70
+ * @param locals An object with additional DI tokens and values, such as `{ someToken: { foo: 1 } }`
71
+ */
16
72
invoke : ( fn : IInjectable , context ?, locals ?) => {
17
73
let all = extend ( { } , globals , locals || { } ) ;
18
74
let params = $injector . annotate ( fn ) ;
@@ -22,6 +78,12 @@ export const $injector = {
22
78
else return ( fn as any [ ] ) . slice ( - 1 ) [ 0 ] . apply ( context , args ) ;
23
79
} ,
24
80
81
+ /**
82
+ * Returns a function's dependencies
83
+ *
84
+ * Analyzes a function (or array) and returns an array of DI tokens that the function requires.
85
+ * @return an array of `string`s
86
+ */
25
87
annotate : ( fn : IInjectable ) : any [ ] => {
26
88
if ( ! isInjectable ( fn ) ) throw new Error ( `Not an injectable function: ${ fn } ` ) ;
27
89
if ( fn && ( fn as any ) . $inject ) return ( fn as any ) . $inject ;
0 commit comments