@joist/di 3.0.0-next.15 → 3.0.0-next.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/target/lib/injectable.js
CHANGED
|
@@ -3,10 +3,6 @@ import { environment } from './environment.js';
|
|
|
3
3
|
export function injectable(Base, _) {
|
|
4
4
|
return withInjector(Base);
|
|
5
5
|
}
|
|
6
|
-
/**
|
|
7
|
-
* This mixin is applied by the @injectable decorator.
|
|
8
|
-
* Id defines an instance injector and registers custom element lifecycle hooks
|
|
9
|
-
*/
|
|
10
6
|
function withInjector(Base) {
|
|
11
7
|
return class InjectableNode extends Base {
|
|
12
8
|
injector$$ = new Injector(Base.providers);
|
|
@@ -47,8 +43,6 @@ function withInjector(Base) {
|
|
|
47
43
|
}
|
|
48
44
|
function findInjectorRoot(e) {
|
|
49
45
|
const path = e.composedPath();
|
|
50
|
-
// find firt parent
|
|
51
|
-
// skips the first item which is the target
|
|
52
46
|
for (let i = 1; i < path.length; i++) {
|
|
53
47
|
const part = path[i];
|
|
54
48
|
if ('injector$$' in part && part.injector$$ instanceof Injector) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectable.js","sourceRoot":"","sources":["../../src/lib/injectable.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,QAAQ,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,UAAU,UAAU,CAA+B,IAAO,EAAE,CAAU;IAC1E,OAAO,YAAY,CAAC,IAAI,CAAC,CAAC;AAC5B,CAAC;
|
|
1
|
+
{"version":3,"file":"injectable.js","sourceRoot":"","sources":["../../src/lib/injectable.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,QAAQ,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,UAAU,UAAU,CAA+B,IAA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AG,CAAC,CAAC,YAAY,EAAE,CAAC;IAI9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACpC,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QAErB,IAAI,YAAY,IAAI,IAAI,IAAI,IAAI,CAAC,UAAU,YAAY,QAAQ,EAAE;YAC/D,OAAO,IAAI,CAAC,UAAU,CAAC;SACxB;KACF;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/target/lib/injector.d.ts
CHANGED
|
@@ -3,23 +3,6 @@ export type Injectable = object & {
|
|
|
3
3
|
injector$$?: Injector;
|
|
4
4
|
onInject?(): void;
|
|
5
5
|
};
|
|
6
|
-
/**
|
|
7
|
-
* Injectors create and store instances of services.
|
|
8
|
-
* A service is any constructable class.
|
|
9
|
-
* When calling Injector.get, the injector will resolve as following.
|
|
10
|
-
*
|
|
11
|
-
* 1. Do I have a cached instance locally?
|
|
12
|
-
* 2. Do I have a local provider definition for the token?
|
|
13
|
-
* 3. Do I have a parent? Check parent for 1 and 2
|
|
14
|
-
* 5. All clear, go ahead and construct and cache the requested service
|
|
15
|
-
*
|
|
16
|
-
* RootInjector --> InjectorA --> InjectorB
|
|
17
|
-
* |------> InjectorC
|
|
18
|
-
* |------> InjectorD --> InjectorE
|
|
19
|
-
*
|
|
20
|
-
* in the above tree, if InjectorE requests a service, it will navigate up to the RootInjector and cached.
|
|
21
|
-
* If Inject B then requests the same token, it will recieve the same cached instance,
|
|
22
|
-
*/
|
|
23
6
|
export declare class Injector {
|
|
24
7
|
#private;
|
|
25
8
|
providers: Provider<any>[];
|
package/target/lib/injector.js
CHANGED
|
@@ -1,41 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Injectors create and store instances of services.
|
|
3
|
-
* A service is any constructable class.
|
|
4
|
-
* When calling Injector.get, the injector will resolve as following.
|
|
5
|
-
*
|
|
6
|
-
* 1. Do I have a cached instance locally?
|
|
7
|
-
* 2. Do I have a local provider definition for the token?
|
|
8
|
-
* 3. Do I have a parent? Check parent for 1 and 2
|
|
9
|
-
* 5. All clear, go ahead and construct and cache the requested service
|
|
10
|
-
*
|
|
11
|
-
* RootInjector --> InjectorA --> InjectorB
|
|
12
|
-
* |------> InjectorC
|
|
13
|
-
* |------> InjectorD --> InjectorE
|
|
14
|
-
*
|
|
15
|
-
* in the above tree, if InjectorE requests a service, it will navigate up to the RootInjector and cached.
|
|
16
|
-
* If Inject B then requests the same token, it will recieve the same cached instance,
|
|
17
|
-
*/
|
|
18
1
|
export class Injector {
|
|
19
2
|
providers;
|
|
20
|
-
// ke track of isntances. One Token can have one instance
|
|
21
3
|
#instances = new WeakMap();
|
|
22
4
|
#parent = undefined;
|
|
23
5
|
constructor(providers = [], parent) {
|
|
24
6
|
this.providers = providers;
|
|
25
7
|
this.setParent(parent);
|
|
26
8
|
}
|
|
27
|
-
// resolves and retuns and instance of the requested service
|
|
28
9
|
get(token) {
|
|
29
|
-
// check for a local instance
|
|
30
10
|
if (this.#instances.has(token)) {
|
|
31
11
|
return this.#instances.get(token);
|
|
32
12
|
}
|
|
33
13
|
const provider = this.#findProvider(token);
|
|
34
|
-
// check for a provider definition
|
|
35
14
|
if (provider) {
|
|
36
15
|
return this.#createAndCache(provider.use);
|
|
37
16
|
}
|
|
38
|
-
// check for a parent and attempt to get there
|
|
39
17
|
if (this.#parent) {
|
|
40
18
|
return this.#parent.get(token);
|
|
41
19
|
}
|
|
@@ -51,12 +29,7 @@ export class Injector {
|
|
|
51
29
|
const instance = new token();
|
|
52
30
|
this.#instances.set(token, instance);
|
|
53
31
|
if (instance.injector$$) {
|
|
54
|
-
// set the this injector instance as a parent.
|
|
55
|
-
// this means that each calling injector will be the parent of what it creates.
|
|
56
|
-
// this allows the created service to navigate up it's chain to find a root
|
|
57
32
|
instance.injector$$.setParent(this);
|
|
58
|
-
// the on inject lifecycle hook should be called after the parent is defined.
|
|
59
|
-
// this ensures that services are initialized when the chain is settled
|
|
60
33
|
if (instance.onInject) {
|
|
61
34
|
instance.onInject();
|
|
62
35
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injector.js","sourceRoot":"","sources":["../../src/lib/injector.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"injector.js","sourceRoot":"","sources":["../../src/lib/injector.ts"],"names":[],"mappings":"AAyBA,MAAM,OAAO,QAAQ;IAMA;IAJnB,UAAU,GAAG,IAAI,OAAO,EAA2B,CAAC;IAEpD,OAAO,GAAyB,SAAS,CAAC;IAE1C,YAAmB,YAA6B,EAAE,EAAE,MAAiB;QAAlD,cAAS,GAAT,SAAS,CAAsB;QAChD,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;IAGD,GAAG,CAAuB,KAAuB;QAE/C,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;YAC9B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC;SACpC;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QAG3C,IAAI,QAAQ,EAAE;YACZ,OAAO,IAAI,CAAC,eAAe,CAAI,QAAQ,CAAC,GAAG,CAAC,CAAC;SAC9C;QAGD,IAAI,IAAI,CAAC,OAAO,EAAE;YAChB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;SAChC;QAED,OAAO,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IACrC,CAAC;IAED,SAAS,CAAC,MAA4B;QACpC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,UAAU,GAAG,IAAI,OAAO,EAAE,CAAC;IAClC,CAAC;IAED,eAAe,CAAuB,KAAuB;QAC3D,MAAM,QAAQ,GAAG,IAAI,KAAK,EAAE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