@navios/di 0.8.0 → 0.9.0
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/CHANGELOG.md +87 -0
- package/README.md +117 -17
- package/lib/browser/container/abstract-container.d.mts +112 -0
- package/lib/browser/container/abstract-container.d.mts.map +1 -0
- package/lib/browser/container/abstract-container.mjs +100 -0
- package/lib/browser/container/abstract-container.mjs.map +1 -0
- package/lib/browser/container/container.d.mts +100 -0
- package/lib/browser/container/container.d.mts.map +1 -0
- package/lib/browser/container/container.mjs +424 -0
- package/lib/browser/container/container.mjs.map +1 -0
- package/lib/browser/container/scoped-container.d.mts +93 -0
- package/lib/browser/container/scoped-container.d.mts.map +1 -0
- package/lib/browser/container/scoped-container.mjs +119 -0
- package/lib/browser/container/scoped-container.mjs.map +1 -0
- package/lib/browser/decorators/factory.decorator.d.mts +26 -0
- package/lib/browser/decorators/factory.decorator.d.mts.map +1 -0
- package/lib/browser/decorators/factory.decorator.mjs +20 -0
- package/lib/browser/decorators/factory.decorator.mjs.map +1 -0
- package/lib/browser/decorators/injectable.decorator.d.mts +38 -0
- package/lib/browser/decorators/injectable.decorator.d.mts.map +1 -0
- package/lib/browser/decorators/injectable.decorator.mjs +21 -0
- package/lib/browser/decorators/injectable.decorator.mjs.map +1 -0
- package/lib/browser/enums/injectable-scope.enum.d.mts +18 -0
- package/lib/browser/enums/injectable-scope.enum.d.mts.map +1 -0
- package/lib/browser/enums/injectable-scope.enum.mjs +20 -0
- package/lib/browser/enums/injectable-scope.enum.mjs.map +1 -0
- package/lib/browser/enums/injectable-type.enum.d.mts +8 -0
- package/lib/browser/enums/injectable-type.enum.d.mts.map +1 -0
- package/lib/browser/enums/injectable-type.enum.mjs +10 -0
- package/lib/browser/enums/injectable-type.enum.mjs.map +1 -0
- package/lib/browser/errors/di-error.d.mts +43 -0
- package/lib/browser/errors/di-error.d.mts.map +1 -0
- package/lib/browser/errors/di-error.mjs +98 -0
- package/lib/browser/errors/di-error.mjs.map +1 -0
- package/lib/browser/event-emitter.d.mts +16 -0
- package/lib/browser/event-emitter.d.mts.map +1 -0
- package/lib/browser/event-emitter.mjs +320 -0
- package/lib/browser/event-emitter.mjs.map +1 -0
- package/lib/browser/index.d.mts +37 -1558
- package/lib/browser/index.mjs +29 -2749
- package/lib/browser/interfaces/container.interface.d.mts +59 -0
- package/lib/browser/interfaces/container.interface.d.mts.map +1 -0
- package/lib/browser/interfaces/factory.interface.d.mts +14 -0
- package/lib/browser/interfaces/factory.interface.d.mts.map +1 -0
- package/lib/browser/interfaces/on-service-destroy.interface.d.mts +7 -0
- package/lib/browser/interfaces/on-service-destroy.interface.d.mts.map +1 -0
- package/lib/browser/interfaces/on-service-init.interface.d.mts +7 -0
- package/lib/browser/interfaces/on-service-init.interface.d.mts.map +1 -0
- package/lib/browser/internal/context/async-local-storage.browser.mjs +20 -0
- package/lib/browser/internal/context/async-local-storage.browser.mjs.map +1 -0
- package/lib/browser/internal/context/async-local-storage.d.mts +9 -0
- package/lib/browser/internal/context/async-local-storage.d.mts.map +1 -0
- package/lib/browser/internal/context/async-local-storage.types.d.mts +11 -0
- package/lib/browser/internal/context/async-local-storage.types.d.mts.map +1 -0
- package/lib/browser/internal/context/factory-context.d.mts +23 -0
- package/lib/browser/internal/context/factory-context.d.mts.map +1 -0
- package/lib/browser/internal/context/resolution-context.d.mts +43 -0
- package/lib/browser/internal/context/resolution-context.d.mts.map +1 -0
- package/lib/browser/internal/context/resolution-context.mjs +56 -0
- package/lib/browser/internal/context/resolution-context.mjs.map +1 -0
- package/lib/browser/internal/context/service-initialization-context.d.mts +48 -0
- package/lib/browser/internal/context/service-initialization-context.d.mts.map +1 -0
- package/lib/browser/internal/context/sync-local-storage.mjs +53 -0
- package/lib/browser/internal/context/sync-local-storage.mjs.map +1 -0
- package/lib/browser/internal/core/instance-resolver.d.mts +119 -0
- package/lib/browser/internal/core/instance-resolver.d.mts.map +1 -0
- package/lib/browser/internal/core/instance-resolver.mjs +306 -0
- package/lib/browser/internal/core/instance-resolver.mjs.map +1 -0
- package/lib/browser/internal/core/name-resolver.d.mts +52 -0
- package/lib/browser/internal/core/name-resolver.d.mts.map +1 -0
- package/lib/browser/internal/core/name-resolver.mjs +118 -0
- package/lib/browser/internal/core/name-resolver.mjs.map +1 -0
- package/lib/browser/internal/core/scope-tracker.d.mts +65 -0
- package/lib/browser/internal/core/scope-tracker.d.mts.map +1 -0
- package/lib/browser/internal/core/scope-tracker.mjs +120 -0
- package/lib/browser/internal/core/scope-tracker.mjs.map +1 -0
- package/lib/browser/internal/core/service-initializer.d.mts +44 -0
- package/lib/browser/internal/core/service-initializer.d.mts.map +1 -0
- package/lib/browser/internal/core/service-initializer.mjs +109 -0
- package/lib/browser/internal/core/service-initializer.mjs.map +1 -0
- package/lib/browser/internal/core/service-invalidator.d.mts +81 -0
- package/lib/browser/internal/core/service-invalidator.d.mts.map +1 -0
- package/lib/browser/internal/core/service-invalidator.mjs +142 -0
- package/lib/browser/internal/core/service-invalidator.mjs.map +1 -0
- package/lib/browser/internal/core/token-resolver.d.mts +54 -0
- package/lib/browser/internal/core/token-resolver.d.mts.map +1 -0
- package/lib/browser/internal/core/token-resolver.mjs +77 -0
- package/lib/browser/internal/core/token-resolver.mjs.map +1 -0
- package/lib/browser/internal/holder/holder-storage.interface.d.mts +99 -0
- package/lib/browser/internal/holder/holder-storage.interface.d.mts.map +1 -0
- package/lib/browser/internal/holder/instance-holder.d.mts +101 -0
- package/lib/browser/internal/holder/instance-holder.d.mts.map +1 -0
- package/lib/browser/internal/holder/instance-holder.mjs +19 -0
- package/lib/browser/internal/holder/instance-holder.mjs.map +1 -0
- package/lib/browser/internal/holder/unified-storage.d.mts +53 -0
- package/lib/browser/internal/holder/unified-storage.d.mts.map +1 -0
- package/lib/browser/internal/holder/unified-storage.mjs +144 -0
- package/lib/browser/internal/holder/unified-storage.mjs.map +1 -0
- package/lib/browser/internal/lifecycle/circular-detector.d.mts +39 -0
- package/lib/browser/internal/lifecycle/circular-detector.d.mts.map +1 -0
- package/lib/browser/internal/lifecycle/circular-detector.mjs +55 -0
- package/lib/browser/internal/lifecycle/circular-detector.mjs.map +1 -0
- package/lib/browser/internal/lifecycle/lifecycle-event-bus.d.mts +18 -0
- package/lib/browser/internal/lifecycle/lifecycle-event-bus.d.mts.map +1 -0
- package/lib/browser/internal/lifecycle/lifecycle-event-bus.mjs +43 -0
- package/lib/browser/internal/lifecycle/lifecycle-event-bus.mjs.map +1 -0
- package/lib/browser/internal/stub-factory-class.d.mts +14 -0
- package/lib/browser/internal/stub-factory-class.d.mts.map +1 -0
- package/lib/browser/internal/stub-factory-class.mjs +18 -0
- package/lib/browser/internal/stub-factory-class.mjs.map +1 -0
- package/lib/browser/symbols/injectable-token.d.mts +5 -0
- package/lib/browser/symbols/injectable-token.d.mts.map +1 -0
- package/lib/browser/symbols/injectable-token.mjs +6 -0
- package/lib/browser/symbols/injectable-token.mjs.map +1 -0
- package/lib/browser/token/injection-token.d.mts +55 -0
- package/lib/browser/token/injection-token.d.mts.map +1 -0
- package/lib/browser/token/injection-token.mjs +100 -0
- package/lib/browser/token/injection-token.mjs.map +1 -0
- package/lib/browser/token/registry.d.mts +37 -0
- package/lib/browser/token/registry.d.mts.map +1 -0
- package/lib/browser/token/registry.mjs +86 -0
- package/lib/browser/token/registry.mjs.map +1 -0
- package/lib/browser/utils/default-injectors.d.mts +12 -0
- package/lib/browser/utils/default-injectors.d.mts.map +1 -0
- package/lib/browser/utils/default-injectors.mjs +13 -0
- package/lib/browser/utils/default-injectors.mjs.map +1 -0
- package/lib/browser/utils/get-injectable-token.d.mts +9 -0
- package/lib/browser/utils/get-injectable-token.d.mts.map +1 -0
- package/lib/browser/utils/get-injectable-token.mjs +13 -0
- package/lib/browser/utils/get-injectable-token.mjs.map +1 -0
- package/lib/browser/utils/get-injectors.d.mts +55 -0
- package/lib/browser/utils/get-injectors.d.mts.map +1 -0
- package/lib/browser/utils/get-injectors.mjs +121 -0
- package/lib/browser/utils/get-injectors.mjs.map +1 -0
- package/lib/browser/utils/types.d.mts +23 -0
- package/lib/browser/utils/types.d.mts.map +1 -0
- package/lib/{container-DAKOvAgr.mjs → container-8-z89TyQ.mjs} +1325 -1462
- package/lib/container-8-z89TyQ.mjs.map +1 -0
- package/lib/{container-Bp1W-pWJ.d.mts → container-CNiqesCL.d.mts} +598 -617
- package/lib/container-CNiqesCL.d.mts.map +1 -0
- package/lib/{container-DENMeJ87.cjs → container-CaY2fDuk.cjs} +1369 -1512
- package/lib/container-CaY2fDuk.cjs.map +1 -0
- package/lib/{container-YPwvmlK2.d.cts → container-D-0Ho3qL.d.cts} +598 -612
- package/lib/container-D-0Ho3qL.d.cts.map +1 -0
- package/lib/index.cjs +13 -15
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +58 -223
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +62 -222
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +5 -6
- package/lib/index.mjs.map +1 -1
- package/lib/testing/index.cjs +569 -311
- package/lib/testing/index.cjs.map +1 -1
- package/lib/testing/index.d.cts +370 -41
- package/lib/testing/index.d.cts.map +1 -1
- package/lib/testing/index.d.mts +370 -41
- package/lib/testing/index.d.mts.map +1 -1
- package/lib/testing/index.mjs +568 -305
- package/lib/testing/index.mjs.map +1 -1
- package/package.json +2 -1
- package/src/__tests__/circular-detector.spec.mts +193 -0
- package/src/__tests__/concurrent.spec.mts +368 -0
- package/src/__tests__/container.spec.mts +32 -30
- package/src/__tests__/di-error.spec.mts +351 -0
- package/src/__tests__/e2e.browser.spec.mts +0 -4
- package/src/__tests__/e2e.spec.mts +10 -19
- package/src/__tests__/event-emitter.spec.mts +232 -109
- package/src/__tests__/get-injectors.spec.mts +250 -39
- package/src/__tests__/injection-token.spec.mts +293 -349
- package/src/__tests__/library-findings.spec.mts +8 -8
- package/src/__tests__/registry.spec.mts +358 -210
- package/src/__tests__/resolution-context.spec.mts +255 -0
- package/src/__tests__/scope-tracker.spec.mts +598 -0
- package/src/__tests__/scope-upgrade.spec.mts +808 -0
- package/src/__tests__/scoped-container.spec.mts +595 -0
- package/src/__tests__/test-container.spec.mts +293 -0
- package/src/__tests__/token-resolver.spec.mts +207 -0
- package/src/__tests__/unified-storage.spec.mts +535 -0
- package/src/__tests__/unit-test-container.spec.mts +405 -0
- package/src/__type-tests__/container.spec-d.mts +180 -0
- package/src/__type-tests__/factory.spec-d.mts +15 -3
- package/src/__type-tests__/inject.spec-d.mts +115 -20
- package/src/__type-tests__/injectable.spec-d.mts +69 -52
- package/src/__type-tests__/injection-token.spec-d.mts +176 -0
- package/src/__type-tests__/scoped-container.spec-d.mts +212 -0
- package/src/container/abstract-container.mts +327 -0
- package/src/container/container.mts +142 -170
- package/src/container/scoped-container.mts +126 -208
- package/src/decorators/factory.decorator.mts +16 -11
- package/src/decorators/injectable.decorator.mts +20 -16
- package/src/enums/index.mts +2 -2
- package/src/enums/injectable-scope.enum.mts +1 -0
- package/src/enums/injectable-type.enum.mts +1 -0
- package/src/errors/di-error.mts +96 -0
- package/src/event-emitter.mts +3 -27
- package/src/index.mts +6 -153
- package/src/interfaces/container.interface.mts +13 -0
- package/src/interfaces/factory.interface.mts +1 -1
- package/src/interfaces/index.mts +1 -1
- package/src/internal/context/async-local-storage.mts +3 -2
- package/src/internal/context/async-local-storage.types.mts +1 -0
- package/src/internal/context/factory-context.mts +1 -0
- package/src/internal/context/index.mts +3 -1
- package/src/internal/context/resolution-context.mts +1 -0
- package/src/internal/context/service-initialization-context.mts +43 -0
- package/src/internal/core/index.mts +5 -4
- package/src/internal/core/instance-resolver.mts +460 -302
- package/src/internal/core/name-resolver.mts +196 -0
- package/src/internal/core/scope-tracker.mts +242 -0
- package/src/internal/core/{instantiator.mts → service-initializer.mts} +51 -29
- package/src/internal/core/service-invalidator.mts +290 -0
- package/src/internal/core/token-resolver.mts +122 -0
- package/src/internal/holder/holder-storage.interface.mts +11 -5
- package/src/internal/holder/index.mts +2 -5
- package/src/internal/holder/instance-holder.mts +1 -3
- package/src/internal/holder/unified-storage.mts +245 -0
- package/src/internal/index.mts +2 -1
- package/src/internal/lifecycle/circular-detector.mts +1 -0
- package/src/internal/lifecycle/index.mts +1 -1
- package/src/internal/lifecycle/lifecycle-event-bus.mts +1 -0
- package/src/internal/stub-factory-class.mts +16 -0
- package/src/symbols/injectable-token.mts +3 -1
- package/src/testing/index.mts +2 -0
- package/src/testing/test-container.mts +546 -85
- package/src/testing/types.mts +117 -0
- package/src/testing/unit-test-container.mts +509 -0
- package/src/token/injection-token.mts +41 -4
- package/src/token/registry.mts +75 -9
- package/src/utils/default-injectors.mts +16 -0
- package/src/utils/get-injectable-token.mts +2 -3
- package/src/utils/get-injectors.mts +26 -15
- package/src/utils/index.mts +3 -1
- package/src/utils/types.mts +1 -0
- package/tsdown.config.mts +11 -1
- package/lib/browser/index.d.mts.map +0 -1
- package/lib/browser/index.mjs.map +0 -1
- package/lib/container-Bp1W-pWJ.d.mts.map +0 -1
- package/lib/container-DAKOvAgr.mjs.map +0 -1
- package/lib/container-DENMeJ87.cjs.map +0 -1
- package/lib/container-YPwvmlK2.d.cts.map +0 -1
- package/src/__tests__/async-local-storage.browser.spec.mts +0 -166
- package/src/__tests__/async-local-storage.spec.mts +0 -333
- package/src/__tests__/errors.spec.mts +0 -87
- package/src/__tests__/factory.spec.mts +0 -137
- package/src/__tests__/injectable.spec.mts +0 -246
- package/src/__tests__/request-scope.spec.mts +0 -416
- package/src/__tests__/service-instantiator.spec.mts +0 -410
- package/src/__tests__/service-locator-event-bus.spec.mts +0 -242
- package/src/__tests__/service-locator-manager.spec.mts +0 -300
- package/src/__tests__/service-locator.spec.mts +0 -966
- package/src/__tests__/unified-api.spec.mts +0 -130
- package/src/browser.mts +0 -11
- package/src/injectors.mts +0 -18
- package/src/internal/context/request-context.mts +0 -225
- package/src/internal/core/invalidator.mts +0 -437
- package/src/internal/core/service-locator.mts +0 -202
- package/src/internal/core/token-processor.mts +0 -252
- package/src/internal/holder/base-holder-manager.mts +0 -334
- package/src/internal/holder/holder-manager.mts +0 -85
- package/src/internal/holder/request-storage.mts +0 -127
- package/src/internal/holder/singleton-storage.mts +0 -92
- package/src/testing/README.md +0 -80
- package/src/testing/__tests__/test-container.spec.mts +0 -173
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from 'vitest'
|
|
2
|
-
import { z } from 'zod/v4'
|
|
3
|
-
|
|
4
|
-
import { Factory } from '../decorators/index.mjs'
|
|
5
|
-
import { InjectableScope } from '../enums/index.mjs'
|
|
6
|
-
import { Container, Registry } from '../index.mjs'
|
|
7
|
-
import { InjectionToken } from '../token/injection-token.mjs'
|
|
8
|
-
|
|
9
|
-
describe('Factory decorator', () => {
|
|
10
|
-
let container: Container
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
container = new Container()
|
|
13
|
-
})
|
|
14
|
-
it('should work with factory', async () => {
|
|
15
|
-
@Factory()
|
|
16
|
-
class Test {
|
|
17
|
-
create() {
|
|
18
|
-
return 'foo'
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const value = await container.get(Test)
|
|
23
|
-
expect(value).toBe('foo')
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('should work with request scope', async () => {
|
|
27
|
-
@Factory({
|
|
28
|
-
scope: InjectableScope.Transient,
|
|
29
|
-
})
|
|
30
|
-
class Test {
|
|
31
|
-
create() {
|
|
32
|
-
return Date.now()
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const value = await container.get(Test)
|
|
37
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
38
|
-
const value2 = await container.get(Test)
|
|
39
|
-
expect(value).not.toBe(value2)
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
it('should work with factory injection token and schema', async () => {
|
|
43
|
-
class TestFoo {
|
|
44
|
-
constructor(public readonly foo: string) {}
|
|
45
|
-
}
|
|
46
|
-
const token = InjectionToken.create(
|
|
47
|
-
TestFoo,
|
|
48
|
-
z.object({
|
|
49
|
-
foo: z.string(),
|
|
50
|
-
}),
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
@Factory({ token })
|
|
54
|
-
// oxlint-disable-next-line no-unused-vars
|
|
55
|
-
class Test {
|
|
56
|
-
create(ctx: any, args: { foo: string }) {
|
|
57
|
-
return new TestFoo(args.foo)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const value = await container.get(token, { foo: 'bar' })
|
|
62
|
-
const sameValue = await container.get(token, { foo: 'bar' })
|
|
63
|
-
const differentValue = await container.get(token, { foo: 'baz' })
|
|
64
|
-
// await new Promise((resolve) => setTimeout(resolve, 10))
|
|
65
|
-
expect(value).toBeInstanceOf(TestFoo)
|
|
66
|
-
expect(value.foo).toBe('bar')
|
|
67
|
-
expect(differentValue).toBeInstanceOf(TestFoo)
|
|
68
|
-
expect(differentValue.foo).toBe('baz')
|
|
69
|
-
expect(value).not.toBe(differentValue)
|
|
70
|
-
expect(value).toBe(sameValue)
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
it('should work with factory and inner inject', async () => {
|
|
74
|
-
@Factory()
|
|
75
|
-
class TestFactory {
|
|
76
|
-
create() {
|
|
77
|
-
return {
|
|
78
|
-
makeFoo: () => 'foo',
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
@Factory()
|
|
84
|
-
class Test2Factory {
|
|
85
|
-
create() {
|
|
86
|
-
return {
|
|
87
|
-
async makeFoo() {
|
|
88
|
-
const instance = await container.get(TestFactory)
|
|
89
|
-
return instance.makeFoo()
|
|
90
|
-
},
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const instance = await container.get(Test2Factory)
|
|
96
|
-
const result = await instance.makeFoo()
|
|
97
|
-
expect(result).toBe('foo')
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
it('should work with factory and custom registry', async () => {
|
|
101
|
-
const registry = new Registry()
|
|
102
|
-
const newContainer = new Container(registry)
|
|
103
|
-
|
|
104
|
-
@Factory({ registry })
|
|
105
|
-
class TestFactory {
|
|
106
|
-
create() {
|
|
107
|
-
return 'custom-registry-foo'
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const value = await newContainer.get(TestFactory)
|
|
112
|
-
expect(value).toBe('custom-registry-foo')
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
it('should work with factory and invalidation', async () => {
|
|
116
|
-
@Factory()
|
|
117
|
-
class TestFactory {
|
|
118
|
-
create() {
|
|
119
|
-
return Date.now()
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const factory = await container.get(TestFactory)
|
|
124
|
-
const result1 = factory
|
|
125
|
-
const inst2 = await container.get(TestFactory)
|
|
126
|
-
expect(factory).toBe(inst2)
|
|
127
|
-
const result2 = inst2
|
|
128
|
-
await container.invalidate(factory)
|
|
129
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
130
|
-
const inst3 = await container.get(TestFactory)
|
|
131
|
-
expect(factory).not.toBe(inst3)
|
|
132
|
-
const result3 = inst3
|
|
133
|
-
expect(result1).not.toBe(result3)
|
|
134
|
-
expect(result2).not.toBe(result3)
|
|
135
|
-
expect(result1).toBe(result2)
|
|
136
|
-
})
|
|
137
|
-
})
|
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from 'vitest'
|
|
2
|
-
import { z } from 'zod/v4'
|
|
3
|
-
|
|
4
|
-
import { Injectable } from '../decorators/index.mjs'
|
|
5
|
-
import {
|
|
6
|
-
asyncInject,
|
|
7
|
-
Container,
|
|
8
|
-
inject,
|
|
9
|
-
InjectableScope,
|
|
10
|
-
Registry,
|
|
11
|
-
} from '../index.mjs'
|
|
12
|
-
import { InjectionToken } from '../token/injection-token.mjs'
|
|
13
|
-
|
|
14
|
-
describe('Injectable decorator', () => {
|
|
15
|
-
let container: Container
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
container = new Container()
|
|
18
|
-
})
|
|
19
|
-
it('should work with class', async () => {
|
|
20
|
-
@Injectable()
|
|
21
|
-
class Test {}
|
|
22
|
-
|
|
23
|
-
const value = await container.get(Test)
|
|
24
|
-
expect(value).toBeInstanceOf(Test)
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('should work with inner inject', async () => {
|
|
28
|
-
@Injectable()
|
|
29
|
-
class Test {
|
|
30
|
-
makeFoo() {
|
|
31
|
-
return 'foo'
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
@Injectable()
|
|
36
|
-
class Test2 {
|
|
37
|
-
fooMaker = asyncInject(Test)
|
|
38
|
-
|
|
39
|
-
async makeFoo() {
|
|
40
|
-
const fooMaker = await this.fooMaker
|
|
41
|
-
return fooMaker.makeFoo()
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const value = await container.get(Test2)
|
|
46
|
-
expect(value).toBeInstanceOf(Test2)
|
|
47
|
-
const result = await value.makeFoo()
|
|
48
|
-
expect(result).toBe('foo')
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
it('should work with injection token', async () => {
|
|
52
|
-
const token = InjectionToken.create('Test')
|
|
53
|
-
|
|
54
|
-
@Injectable({ token })
|
|
55
|
-
class Test {}
|
|
56
|
-
|
|
57
|
-
const value = await container.get(token)
|
|
58
|
-
expect(value).toBeInstanceOf(Test)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
it('should work with invalidation', async () => {
|
|
62
|
-
@Injectable({ scope: InjectableScope.Transient })
|
|
63
|
-
class Test {
|
|
64
|
-
value = Date.now()
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
@Injectable()
|
|
68
|
-
class Test2 {
|
|
69
|
-
test = asyncInject(Test)
|
|
70
|
-
|
|
71
|
-
async makeFoo() {
|
|
72
|
-
const test = await this.test
|
|
73
|
-
return test.value
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
const inst1 = await container.get(Test2)
|
|
77
|
-
expect(inst1).toBeInstanceOf(Test2)
|
|
78
|
-
const result1 = await inst1.makeFoo()
|
|
79
|
-
const inst2 = await container.get(Test2)
|
|
80
|
-
expect(inst1).toBe(inst2)
|
|
81
|
-
const result2 = await inst2.makeFoo()
|
|
82
|
-
await container.invalidate(inst1)
|
|
83
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
84
|
-
const inst3 = await container.get(Test2)
|
|
85
|
-
expect(inst1).not.toBe(inst3)
|
|
86
|
-
const result3 = await inst3.makeFoo()
|
|
87
|
-
expect(result1).not.toBe(result3)
|
|
88
|
-
expect(result2).not.toBe(result3)
|
|
89
|
-
expect(result1).toBe(result2)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it('should work with inject', async () => {
|
|
93
|
-
@Injectable()
|
|
94
|
-
class Test {
|
|
95
|
-
value = Date.now()
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
@Injectable()
|
|
99
|
-
class Test2 {
|
|
100
|
-
test = inject(Test)
|
|
101
|
-
|
|
102
|
-
makeFoo() {
|
|
103
|
-
return this.test.value
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
const inst1 = await container.get(Test2)
|
|
107
|
-
expect(inst1).toBeInstanceOf(Test2)
|
|
108
|
-
const result1 = inst1.makeFoo()
|
|
109
|
-
const inst2 = await container.get(Test2)
|
|
110
|
-
expect(inst1).toBe(inst2)
|
|
111
|
-
const result2 = await inst2.makeFoo()
|
|
112
|
-
expect(result1).toBe(result2)
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
it('should work with constructor argument', async () => {
|
|
116
|
-
const schema = z.object({
|
|
117
|
-
foo: z.string(),
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
const token = InjectionToken.create('Test', schema)
|
|
121
|
-
|
|
122
|
-
@Injectable({ token })
|
|
123
|
-
class Test {
|
|
124
|
-
constructor(public readonly arg: z.output<typeof schema>) {}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const value = await container.get(token, { foo: 'bar' })
|
|
128
|
-
expect(value).toBeInstanceOf(Test)
|
|
129
|
-
// @ts-expect-error It's a test
|
|
130
|
-
expect(value.arg).toEqual({ foo: 'bar' })
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
it('should work with bound injection token', async () => {
|
|
134
|
-
const schema = z.object({
|
|
135
|
-
foo: z.string(),
|
|
136
|
-
})
|
|
137
|
-
const token = InjectionToken.create('Test', schema)
|
|
138
|
-
|
|
139
|
-
@Injectable({ token })
|
|
140
|
-
class Test {}
|
|
141
|
-
|
|
142
|
-
const boundToken = InjectionToken.bound(token, {
|
|
143
|
-
foo: 'bar',
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
const value = await container.get(boundToken)
|
|
147
|
-
expect(value).toBeInstanceOf(Test)
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
it('should work with bound injection token and custom registry', async () => {
|
|
151
|
-
const registry = new Registry()
|
|
152
|
-
const newContainer = new Container(registry, console)
|
|
153
|
-
const schema = z.object({
|
|
154
|
-
foo: z.string(),
|
|
155
|
-
})
|
|
156
|
-
const token = InjectionToken.create('TestInner', schema)
|
|
157
|
-
@Injectable({ token, registry })
|
|
158
|
-
class TestInner {}
|
|
159
|
-
|
|
160
|
-
const boundToken = InjectionToken.bound(token, {
|
|
161
|
-
foo: 'bar',
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
@Injectable({ registry })
|
|
165
|
-
class TestOuter {
|
|
166
|
-
foo = inject(boundToken)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const value = await newContainer.get(TestOuter)
|
|
170
|
-
expect(value).toBeInstanceOf(TestOuter)
|
|
171
|
-
expect(value.foo).toBeInstanceOf(TestInner)
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
it('should work with inject and Transient services', async () => {
|
|
175
|
-
@Injectable({ scope: InjectableScope.Transient })
|
|
176
|
-
class TransientService {
|
|
177
|
-
value = Math.random()
|
|
178
|
-
|
|
179
|
-
getValue() {
|
|
180
|
-
return this.value
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
@Injectable()
|
|
185
|
-
class ConsumerService {
|
|
186
|
-
private readonly transientService = inject(TransientService)
|
|
187
|
-
|
|
188
|
-
async getTransientValue() {
|
|
189
|
-
// Service should be available in async methods after initialization
|
|
190
|
-
return this.transientService.getValue()
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// Synchronous access should work after initialization
|
|
194
|
-
getTransientValueSync() {
|
|
195
|
-
return this.transientService.getValue()
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const consumer1 = await container.get(ConsumerService)
|
|
200
|
-
expect(consumer1).toBeInstanceOf(ConsumerService)
|
|
201
|
-
|
|
202
|
-
const value1 = await consumer1.getTransientValue()
|
|
203
|
-
expect(typeof value1).toBe('number')
|
|
204
|
-
|
|
205
|
-
const value2 = consumer1.getTransientValueSync()
|
|
206
|
-
expect(value2).toBe(value1) // Should be the same instance
|
|
207
|
-
|
|
208
|
-
// Each consumer gets a new transient instance
|
|
209
|
-
const consumer2 = await container.get(ConsumerService)
|
|
210
|
-
expect(consumer2).toBe(consumer1) // Consumer is singleton
|
|
211
|
-
|
|
212
|
-
const value3 = await consumer2.getTransientValue()
|
|
213
|
-
expect(value3).toBe(value1) // Same transient instance for same consumer
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
it('should track async dependencies with inject for Transient services', async () => {
|
|
217
|
-
@Injectable({ scope: InjectableScope.Transient })
|
|
218
|
-
class AsyncTransientService {
|
|
219
|
-
private initialized = false
|
|
220
|
-
|
|
221
|
-
async onServiceInit() {
|
|
222
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
223
|
-
this.initialized = true
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
isInitialized() {
|
|
227
|
-
return this.initialized
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
@Injectable()
|
|
232
|
-
class AsyncConsumerService {
|
|
233
|
-
private readonly transientService = inject(AsyncTransientService)
|
|
234
|
-
|
|
235
|
-
async checkInitialization() {
|
|
236
|
-
return this.transientService.isInitialized()
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const consumer = await container.get(AsyncConsumerService)
|
|
241
|
-
expect(consumer).toBeInstanceOf(AsyncConsumerService)
|
|
242
|
-
|
|
243
|
-
const isInitialized = await consumer.checkInitialization()
|
|
244
|
-
expect(isInitialized).toBe(true) // Should be initialized after container.get()
|
|
245
|
-
})
|
|
246
|
-
})
|