@joist/di 4.0.0-next.1 → 4.0.0-next.11

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.
Files changed (66) hide show
  1. package/README.md +52 -34
  2. package/package.json +4 -14
  3. package/src/lib/dom-injector.test.ts +16 -18
  4. package/src/lib/dom-injector.ts +3 -4
  5. package/src/lib/inject.test.ts +33 -56
  6. package/src/lib/inject.ts +4 -6
  7. package/src/lib/injectable-el.test.ts +130 -0
  8. package/src/lib/injectable-el.ts +63 -0
  9. package/src/lib/injectable.test.ts +19 -116
  10. package/src/lib/injectable.ts +21 -57
  11. package/src/lib/injector.test.ts +132 -130
  12. package/src/lib/injector.ts +33 -19
  13. package/src/lib/lifecycle.test.ts +68 -64
  14. package/src/lib/lifecycle.ts +19 -4
  15. package/src/lib/metadata.ts +12 -0
  16. package/src/lib/provider.ts +16 -8
  17. package/src/lib.ts +1 -1
  18. package/target/lib/dom-injector.js +3 -4
  19. package/target/lib/dom-injector.js.map +1 -1
  20. package/target/lib/dom-injector.test.js +16 -18
  21. package/target/lib/dom-injector.test.js.map +1 -1
  22. package/target/lib/inject.js +3 -3
  23. package/target/lib/inject.js.map +1 -1
  24. package/target/lib/inject.test.js +58 -90
  25. package/target/lib/inject.test.js.map +1 -1
  26. package/target/lib/injectable-el.d.ts +334 -0
  27. package/target/lib/injectable-el.js +40 -0
  28. package/target/lib/injectable-el.js.map +1 -0
  29. package/target/lib/injectable-el.test.js +238 -0
  30. package/target/lib/injectable-el.test.js.map +1 -0
  31. package/target/lib/injectable.d.ts +5 -7
  32. package/target/lib/injectable.js +13 -42
  33. package/target/lib/injectable.js.map +1 -1
  34. package/target/lib/injectable.test.js +49 -204
  35. package/target/lib/injectable.test.js.map +1 -1
  36. package/target/lib/injector.d.ts +1 -1
  37. package/target/lib/injector.js +18 -14
  38. package/target/lib/injector.js.map +1 -1
  39. package/target/lib/injector.test.js +215 -216
  40. package/target/lib/injector.test.js.map +1 -1
  41. package/target/lib/lifecycle.d.ts +2 -4
  42. package/target/lib/lifecycle.js +15 -4
  43. package/target/lib/lifecycle.js.map +1 -1
  44. package/target/lib/lifecycle.test.js +142 -123
  45. package/target/lib/lifecycle.test.js.map +1 -1
  46. package/target/lib/metadata.d.ts +6 -0
  47. package/target/lib/metadata.js +5 -0
  48. package/target/lib/metadata.js.map +1 -0
  49. package/target/lib/provider.d.ts +6 -5
  50. package/target/lib/provider.js +10 -4
  51. package/target/lib/provider.js.map +1 -1
  52. package/target/lib.d.ts +1 -1
  53. package/target/lib.js +1 -1
  54. package/target/lib.js.map +1 -1
  55. package/src/lib/injectable-map.test.ts +0 -18
  56. package/src/lib/injectable-map.ts +0 -3
  57. package/src/lib/injector.test-node.ts +0 -187
  58. package/target/lib/injectable-map.d.ts +0 -3
  59. package/target/lib/injectable-map.js +0 -3
  60. package/target/lib/injectable-map.js.map +0 -1
  61. package/target/lib/injectable-map.test.js +0 -15
  62. package/target/lib/injectable-map.test.js.map +0 -1
  63. package/target/lib/injector.test-node.d.ts +0 -1
  64. package/target/lib/injector.test-node.js +0 -231
  65. package/target/lib/injector.test-node.js.map +0 -1
  66. /package/target/lib/{injectable-map.test.d.ts → injectable-el.test.d.ts} +0 -0
@@ -1,134 +1,153 @@
1
1
  import { __esDecorate, __runInitializers } from "tslib";
2
- import { expect } from '@open-wc/testing';
2
+ import { assert } from 'chai';
3
3
  import { Injector } from './injector.js';
4
- import { LifeCycle } from './lifecycle.js';
5
- import { injectable } from './injectable';
4
+ import { injected, created } from './lifecycle.js';
5
+ import { injectable } from './injectable.js';
6
6
  import { inject } from './inject.js';
7
- describe('LifeCycle', () => {
8
- it('should call onInit and onInject when a service is first created', () => {
9
- const i = new Injector();
10
- const res = {
11
- onInit: 0,
12
- onInject: 0
7
+ it('should call onInit and onInject when a service is first created', () => {
8
+ const i = new Injector();
9
+ let MyService = (() => {
10
+ let _classDecorators = [injectable()];
11
+ let _classDescriptor;
12
+ let _classExtraInitializers = [];
13
+ let _classThis;
14
+ let _instanceExtraInitializers = [];
15
+ let _onCreated_decorators;
16
+ let _onInjected_decorators;
17
+ var MyService = class {
18
+ static { _classThis = this; }
19
+ static {
20
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
21
+ _onCreated_decorators = [created()];
22
+ _onInjected_decorators = [injected()];
23
+ __esDecorate(this, null, _onCreated_decorators, { kind: "method", name: "onCreated", static: false, private: false, access: { has: obj => "onCreated" in obj, get: obj => obj.onCreated }, metadata: _metadata }, null, _instanceExtraInitializers);
24
+ __esDecorate(this, null, _onInjected_decorators, { kind: "method", name: "onInjected", static: false, private: false, access: { has: obj => "onInjected" in obj, get: obj => obj.onInjected }, metadata: _metadata }, null, _instanceExtraInitializers);
25
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
26
+ MyService = _classThis = _classDescriptor.value;
27
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
28
+ __runInitializers(_classThis, _classExtraInitializers);
29
+ }
30
+ res = (__runInitializers(this, _instanceExtraInitializers), {
31
+ onCreated: 0,
32
+ onInjected: 0
33
+ });
34
+ onCreated() {
35
+ this.res.onCreated++;
36
+ }
37
+ onInjected() {
38
+ this.res.onInjected++;
39
+ }
13
40
  };
14
- let MyService = (() => {
15
- let _classDecorators = [injectable];
16
- let _classDescriptor;
17
- let _classExtraInitializers = [];
18
- let _classThis;
19
- var MyService = class {
20
- static { _classThis = this; }
21
- static {
22
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
23
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
24
- MyService = _classThis = _classDescriptor.value;
25
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
26
- __runInitializers(_classThis, _classExtraInitializers);
27
- }
28
- [LifeCycle.onInit]() {
29
- res.onInit++;
30
- }
31
- [LifeCycle.onInject]() {
32
- res.onInject++;
33
- }
34
- };
35
- return MyService = _classThis;
36
- })();
37
- i.inject(MyService);
38
- expect(res).to.deep.equal({
39
- onInit: 1,
40
- onInject: 1
41
- });
41
+ return MyService = _classThis;
42
+ })();
43
+ const service = i.inject(MyService);
44
+ assert.deepEqual(service.res, {
45
+ onCreated: 1,
46
+ onInjected: 1
42
47
  });
43
- it('should call onInject any time a service is returned', () => {
44
- const i = new Injector();
45
- const res = {
46
- onInit: 0,
47
- onInject: 0
48
+ });
49
+ it('should call onInject any time a service is returned', () => {
50
+ const i = new Injector();
51
+ let MyService = (() => {
52
+ let _classDecorators = [injectable()];
53
+ let _classDescriptor;
54
+ let _classExtraInitializers = [];
55
+ let _classThis;
56
+ let _instanceExtraInitializers = [];
57
+ let _onCreated_decorators;
58
+ let _onInjected_decorators;
59
+ var MyService = class {
60
+ static { _classThis = this; }
61
+ static {
62
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
63
+ _onCreated_decorators = [created()];
64
+ _onInjected_decorators = [injected()];
65
+ __esDecorate(this, null, _onCreated_decorators, { kind: "method", name: "onCreated", static: false, private: false, access: { has: obj => "onCreated" in obj, get: obj => obj.onCreated }, metadata: _metadata }, null, _instanceExtraInitializers);
66
+ __esDecorate(this, null, _onInjected_decorators, { kind: "method", name: "onInjected", static: false, private: false, access: { has: obj => "onInjected" in obj, get: obj => obj.onInjected }, metadata: _metadata }, null, _instanceExtraInitializers);
67
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
68
+ MyService = _classThis = _classDescriptor.value;
69
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
70
+ __runInitializers(_classThis, _classExtraInitializers);
71
+ }
72
+ res = (__runInitializers(this, _instanceExtraInitializers), {
73
+ onCreated: 0,
74
+ onInjected: 0
75
+ });
76
+ onCreated() {
77
+ this.res.onCreated++;
78
+ }
79
+ onInjected() {
80
+ this.res.onInjected++;
81
+ }
48
82
  };
49
- let MyService = (() => {
50
- let _classDecorators = [injectable];
51
- let _classDescriptor;
52
- let _classExtraInitializers = [];
53
- let _classThis;
54
- var MyService = class {
55
- static { _classThis = this; }
56
- static {
57
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
58
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
59
- MyService = _classThis = _classDescriptor.value;
60
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
61
- __runInitializers(_classThis, _classExtraInitializers);
62
- }
63
- [LifeCycle.onInit]() {
64
- res.onInit++;
65
- }
66
- [LifeCycle.onInject]() {
67
- res.onInject++;
68
- }
69
- };
70
- return MyService = _classThis;
71
- })();
72
- i.inject(MyService);
73
- i.inject(MyService);
74
- expect(res).to.deep.equal({
75
- onInit: 1,
76
- onInject: 2
77
- });
83
+ return MyService = _classThis;
84
+ })();
85
+ i.inject(MyService);
86
+ const service = i.inject(MyService);
87
+ assert.deepEqual(service.res, {
88
+ onCreated: 1,
89
+ onInjected: 2
78
90
  });
79
- it('should call onInject and on init when injected from another service', () => {
80
- const i = new Injector();
81
- const res = {
82
- onInit: 0,
83
- onInject: 0
91
+ });
92
+ it('should call onInject and on init when injected from another service', () => {
93
+ const i = new Injector();
94
+ let MyService = (() => {
95
+ let _classDecorators = [injectable()];
96
+ let _classDescriptor;
97
+ let _classExtraInitializers = [];
98
+ let _classThis;
99
+ let _instanceExtraInitializers = [];
100
+ let _onCreated_decorators;
101
+ let _onInjected_decorators;
102
+ var MyService = class {
103
+ static { _classThis = this; }
104
+ static {
105
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
106
+ _onCreated_decorators = [created()];
107
+ _onInjected_decorators = [injected()];
108
+ __esDecorate(this, null, _onCreated_decorators, { kind: "method", name: "onCreated", static: false, private: false, access: { has: obj => "onCreated" in obj, get: obj => obj.onCreated }, metadata: _metadata }, null, _instanceExtraInitializers);
109
+ __esDecorate(this, null, _onInjected_decorators, { kind: "method", name: "onInjected", static: false, private: false, access: { has: obj => "onInjected" in obj, get: obj => obj.onInjected }, metadata: _metadata }, null, _instanceExtraInitializers);
110
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
111
+ MyService = _classThis = _classDescriptor.value;
112
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
113
+ __runInitializers(_classThis, _classExtraInitializers);
114
+ }
115
+ res = (__runInitializers(this, _instanceExtraInitializers), {
116
+ onCreated: 0,
117
+ onInjected: 0
118
+ });
119
+ onCreated() {
120
+ this.res.onCreated++;
121
+ }
122
+ onInjected() {
123
+ this.res.onInjected++;
124
+ }
125
+ };
126
+ return MyService = _classThis;
127
+ })();
128
+ let MyApp = (() => {
129
+ let _classDecorators = [injectable()];
130
+ let _classDescriptor;
131
+ let _classExtraInitializers = [];
132
+ let _classThis;
133
+ var MyApp = class {
134
+ static { _classThis = this; }
135
+ static {
136
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
137
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
138
+ MyApp = _classThis = _classDescriptor.value;
139
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
140
+ __runInitializers(_classThis, _classExtraInitializers);
141
+ }
142
+ service = inject(MyService);
84
143
  };
85
- let MyService = (() => {
86
- let _classDecorators = [injectable];
87
- let _classDescriptor;
88
- let _classExtraInitializers = [];
89
- let _classThis;
90
- var MyService = class {
91
- static { _classThis = this; }
92
- static {
93
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
94
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
95
- MyService = _classThis = _classDescriptor.value;
96
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
97
- __runInitializers(_classThis, _classExtraInitializers);
98
- }
99
- [LifeCycle.onInit]() {
100
- res.onInit++;
101
- }
102
- [LifeCycle.onInject]() {
103
- res.onInject++;
104
- }
105
- };
106
- return MyService = _classThis;
107
- })();
108
- let MyApp = (() => {
109
- let _classDecorators = [injectable];
110
- let _classDescriptor;
111
- let _classExtraInitializers = [];
112
- let _classThis;
113
- var MyApp = class {
114
- static { _classThis = this; }
115
- static {
116
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
117
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
118
- MyApp = _classThis = _classDescriptor.value;
119
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
120
- __runInitializers(_classThis, _classExtraInitializers);
121
- }
122
- service = inject(MyService);
123
- };
124
- return MyApp = _classThis;
125
- })();
126
- i.inject(MyApp).service();
127
- i.inject(MyService);
128
- expect(res).to.deep.equal({
129
- onInit: 1,
130
- onInject: 2
131
- });
144
+ return MyApp = _classThis;
145
+ })();
146
+ i.inject(MyApp).service();
147
+ const service = i.inject(MyService);
148
+ assert.deepEqual(service.res, {
149
+ onCreated: 1,
150
+ onInjected: 2
132
151
  });
133
152
  });
134
153
  //# sourceMappingURL=lifecycle.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"lifecycle.test.js","sourceRoot":"","s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AAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC;IAC1B,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAEpC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE;QAC5B,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;KACd,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { ConstructableToken } from './provider.js';
2
+ export interface InjectableMetadata {
3
+ onCreated?: Function[];
4
+ onInjected?: Function[];
5
+ }
6
+ export declare function readMetadata<T>(target: ConstructableToken<T>): InjectableMetadata | null;
@@ -0,0 +1,5 @@
1
+ export function readMetadata(target) {
2
+ const metadata = target[Symbol.metadata];
3
+ return metadata;
4
+ }
5
+ //# sourceMappingURL=metadata.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metadata.js","sourceRoot":"","sources":["../../src/lib/metadata.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,YAAY,CAAI,MAA6B;IAC3D,MAAM,QAAQ,GAA8B,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAEpE,OAAO,QAAQ,CAAC;AAClB,CAAC"}
@@ -1,16 +1,17 @@
1
1
  import { Injector } from './injector.js';
2
+ export type ProviderFactory<T> = (injector: Injector) => T;
2
3
  export declare class StaticToken<T> {
3
- name: string;
4
- factory: (() => T) | undefined;
5
- constructor(name: string, factory?: () => T);
4
+ #private;
5
+ get name(): string;
6
+ get factory(): ProviderFactory<T> | undefined;
7
+ constructor(name: string, factory?: ProviderFactory<T>);
6
8
  }
7
9
  export interface ConstructableToken<T> {
8
- providers?: Provider<any>[];
9
10
  new (...args: any[]): T;
10
11
  }
11
12
  export type InjectionToken<T> = ConstructableToken<T> | StaticToken<T>;
12
13
  export interface Provider<T> {
13
14
  provide: InjectionToken<T>;
14
15
  use?: ConstructableToken<T>;
15
- factory?(injector: Injector): T;
16
+ factory?: ProviderFactory<T>;
16
17
  }
@@ -1,9 +1,15 @@
1
1
  export class StaticToken {
2
- name;
3
- factory;
2
+ #name;
3
+ #factory;
4
+ get name() {
5
+ return this.#name;
6
+ }
7
+ get factory() {
8
+ return this.#factory;
9
+ }
4
10
  constructor(name, factory) {
5
- this.name = name;
6
- this.factory = factory;
11
+ this.#name = name;
12
+ this.#factory = factory;
7
13
  }
8
14
  }
9
15
  //# sourceMappingURL=provider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/lib/provider.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAW;IACtB,IAAI,CAAC;IACL,OAAO,CAAC;IAER,YAAY,IAAY,EAAE,OAAiB;QACzC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF"}
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/lib/provider.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,WAAW;IACtB,KAAK,CAAC;IACN,QAAQ,CAAC;IAET,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,YAAY,IAAY,EAAE,OAA4B;QACpD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;CACF"}
package/target/lib.d.ts CHANGED
@@ -2,5 +2,5 @@ export { Injector } from './lib/injector.js';
2
2
  export { Provider, ConstructableToken, StaticToken } from './lib/provider.js';
3
3
  export { injectable } from './lib/injectable.js';
4
4
  export { inject, Injected } from './lib/inject.js';
5
- export { LifeCycle } from './lib/lifecycle.js';
5
+ export { injected, created } from './lib/lifecycle.js';
6
6
  export { DOMInjector } from './lib/dom-injector.js';
package/target/lib.js CHANGED
@@ -2,6 +2,6 @@ export { Injector } from './lib/injector.js';
2
2
  export { StaticToken } from './lib/provider.js';
3
3
  export { injectable } from './lib/injectable.js';
4
4
  export { inject } from './lib/inject.js';
5
- export { LifeCycle } from './lib/lifecycle.js';
5
+ export { injected, created } from './lib/lifecycle.js';
6
6
  export { DOMInjector } from './lib/dom-injector.js';
7
7
  //# sourceMappingURL=lib.js.map
package/target/lib.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAgC,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAY,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAgC,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAY,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC"}
@@ -1,18 +0,0 @@
1
- import { expect } from '@open-wc/testing';
2
-
3
- import { InjectableMap } from './injectable-map.js';
4
- import { Injector } from './injector.js';
5
-
6
- describe('InjectableMap', () => {
7
- it('should add a new weak reference for an injector', () => {
8
- const map = new InjectableMap();
9
- const app = {
10
- key: {},
11
- injector: new Injector()
12
- };
13
-
14
- map.set(app.key, app.injector);
15
-
16
- expect(map.get(app.key)).to.equal(app.injector);
17
- });
18
- });
@@ -1,3 +0,0 @@
1
- import { Injector } from './injector.js';
2
-
3
- export class InjectableMap extends WeakMap<object, Injector> {}
@@ -1,187 +0,0 @@
1
- import test from 'node:test';
2
- import assert from 'node:assert';
3
-
4
- import { Injector } from './injector.js';
5
- import { inject } from './inject.js';
6
- import { injectable } from './injectable.js';
7
- import { Provider, StaticToken } from './provider.js';
8
-
9
- test('should create a new instance of a single provider', () => {
10
- class A {}
11
-
12
- const app = new Injector();
13
-
14
- assert(app.inject(A) instanceof A);
15
-
16
- assert.equal(app.inject(A), app.inject(A));
17
- });
18
-
19
- test('should inject providers in the correct order', () => {
20
- class A {}
21
- class B {}
22
-
23
- @injectable
24
- class MyService {
25
- a = inject(A);
26
- b = inject(B);
27
- }
28
-
29
- const app = new Injector();
30
- const instance = app.inject(MyService);
31
-
32
- assert(instance.a() instanceof A);
33
- assert(instance.b() instanceof B);
34
- });
35
-
36
- test('should create a new instance of a provider that has a full dep tree', () => {
37
- class A {}
38
-
39
- @injectable
40
- class B {
41
- a = inject(A);
42
- }
43
-
44
- @injectable
45
- class C {
46
- b = inject(B);
47
- }
48
-
49
- @injectable
50
- class D {
51
- c = inject(C);
52
- }
53
-
54
- @injectable
55
- class E {
56
- d = inject(D);
57
- }
58
-
59
- const app = new Injector();
60
- const instance = app.inject(E);
61
-
62
- assert(instance.d().c().b().a() instanceof A);
63
- });
64
-
65
- test('should override a provider if explicitly instructed', () => {
66
- class A {}
67
-
68
- @injectable
69
- class B {
70
- a = inject(A);
71
- }
72
-
73
- class AltA extends A {}
74
- const app = new Injector([{ provide: A, use: AltA }]);
75
-
76
- assert(app.inject(B).a() instanceof AltA);
77
- });
78
-
79
- test('should return an existing instance from a parent injector', () => {
80
- class A {}
81
-
82
- const parent = new Injector();
83
-
84
- const app = new Injector([], parent);
85
-
86
- assert.equal(parent.inject(A), app.inject(A));
87
- });
88
-
89
- test('should use a factory if provided', () => {
90
- class Service {
91
- hello() {
92
- return 'world';
93
- }
94
- }
95
-
96
- const injector = new Injector([
97
- {
98
- provide: Service,
99
- factory() {
100
- return {
101
- hello() {
102
- return 'world';
103
- }
104
- };
105
- }
106
- }
107
- ]);
108
-
109
- assert.equal(injector.inject(Service).hello(), 'world');
110
- });
111
-
112
- test('should throw an error if provider is missing both factory and use', () => {
113
- class Service {
114
- hello() {
115
- return 'world';
116
- }
117
- }
118
-
119
- const injector = new Injector([
120
- {
121
- provide: Service
122
- }
123
- ]);
124
-
125
- assert.throws(
126
- () => injector.inject(Service),
127
- new Error("Provider for Service found but is missing either 'use' or 'factory'")
128
- );
129
- });
130
-
131
- test('should pass factories and instance of the injector', async () => {
132
- class Service {
133
- hello() {
134
- return 'world';
135
- }
136
- }
137
-
138
- let factoryInjector: Injector | null = null;
139
-
140
- const injector = new Injector([
141
- {
142
- provide: Service,
143
- factory(i) {
144
- factoryInjector = i;
145
- }
146
- }
147
- ]);
148
-
149
- injector.inject(Service);
150
-
151
- assert.equal(factoryInjector, injector);
152
- });
153
-
154
- test('should create an instance from a StaticToken factory', () => {
155
- const TOKEN = new StaticToken('test', () => 'Hello World');
156
- const injector = new Injector();
157
-
158
- const res = injector.inject(TOKEN);
159
-
160
- assert.equal(res, 'Hello World');
161
- });
162
-
163
- test('should create an instance from an async StaticToken factory', async () => {
164
- const TOKEN = new StaticToken('test', () => Promise.resolve('Hello World'));
165
- const injector = new Injector();
166
-
167
- const res = await injector.inject(TOKEN);
168
-
169
- assert.equal(res, 'Hello World');
170
- });
171
-
172
- test('should allow static token to be overridden', () => {
173
- const TOKEN = new StaticToken<string>('test');
174
-
175
- const provider: Provider<string> = {
176
- provide: TOKEN,
177
- factory() {
178
- return 'Hello World';
179
- }
180
- };
181
-
182
- const injector = new Injector([provider]);
183
-
184
- const res = injector.inject(TOKEN);
185
-
186
- assert.equal(res, 'Hello World');
187
- });
@@ -1,3 +0,0 @@
1
- import { Injector } from './injector.js';
2
- export declare class InjectableMap extends WeakMap<object, Injector> {
3
- }
@@ -1,3 +0,0 @@
1
- export class InjectableMap extends WeakMap {
2
- }
3
- //# sourceMappingURL=injectable-map.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"injectable-map.js","sourceRoot":"","sources":["../../src/lib/injectable-map.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,aAAc,SAAQ,OAAyB;CAAG"}
@@ -1,15 +0,0 @@
1
- import { expect } from '@open-wc/testing';
2
- import { InjectableMap } from './injectable-map.js';
3
- import { Injector } from './injector.js';
4
- describe('InjectableMap', () => {
5
- it('should add a new weak reference for an injector', () => {
6
- const map = new InjectableMap();
7
- const app = {
8
- key: {},
9
- injector: new Injector()
10
- };
11
- map.set(app.key, app.injector);
12
- expect(map.get(app.key)).to.equal(app.injector);
13
- });
14
- });
15
- //# sourceMappingURL=injectable-map.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"injectable-map.test.js","sourceRoot":"","sources":["../../src/lib/injectable-map.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,EAAE,CAAC,iDAAiD,EAAE,GAAG,EAAE;QACzD,MAAM,GAAG,GAAG,IAAI,aAAa,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG;YACV,GAAG,EAAE,EAAE;YACP,QAAQ,EAAE,IAAI,QAAQ,EAAE;SACzB,CAAC;QAEF,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;QAE/B,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- export {};