@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,96 +1,100 @@
1
- import { expect } from '@open-wc/testing';
1
+ import { assert } from 'chai';
2
2
 
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
7
 
8
- describe('LifeCycle', () => {
9
- it('should call onInit and onInject when a service is first created', () => {
10
- const i = new Injector();
8
+ it('should call onInit and onInject when a service is first created', () => {
9
+ const i = new Injector();
11
10
 
12
- const res = {
13
- onInit: 0,
14
- onInject: 0
11
+ @injectable()
12
+ class MyService {
13
+ res = {
14
+ onCreated: 0,
15
+ onInjected: 0
15
16
  };
16
17
 
17
- @injectable
18
- class MyService {
19
- [LifeCycle.onInit]() {
20
- res.onInit++;
21
- }
18
+ @created()
19
+ onCreated() {
20
+ this.res.onCreated++;
21
+ }
22
22
 
23
- [LifeCycle.onInject]() {
24
- res.onInject++;
25
- }
23
+ @injected()
24
+ onInjected() {
25
+ this.res.onInjected++;
26
26
  }
27
+ }
27
28
 
28
- i.inject(MyService);
29
+ const service = i.inject(MyService);
29
30
 
30
- expect(res).to.deep.equal({
31
- onInit: 1,
32
- onInject: 1
33
- });
31
+ assert.deepEqual(service.res, {
32
+ onCreated: 1,
33
+ onInjected: 1
34
34
  });
35
+ });
35
36
 
36
- it('should call onInject any time a service is returned', () => {
37
- const i = new Injector();
37
+ it('should call onInject any time a service is returned', () => {
38
+ const i = new Injector();
38
39
 
39
- const res = {
40
- onInit: 0,
41
- onInject: 0
40
+ @injectable()
41
+ class MyService {
42
+ res = {
43
+ onCreated: 0,
44
+ onInjected: 0
42
45
  };
43
46
 
44
- @injectable
45
- class MyService {
46
- [LifeCycle.onInit]() {
47
- res.onInit++;
48
- }
47
+ @created()
48
+ onCreated() {
49
+ this.res.onCreated++;
50
+ }
49
51
 
50
- [LifeCycle.onInject]() {
51
- res.onInject++;
52
- }
52
+ @injected()
53
+ onInjected() {
54
+ this.res.onInjected++;
53
55
  }
56
+ }
54
57
 
55
- i.inject(MyService);
56
- i.inject(MyService);
58
+ i.inject(MyService);
59
+ const service = i.inject(MyService);
57
60
 
58
- expect(res).to.deep.equal({
59
- onInit: 1,
60
- onInject: 2
61
- });
61
+ assert.deepEqual(service.res, {
62
+ onCreated: 1,
63
+ onInjected: 2
62
64
  });
65
+ });
63
66
 
64
- it('should call onInject and on init when injected from another service', () => {
65
- const i = new Injector();
67
+ it('should call onInject and on init when injected from another service', () => {
68
+ const i = new Injector();
66
69
 
67
- const res = {
68
- onInit: 0,
69
- onInject: 0
70
+ @injectable()
71
+ class MyService {
72
+ res = {
73
+ onCreated: 0,
74
+ onInjected: 0
70
75
  };
71
76
 
72
- @injectable
73
- class MyService {
74
- [LifeCycle.onInit]() {
75
- res.onInit++;
76
- }
77
-
78
- [LifeCycle.onInject]() {
79
- res.onInject++;
80
- }
77
+ @created()
78
+ onCreated() {
79
+ this.res.onCreated++;
81
80
  }
82
81
 
83
- @injectable
84
- class MyApp {
85
- service = inject(MyService);
82
+ @injected()
83
+ onInjected() {
84
+ this.res.onInjected++;
86
85
  }
86
+ }
87
+
88
+ @injectable()
89
+ class MyApp {
90
+ service = inject(MyService);
91
+ }
87
92
 
88
- i.inject(MyApp).service();
89
- i.inject(MyService);
93
+ i.inject(MyApp).service();
94
+ const service = i.inject(MyService);
90
95
 
91
- expect(res).to.deep.equal({
92
- onInit: 1,
93
- onInject: 2
94
- });
96
+ assert.deepEqual(service.res, {
97
+ onCreated: 1,
98
+ onInjected: 2
95
99
  });
96
100
  });
@@ -1,4 +1,19 @@
1
- export const LifeCycle = {
2
- onInit: Symbol('OnInit'),
3
- onInject: Symbol('OnInject')
4
- } as const;
1
+ import { InjectableMetadata } from './metadata';
2
+
3
+ (Symbol as any).metadata ??= Symbol('Symbol.metadata');
4
+
5
+ export function injected() {
6
+ return function onInjectDecorator(val: Function, ctx: ClassMethodDecoratorContext) {
7
+ const metadata: InjectableMetadata = ctx.metadata;
8
+ metadata.onInjected ??= [];
9
+ metadata.onInjected.push(val);
10
+ };
11
+ }
12
+
13
+ export function created() {
14
+ return function onInjectDecorator(val: Function, ctx: ClassMethodDecoratorContext) {
15
+ const metadata: InjectableMetadata = ctx.metadata;
16
+ metadata.onCreated ??= [];
17
+ metadata.onCreated.push(val);
18
+ };
19
+ }
@@ -0,0 +1,12 @@
1
+ import { ConstructableToken } from './provider.js';
2
+
3
+ export interface InjectableMetadata {
4
+ onCreated?: Function[];
5
+ onInjected?: Function[];
6
+ }
7
+
8
+ export function readMetadata<T>(target: ConstructableToken<T>) {
9
+ const metadata: InjectableMetadata | null = target[Symbol.metadata];
10
+
11
+ return metadata;
12
+ }
@@ -1,18 +1,26 @@
1
1
  import { Injector } from './injector.js';
2
2
 
3
+ export type ProviderFactory<T> = (injector: Injector) => T;
4
+
3
5
  export class StaticToken<T> {
4
- name;
5
- factory;
6
+ #name;
7
+ #factory;
8
+
9
+ get name() {
10
+ return this.#name;
11
+ }
6
12
 
7
- constructor(name: string, factory?: () => T) {
8
- this.name = name;
9
- this.factory = factory;
13
+ get factory() {
14
+ return this.#factory;
15
+ }
16
+
17
+ constructor(name: string, factory?: ProviderFactory<T>) {
18
+ this.#name = name;
19
+ this.#factory = factory;
10
20
  }
11
21
  }
12
22
 
13
23
  export interface ConstructableToken<T> {
14
- providers?: Provider<any>[];
15
-
16
24
  new (...args: any[]): T;
17
25
  }
18
26
 
@@ -21,5 +29,5 @@ export type InjectionToken<T> = ConstructableToken<T> | StaticToken<T>;
21
29
  export interface Provider<T> {
22
30
  provide: InjectionToken<T>;
23
31
  use?: ConstructableToken<T>;
24
- factory?(injector: Injector): T;
32
+ factory?: ProviderFactory<T>;
25
33
  }
package/src/lib.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';
@@ -1,11 +1,10 @@
1
- import { INJECTABLE_MAP } from './injectable.js';
2
- import { Injector } from './injector.js';
1
+ import { injectables, Injector } from './injector.js';
3
2
  export class DOMInjector extends Injector {
4
3
  attach(root) {
5
- INJECTABLE_MAP.set(root, this);
4
+ injectables.set(root, this);
6
5
  }
7
6
  detach(root) {
8
- INJECTABLE_MAP.delete(root);
7
+ injectables.delete(root);
9
8
  }
10
9
  }
11
10
  //# sourceMappingURL=dom-injector.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dom-injector.js","sourceRoot":"","sources":["../../src/lib/dom-injector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,MAAM,OAAO,WAAY,SAAQ,QAAQ;IACvC,MAAM,CAAC,IAAiB;QACtB,cAAc,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,MAAM,CAAC,IAAiB;QACtB,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;CACF"}
1
+ {"version":3,"file":"dom-injector.js","sourceRoot":"","sources":["../../src/lib/dom-injector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEtD,MAAM,OAAO,WAAY,SAAQ,QAAQ;IACvC,MAAM,CAAC,IAAiB;QACtB,WAAW,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED,MAAM,CAAC,IAAiB;QACtB,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;CACF"}
@@ -1,21 +1,19 @@
1
- import { expect } from '@open-wc/testing';
1
+ import { assert } from 'chai';
2
2
  import { DOMInjector } from './dom-injector.js';
3
- import { INJECTABLE_MAP } from './injectable.js';
4
- describe('DOMInjector', () => {
5
- it('should attach an injector to a dom element', () => {
6
- const root = document.createElement('div');
7
- const app = new DOMInjector();
8
- app.attach(root);
9
- const injector = INJECTABLE_MAP.get(root);
10
- expect(injector).to.equal(app);
11
- });
12
- it('should remove an injector associated with a dom element', () => {
13
- const root = document.createElement('div');
14
- const app = new DOMInjector();
15
- app.attach(root);
16
- expect(INJECTABLE_MAP.get(root)).to.equal(app);
17
- app.detach(root);
18
- expect(INJECTABLE_MAP.get(root)).to.equal(undefined);
19
- });
3
+ import { injectables } from './injector.js';
4
+ it('should attach an injector to a dom element', () => {
5
+ const root = document.createElement('div');
6
+ const app = new DOMInjector();
7
+ app.attach(root);
8
+ const injector = injectables.get(root);
9
+ assert.strictEqual(injector, app);
10
+ });
11
+ it('should remove an injector associated with a dom element', () => {
12
+ const root = document.createElement('div');
13
+ const app = new DOMInjector();
14
+ app.attach(root);
15
+ assert.strictEqual(injectables.get(root), app);
16
+ app.detach(root);
17
+ assert.strictEqual(injectables.get(root), undefined);
20
18
  });
21
19
  //# sourceMappingURL=dom-injector.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dom-injector.test.js","sourceRoot":"","sources":["../../src/lib/dom-injector.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QACpD,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QAC3C,MAAM,GAAG,GAAG,IAAI,WAAW,EAAE,CAAC;QAE9B,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEjB,MAAM,QAAQ,GAAG,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAE1C,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,yDAAyD,EAAE,GAAG,EAAE;QACjE,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QAC3C,MAAM,GAAG,GAAG,IAAI,WAAW,EAAE,CAAC;QAE9B,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEjB,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CA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GAAG,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,CAAC;AACvD,CAAC,CAAC,CAAC"}
@@ -1,10 +1,10 @@
1
- import { INJECTABLE_MAP } from './injectable.js';
1
+ import { injectables } from './injector.js';
2
2
  export function inject(token) {
3
3
  return function () {
4
- const injector = INJECTABLE_MAP.get(this);
4
+ const injector = injectables.get(this);
5
5
  if (injector === undefined) {
6
6
  const name = Object.getPrototypeOf(this.constructor).name;
7
- throw new Error(`${name} is either not injectable or a service is being called in the constructor. \n Either add the @injectable to your class or use the [LifeCycle.onInject] callback method.`);
7
+ throw new Error(`${name} is either not injectable or a service is being called in the constructor. \n Either add the @injectable() to your class or use the [LifeCycle.onInject] callback method.`);
8
8
  }
9
9
  return injector.inject(token);
10
10
  };
@@ -1 +1 @@
1
- {"version":3,"file":"inject.js","sourceRoot":"","sources":["../../src/lib/inject.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,UAAU,MAAM,CACpB,KAAwB;IAExB,OAAO;QACL,MAAM,QAAQ,GAAG,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAE1C,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,IAAI,GAAG,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC;YAE1D,MAAM,IAAI,KAAK,CACb,GAAG,IAAI,yKAAyK,CACjL,CAAC;QACJ,CAAC;QAED,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"inject.js","sourceRoot":"","sources":["../../src/lib/inject.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAI5C,MAAM,UAAU,MAAM,CAAyB,KAAwB;IACrE,OAAO;QACL,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAEvC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,IAAI,GAAG,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC;YAE1D,MAAM,IAAI,KAAK,CACb,GAAG,IAAI,2KAA2K,CACnL,CAAC;QACJ,CAAC;QAED,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC"}
@@ -1,41 +1,16 @@
1
1
  import { __esDecorate, __runInitializers } from "tslib";
2
- import { expect } from '@open-wc/testing';
2
+ import { assert } from 'chai';
3
3
  import { inject } from './inject.js';
4
4
  import { injectable } from './injectable.js';
5
5
  import { Injector } from './injector.js';
6
6
  import { StaticToken } from './provider.js';
7
- describe('inject', () => {
8
- it('should work', () => {
9
- class HelloService {
10
- }
11
- let HelloWorld = (() => {
12
- let _classDecorators = [injectable];
13
- let _classDescriptor;
14
- let _classExtraInitializers = [];
15
- let _classThis;
16
- let _classSuper = HTMLElement;
17
- var HelloWorld = class extends _classSuper {
18
- static { _classThis = this; }
19
- static {
20
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
21
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
22
- HelloWorld = _classThis = _classDescriptor.value;
23
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
24
- __runInitializers(_classThis, _classExtraInitializers);
25
- }
26
- hello = inject(HelloService);
27
- };
28
- return HelloWorld = _classThis;
29
- })();
30
- customElements.define('inject-1', HelloWorld);
31
- expect(new HelloWorld().hello()).to.be.instanceOf(HelloService);
32
- });
33
- it('should throw error if called in constructor', () => {
7
+ it('should throw error if called in constructor', () => {
8
+ assert.throws(() => {
34
9
  class FooService {
35
10
  value = '1';
36
11
  }
37
12
  let BarService = (() => {
38
- let _classDecorators = [injectable];
13
+ let _classDecorators = [injectable()];
39
14
  let _classDescriptor;
40
15
  let _classExtraInitializers = [];
41
16
  let _classThis;
@@ -56,68 +31,61 @@ describe('inject', () => {
56
31
  return BarService = _classThis;
57
32
  })();
58
33
  const parent = new Injector();
59
- try {
60
- parent.inject(BarService);
61
- throw new Error('Should not succeed');
62
- }
63
- catch (err) {
64
- const error = err;
65
- expect(error.message).to.equal(`BarService is either not injectable or a service is being called in the constructor. \n Either add the @injectable to your class or use the [LifeCycle.onInject] callback method.`);
66
- }
67
- });
68
- it('should use the calling injector as parent', () => {
69
- class FooService {
70
- value = '1';
34
+ parent.inject(BarService);
35
+ }, 'BarService is either not injectable or a service is being called in the constructor.');
36
+ });
37
+ it('should use the calling injector as parent', () => {
38
+ class FooService {
39
+ value = '1';
40
+ }
41
+ let BarService = (() => {
42
+ let _classDecorators = [injectable()];
43
+ let _classDescriptor;
44
+ let _classExtraInitializers = [];
45
+ let _classThis;
46
+ var BarService = class {
47
+ static { _classThis = this; }
48
+ static {
49
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
50
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
51
+ BarService = _classThis = _classDescriptor.value;
52
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
53
+ __runInitializers(_classThis, _classExtraInitializers);
54
+ }
55
+ foo = inject(FooService);
56
+ };
57
+ return BarService = _classThis;
58
+ })();
59
+ const parent = new Injector([
60
+ {
61
+ provide: FooService,
62
+ use: class extends FooService {
63
+ value = '100';
64
+ }
71
65
  }
72
- let BarService = (() => {
73
- let _classDecorators = [injectable];
74
- let _classDescriptor;
75
- let _classExtraInitializers = [];
76
- let _classThis;
77
- var BarService = class {
78
- static { _classThis = this; }
79
- static {
80
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
81
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
82
- BarService = _classThis = _classDescriptor.value;
83
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
84
- __runInitializers(_classThis, _classExtraInitializers);
85
- }
86
- foo = inject(FooService);
87
- };
88
- return BarService = _classThis;
89
- })();
90
- const parent = new Injector([
91
- {
92
- provide: FooService,
93
- use: class extends FooService {
94
- value = '100';
95
- }
66
+ ]);
67
+ assert.strictEqual(parent.inject(BarService).foo().value, '100');
68
+ });
69
+ it('should inject a static token', () => {
70
+ const TOKEN = new StaticToken('test', () => 'Hello World');
71
+ let HelloWorld = (() => {
72
+ let _classDecorators = [injectable()];
73
+ let _classDescriptor;
74
+ let _classExtraInitializers = [];
75
+ let _classThis;
76
+ var HelloWorld = class {
77
+ static { _classThis = this; }
78
+ static {
79
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
80
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
81
+ HelloWorld = _classThis = _classDescriptor.value;
82
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
83
+ __runInitializers(_classThis, _classExtraInitializers);
96
84
  }
97
- ]);
98
- expect(parent.inject(BarService).foo().value).to.equal('100');
99
- });
100
- it('should inject a static token', () => {
101
- const TOKEN = new StaticToken('test', () => 'Hello World');
102
- let HelloWorld = (() => {
103
- let _classDecorators = [injectable];
104
- let _classDescriptor;
105
- let _classExtraInitializers = [];
106
- let _classThis;
107
- var HelloWorld = class {
108
- static { _classThis = this; }
109
- static {
110
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
111
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
112
- HelloWorld = _classThis = _classDescriptor.value;
113
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
114
- __runInitializers(_classThis, _classExtraInitializers);
115
- }
116
- hello = inject(TOKEN);
117
- };
118
- return HelloWorld = _classThis;
119
- })();
120
- expect(new HelloWorld().hello()).to.equal('Hello World');
121
- });
85
+ hello = inject(TOKEN);
86
+ };
87
+ return HelloWorld = _classThis;
88
+ })();
89
+ assert.strictEqual(new HelloWorld().hello(), 'Hello World');
122
90
  });
123
91
  //# sourceMappingURL=inject.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"inject.test.js","sourceRoot":"","sources":["../../src/lib/inject.test.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,EAAE,CAAC,aAAa,EAAE,GAAG,EAAE;QACrB,MAAM,YAAY;SAAG;YAGf,UAAU;oCADf,UAAU;;;;8BACc,WAAW;kCAAnB,SAAQ,WAAW;;;;oBAApC,6KAEC;;;oBAFK,uDAAU;;gBACd,KAAK,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;;;;QAG/B,cAAc,CAAC,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;QAE9C,MAAM,CAAC,IAAI,UAAU,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;IAClE,CA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