@navios/di 0.1.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.
Files changed (41) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +2 -0
  3. package/dist/_tsup-dts-rollup.d.mts +536 -0
  4. package/dist/_tsup-dts-rollup.d.ts +536 -0
  5. package/dist/index.d.mts +54 -0
  6. package/dist/index.d.ts +54 -0
  7. package/dist/index.js +1019 -0
  8. package/dist/index.mjs +960 -0
  9. package/package.json +46 -0
  10. package/src/__tests__/injectable.spec.mts +167 -0
  11. package/src/__tests__/injection-token.spec.mts +127 -0
  12. package/src/decorators/index.mts +1 -0
  13. package/src/decorators/injectable.decorator.mts +107 -0
  14. package/src/enums/index.mts +2 -0
  15. package/src/enums/injectable-scope.enum.mts +10 -0
  16. package/src/enums/injectable-type.enum.mts +4 -0
  17. package/src/errors/errors.enum.mts +8 -0
  18. package/src/errors/factory-not-found.mts +8 -0
  19. package/src/errors/factory-token-not-resolved.mts +10 -0
  20. package/src/errors/index.mts +7 -0
  21. package/src/errors/instance-destroying.mts +8 -0
  22. package/src/errors/instance-expired.mts +8 -0
  23. package/src/errors/instance-not-found.mts +8 -0
  24. package/src/errors/unknown-error.mts +15 -0
  25. package/src/event-emitter.mts +107 -0
  26. package/src/factory-context.mts +16 -0
  27. package/src/index.mts +16 -0
  28. package/src/injection-token.mts +130 -0
  29. package/src/injector.mts +19 -0
  30. package/src/interfaces/factory.interface.mts +11 -0
  31. package/src/interfaces/index.mts +1 -0
  32. package/src/proxy-service-locator.mts +83 -0
  33. package/src/registry.mts +65 -0
  34. package/src/resolve-service.mts +43 -0
  35. package/src/service-locator-event-bus.mts +96 -0
  36. package/src/service-locator-instance-holder.mts +63 -0
  37. package/src/service-locator-manager.mts +89 -0
  38. package/src/service-locator.mts +571 -0
  39. package/src/utils/get-injectable-token.mts +19 -0
  40. package/src/utils/get-injectors.mts +133 -0
  41. package/src/utils/index.mts +2 -0
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@navios/di",
3
+ "version": "0.1.0",
4
+ "author": {
5
+ "name": "Oleksandr Hanzha",
6
+ "email": "alex@granted.name"
7
+ },
8
+ "repository": {
9
+ "directory": "packages/di",
10
+ "type": "git",
11
+ "url": "https://github.com/Arilas/navios.git"
12
+ },
13
+ "license": "MIT",
14
+ "typings": "./dist/index.d.ts",
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/index.mjs",
17
+ "peerDependencies": {
18
+ "@navios/common": "^0.1.5",
19
+ "zod": "^3.23.8"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "import": {
24
+ "types": "./dist/index.d.mts",
25
+ "default": "./dist/index.mjs"
26
+ },
27
+ "require": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "./package.json": "./package.json"
33
+ },
34
+ "devDependencies": {
35
+ "@navios/common": "^0.1.5",
36
+ "tsx": "^4.19.4",
37
+ "typescript": "^5.8.3",
38
+ "zod": "^3.24.4"
39
+ },
40
+ "dependencies": {
41
+ "@fastify/cors": "^11.0.1",
42
+ "@fastify/multipart": "^9.0.3",
43
+ "fastify": "^5.3.2",
44
+ "fastify-type-provider-zod": "^4.0.2"
45
+ }
46
+ }
@@ -0,0 +1,167 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { z } from 'zod'
3
+
4
+ import { Injectable } from '../decorators/index.mjs'
5
+ import { InjectableScope, InjectableType } from '../enums/index.mjs'
6
+ import { syncInject } from '../index.mjs'
7
+ import { InjectionToken } from '../injection-token.mjs'
8
+ import { getGlobalServiceLocator, inject } from '../injector.mjs'
9
+ import { getInjectableToken } from '../utils/index.mjs'
10
+
11
+ describe('Injectable decorator', () => {
12
+ it('should work with class', async () => {
13
+ @Injectable()
14
+ class Test {}
15
+
16
+ const value = await inject(Test)
17
+ expect(value).toBeInstanceOf(Test)
18
+ })
19
+
20
+ it('should work with inner inject', async () => {
21
+ @Injectable()
22
+ class Test {
23
+ makeFoo() {
24
+ return 'foo'
25
+ }
26
+ }
27
+
28
+ @Injectable()
29
+ class Test2 {
30
+ fooMaker = inject(Test)
31
+
32
+ async makeFoo() {
33
+ const fooMaker = await this.fooMaker
34
+ return fooMaker.makeFoo()
35
+ }
36
+ }
37
+
38
+ const value = await inject(Test2)
39
+ expect(value).toBeInstanceOf(Test2)
40
+ const result = await value.makeFoo()
41
+ expect(result).toBe('foo')
42
+ })
43
+
44
+ it('should work with factory', async () => {
45
+ @Injectable({ type: InjectableType.Factory })
46
+ class Test {
47
+ create() {
48
+ return 'foo'
49
+ }
50
+ }
51
+
52
+ const value = await inject(Test)
53
+ expect(value).toBe('foo')
54
+ })
55
+ it('should work with request scope', async () => {
56
+ @Injectable({
57
+ scope: InjectableScope.Instance,
58
+ type: InjectableType.Factory,
59
+ })
60
+ class Test {
61
+ create() {
62
+ return Date.now()
63
+ }
64
+ }
65
+
66
+ const value = await inject(Test)
67
+ await new Promise((resolve) => setTimeout(resolve, 10))
68
+ const value2 = await inject(Test)
69
+ expect(value).not.toBe(value2)
70
+ })
71
+
72
+ it('should work with injection token', async () => {
73
+ const token = InjectionToken.create('Test')
74
+
75
+ @Injectable({ token })
76
+ class Test {}
77
+
78
+ const value = await inject(token)
79
+ expect(value).toBeInstanceOf(Test)
80
+ })
81
+
82
+ it('should work with injection token and schema', async () => {
83
+ class TestFoo {
84
+ constructor(public readonly foo: string) {}
85
+ }
86
+ const token = InjectionToken.create(
87
+ TestFoo,
88
+ z.object({
89
+ foo: z.string(),
90
+ }),
91
+ )
92
+
93
+ @Injectable({ token, type: InjectableType.Factory })
94
+ class Test {
95
+ create(ctx: any, args: { foo: string }) {
96
+ return new TestFoo(args.foo)
97
+ }
98
+ }
99
+
100
+ const value = await inject(token, { foo: 'bar' })
101
+ const differentValue = await inject(token, { foo: 'baz' })
102
+ const sameValue = await inject(token, { foo: 'bar' })
103
+ expect(value).toBeInstanceOf(TestFoo)
104
+ expect(value.foo).toBe('bar')
105
+ expect(differentValue).toBeInstanceOf(TestFoo)
106
+ expect(differentValue.foo).toBe('baz')
107
+ expect(value).not.toBe(differentValue)
108
+ expect(value).toBe(sameValue)
109
+ })
110
+ it('should work with invalidation', async () => {
111
+ @Injectable()
112
+ class Test {
113
+ value = Date.now()
114
+ }
115
+
116
+ @Injectable()
117
+ class Test2 {
118
+ test = inject(Test)
119
+
120
+ async makeFoo() {
121
+ const test = await this.test
122
+ return test.value
123
+ }
124
+ }
125
+ const identifier = getGlobalServiceLocator().getInstanceIdentifier(
126
+ getInjectableToken(Test),
127
+ undefined,
128
+ )
129
+ const inst1 = await inject(Test2)
130
+ expect(inst1).toBeInstanceOf(Test2)
131
+ const result1 = await inst1.makeFoo()
132
+ const inst2 = await inject(Test2)
133
+ expect(inst1).toBe(inst2)
134
+ const result2 = await inst2.makeFoo()
135
+ await getGlobalServiceLocator().invalidate(identifier)
136
+ await new Promise((resolve) => setTimeout(resolve, 10))
137
+ const inst3 = await inject(Test2)
138
+ expect(inst1).not.toBe(inst3)
139
+ const result3 = await inst3.makeFoo()
140
+ expect(result1).not.toBe(result3)
141
+ expect(result2).not.toBe(result3)
142
+ expect(result1).toBe(result2)
143
+ })
144
+
145
+ it('should work with syncInject', async () => {
146
+ @Injectable()
147
+ class Test {
148
+ value = Date.now()
149
+ }
150
+
151
+ @Injectable()
152
+ class Test2 {
153
+ test = syncInject(Test)
154
+
155
+ makeFoo() {
156
+ return this.test.value
157
+ }
158
+ }
159
+ const inst1 = await inject(Test2)
160
+ expect(inst1).toBeInstanceOf(Test2)
161
+ const result1 = inst1.makeFoo()
162
+ const inst2 = await inject(Test2)
163
+ expect(inst1).toBe(inst2)
164
+ const result2 = await inst2.makeFoo()
165
+ expect(result1).toBe(result2)
166
+ })
167
+ })
@@ -0,0 +1,127 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { z } from 'zod'
3
+
4
+ import type { Factory, FactoryWithArgs } from '../interfaces/index.mjs'
5
+
6
+ import { Injectable } from '../decorators/index.mjs'
7
+ import { InjectableType } from '../enums/index.mjs'
8
+ import { InjectionToken } from '../injection-token.mjs'
9
+ import { inject } from '../injector.mjs'
10
+
11
+ describe('InjectToken', () => {
12
+ it('should work with class', async () => {
13
+ const token = InjectionToken.create('Test')
14
+ @Injectable({
15
+ token,
16
+ })
17
+ class Test {}
18
+
19
+ const value = await inject(Test)
20
+ expect(value).toBeInstanceOf(Test)
21
+ })
22
+
23
+ it('should work with class and schema', async () => {
24
+ const schema = z.object({
25
+ test: z.string(),
26
+ })
27
+ const token = InjectionToken.create('Test', schema)
28
+
29
+ @Injectable({
30
+ token,
31
+ })
32
+ class Test {
33
+ makeFoo() {
34
+ return 'foo'
35
+ }
36
+ }
37
+ const value = await inject(token, {
38
+ test: 'test',
39
+ })
40
+
41
+ expect(value).toBeInstanceOf(Test)
42
+ })
43
+
44
+ it('should work with factory', async () => {
45
+ const token = InjectionToken.create<string>('Test')
46
+ @Injectable({
47
+ token,
48
+ type: InjectableType.Factory,
49
+ })
50
+ class Test implements Factory<string> {
51
+ async create() {
52
+ return 'foo'
53
+ }
54
+ }
55
+
56
+ const value = await inject(Test)
57
+ expect(value).toBe('foo')
58
+ })
59
+
60
+ it('should work with factory and schema', async () => {
61
+ const schema = z.object({
62
+ test: z.string(),
63
+ })
64
+ const token = InjectionToken.create<string, typeof schema>('Test', schema)
65
+
66
+ @Injectable({
67
+ token,
68
+ type: InjectableType.Factory,
69
+ })
70
+ class Test implements FactoryWithArgs<string, typeof schema> {
71
+ async create(ctx: any, args: { test: string }) {
72
+ return args.test
73
+ }
74
+ }
75
+ const value = await inject(token, {
76
+ test: 'test',
77
+ })
78
+
79
+ expect(value).toBe('test')
80
+ })
81
+
82
+ it('should work with bound token', async () => {
83
+ const schema = z.object({
84
+ test: z.string(),
85
+ })
86
+ const token = InjectionToken.create('Test', schema)
87
+ const boundToken = InjectionToken.bound(token, {
88
+ test: 'test',
89
+ })
90
+
91
+ @Injectable({
92
+ token,
93
+ })
94
+ class Test {
95
+ makeFoo() {
96
+ return 'foo'
97
+ }
98
+ }
99
+ const value = await inject(boundToken)
100
+
101
+ expect(value).toBeInstanceOf(Test)
102
+ })
103
+
104
+ it('should work with factory token', async () => {
105
+ const schema = z.object({
106
+ test: z.string(),
107
+ })
108
+ const token = InjectionToken.create('Test', schema)
109
+ const factoryInjectionToken = InjectionToken.factory(token, () =>
110
+ Promise.resolve({
111
+ test: 'test',
112
+ }),
113
+ )
114
+
115
+ @Injectable({
116
+ token,
117
+ })
118
+ class Test {
119
+ makeFoo() {
120
+ return 'foo'
121
+ }
122
+ }
123
+ const value = await inject(factoryInjectionToken)
124
+
125
+ expect(value).toBeInstanceOf(Test)
126
+ })
127
+ })
@@ -0,0 +1 @@
1
+ export * from './injectable.decorator.mjs'
@@ -0,0 +1,107 @@
1
+ import type { AnyZodObject } from 'zod'
2
+
3
+ import { NaviosException } from '@navios/common'
4
+
5
+ import type { ClassType, ClassTypeWithInstance } from '../injection-token.mjs'
6
+ import type { Factory, FactoryWithArgs } from '../interfaces/index.mjs'
7
+ import type { Registry } from '../registry.mjs'
8
+
9
+ import { InjectableScope, InjectableType } from '../enums/index.mjs'
10
+ import { InjectionToken } from '../injection-token.mjs'
11
+ import { globalRegistry } from '../registry.mjs'
12
+ import { resolveService } from '../resolve-service.mjs'
13
+
14
+ export interface InjectableOptions {
15
+ scope?: InjectableScope
16
+ type?: InjectableType
17
+ token?: InjectionToken<any, any>
18
+ registry?: Registry
19
+ }
20
+
21
+ export const InjectableTokenMeta = Symbol.for('InjectableTokenMeta')
22
+
23
+ export function Injectable(): <T extends ClassType>(
24
+ target: T,
25
+ context: ClassDecoratorContext,
26
+ ) => T & { [InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined> }
27
+ export function Injectable<T extends ClassType>(options: {
28
+ scope?: InjectableScope
29
+ token: InjectionToken<T, undefined>
30
+ }): (
31
+ target: T,
32
+ context: ClassDecoratorContext,
33
+ ) => T & {
34
+ [InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>
35
+ }
36
+ export function Injectable<R>(options: {
37
+ scope?: InjectableScope
38
+ type: InjectableType.Factory
39
+ }): <T extends ClassTypeWithInstance<Factory<R>>>(
40
+ target: T,
41
+ context: ClassDecoratorContext,
42
+ ) => T & { [InjectableTokenMeta]: InjectionToken<R, undefined> }
43
+ export function Injectable<R, S extends AnyZodObject>(options: {
44
+ scope?: InjectableScope
45
+ type: InjectableType.Factory
46
+ token: InjectionToken<R, S>
47
+ }): <T extends ClassTypeWithInstance<FactoryWithArgs<R, S>>>(
48
+ target: T,
49
+ context: ClassDecoratorContext,
50
+ ) => T & { [InjectableTokenMeta]: InjectionToken<R, S> }
51
+ export function Injectable<R>(options: {
52
+ scope?: InjectableScope
53
+ type: InjectableType.Factory
54
+ token: InjectionToken<R, undefined>
55
+ }): <T extends ClassTypeWithInstance<Factory<R>>>(
56
+ target: T,
57
+ context: ClassDecoratorContext,
58
+ ) => T & { [InjectableTokenMeta]: InjectionToken<R, undefined> }
59
+ export function Injectable({
60
+ scope = InjectableScope.Singleton,
61
+ type = InjectableType.Class,
62
+ token,
63
+ registry = globalRegistry,
64
+ }: InjectableOptions = {}) {
65
+ return <T extends ClassType>(
66
+ target: T,
67
+ context: ClassDecoratorContext,
68
+ ): T & {
69
+ [InjectableTokenMeta]: InjectionToken<any, any>
70
+ } => {
71
+ if (context.kind !== 'class') {
72
+ throw new Error(
73
+ '[ServiceLocator] @Injectable decorator can only be used on classes.',
74
+ )
75
+ }
76
+ let injectableToken: InjectionToken<any, any> =
77
+ token ?? InjectionToken.create(target)
78
+ if (type === InjectableType.Class) {
79
+ registry.set(
80
+ injectableToken,
81
+ async (ctx) => resolveService(ctx, target),
82
+ scope,
83
+ )
84
+ } else if (type === InjectableType.Factory) {
85
+ registry.set(
86
+ injectableToken,
87
+ async (ctx, args: any) => {
88
+ const builder = await resolveService(ctx, target)
89
+ if (typeof builder.create !== 'function') {
90
+ throw new NaviosException(
91
+ `[ServiceLocator] Factory ${target.name} does not implement the create method.`,
92
+ )
93
+ }
94
+ return builder.create(ctx, args)
95
+ },
96
+ scope,
97
+ )
98
+ }
99
+
100
+ // @ts-expect-error
101
+ target[InjectableTokenMeta] = injectableToken
102
+
103
+ return target as T & {
104
+ [InjectableTokenMeta]: InjectionToken<any, any>
105
+ }
106
+ }
107
+ }
@@ -0,0 +1,2 @@
1
+ export * from './injectable-scope.enum.mjs'
2
+ export * from './injectable-type.enum.mjs'
@@ -0,0 +1,10 @@
1
+ export enum InjectableScope {
2
+ /**
3
+ * Singleton scope: The instance is created once and shared across the application.
4
+ */
5
+ Singleton = 'Singleton',
6
+ /**
7
+ * Instance scope: A new instance is created for each injection.
8
+ */
9
+ Instance = 'Instance',
10
+ }
@@ -0,0 +1,4 @@
1
+ export enum InjectableType {
2
+ Class = 'Class',
3
+ Factory = 'Factory',
4
+ }
@@ -0,0 +1,8 @@
1
+ export enum ErrorsEnum {
2
+ InstanceExpired = 'InstanceExpired',
3
+ InstanceNotFound = 'InstanceNotFound',
4
+ InstanceDestroying = 'InstanceDestroying',
5
+ UnknownError = 'UnknownError',
6
+ FactoryNotFound = 'FactoryNotFound',
7
+ FactoryTokenNotResolved = 'FactoryTokenNotResolved',
8
+ }
@@ -0,0 +1,8 @@
1
+ import { ErrorsEnum } from './errors.enum.mjs'
2
+
3
+ export class FactoryNotFound extends Error {
4
+ code = ErrorsEnum.FactoryNotFound
5
+ constructor(public name: string) {
6
+ super(`Factory ${name} not found`)
7
+ }
8
+ }
@@ -0,0 +1,10 @@
1
+ import type { ClassType } from '../injection-token.mjs'
2
+
3
+ import { ErrorsEnum } from './errors.enum.mjs'
4
+
5
+ export class FactoryTokenNotResolved extends Error {
6
+ code = ErrorsEnum.FactoryTokenNotResolved
7
+ constructor(name: string | symbol | ClassType) {
8
+ super(`Factory token not resolved: ${name.toString()}`)
9
+ }
10
+ }
@@ -0,0 +1,7 @@
1
+ export * from './errors.enum.mjs'
2
+ export * from './factory-not-found.mjs'
3
+ export * from './factory-token-not-resolved.mjs'
4
+ export * from './instance-destroying.mjs'
5
+ export * from './instance-expired.mjs'
6
+ export * from './instance-not-found.mjs'
7
+ export * from './unknown-error.mjs'
@@ -0,0 +1,8 @@
1
+ import { ErrorsEnum } from './errors.enum.mjs'
2
+
3
+ export class InstanceDestroying extends Error {
4
+ code = ErrorsEnum.InstanceDestroying
5
+ constructor(public name: string) {
6
+ super(`Instance ${name} destroying`)
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import { ErrorsEnum } from './errors.enum.mjs'
2
+
3
+ export class InstanceExpired extends Error {
4
+ code = ErrorsEnum.InstanceExpired
5
+ constructor(public name: string) {
6
+ super(`Instance ${name} expired`)
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import { ErrorsEnum } from './errors.enum.mjs'
2
+
3
+ export class InstanceNotFound extends Error {
4
+ code = ErrorsEnum.InstanceNotFound
5
+ constructor(public name: string) {
6
+ super(`Instance ${name} not found`)
7
+ }
8
+ }
@@ -0,0 +1,15 @@
1
+ import { ErrorsEnum } from './errors.enum.mjs'
2
+
3
+ export class UnknownError extends Error {
4
+ code = ErrorsEnum.UnknownError
5
+ parent?: Error
6
+
7
+ constructor(message: string | Error) {
8
+ if (message instanceof Error) {
9
+ super(message.message)
10
+ this.parent = message
11
+ return
12
+ }
13
+ super(message)
14
+ }
15
+ }
@@ -0,0 +1,107 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ /* eslint-disable @typescript-eslint/no-empty-object-type */
3
+ /* eslint-disable @typescript-eslint/no-unsafe-function-type */
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ export type EventsConfig = {
6
+ [event: string]: any[]
7
+ }
8
+ export type EventsNames<Events extends EventsConfig> = Exclude<keyof Events, symbol | number>
9
+ export type EventsArgs<
10
+ Events extends EventsConfig,
11
+ Name extends EventsNames<Events>,
12
+ > = Events[Name] extends any[] ? Events[Name] : []
13
+
14
+ export type ChannelEmitter<
15
+ Events extends EventsConfig,
16
+ Ns extends string,
17
+ E extends EventsNames<Events>,
18
+ > = {
19
+ emit<Args extends EventsArgs<Events, E>>(ns: Ns, event: E, ...args: Args): Promise<any>
20
+ }
21
+
22
+ export interface EventEmitterInterface<Events extends EventsConfig> {
23
+ on<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
24
+ event: E,
25
+ listener: (...args: Args) => void,
26
+ ): () => void
27
+ emit<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
28
+ event: E,
29
+ ...args: Args
30
+ ): void
31
+ addChannel<
32
+ E extends EventsNames<Events>,
33
+ Ns extends string,
34
+ Emmiter extends ChannelEmitter<Events, Ns, E>,
35
+ >(
36
+ ns: Ns,
37
+ event: E,
38
+ target: Emmiter,
39
+ ): () => void
40
+ }
41
+
42
+ export class EventEmitter<Events extends EventsConfig = {}>
43
+ implements EventEmitterInterface<Events>
44
+ {
45
+ private listeners: Map<EventsNames<Events>, Set<Function>> = new Map()
46
+
47
+ on<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
48
+ event: E,
49
+ listener: (...args: Args) => void,
50
+ ) {
51
+ if (!this.listeners.has(event)) {
52
+ this.listeners.set(event, new Set())
53
+ }
54
+
55
+ this.listeners.get(event)!.add(listener)
56
+
57
+ return () => {
58
+ this.off(event, listener)
59
+ }
60
+ }
61
+
62
+ off<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
63
+ event: E,
64
+ listener: (...args: Args) => void,
65
+ ) {
66
+ if (!this.listeners.has(event)) {
67
+ return
68
+ }
69
+
70
+ this.listeners.get(event)!.delete(listener)
71
+ if (this.listeners.get(event)!.size === 0) {
72
+ this.listeners.delete(event)
73
+ }
74
+ }
75
+
76
+ once<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
77
+ event: E,
78
+ listener: (...args: Args) => void,
79
+ ) {
80
+ const off = this.on(event, (...args) => {
81
+ off()
82
+ // @ts-expect-error - This is a valid call
83
+ listener(...args)
84
+ })
85
+
86
+ return off
87
+ }
88
+
89
+ async emit<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
90
+ event: E,
91
+ ...args: Args
92
+ ): Promise<any> {
93
+ if (!this.listeners.has(event)) {
94
+ return
95
+ }
96
+
97
+ return Promise.all(Array.from(this.listeners.get(event)!).map(listener => listener(...args)))
98
+ }
99
+
100
+ addChannel<
101
+ E extends EventsNames<Events>,
102
+ Ns extends string,
103
+ Emitter extends ChannelEmitter<Events, Ns, E>,
104
+ >(ns: Ns, event: E, target: Emitter) {
105
+ return this.on(event, (...args) => target.emit(ns, event, ...args))
106
+ }
107
+ }
@@ -0,0 +1,16 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-object-type */
2
+ import type { ServiceLocatorEventBus } from './service-locator-event-bus.mjs'
3
+ import type { ServiceLocator } from './service-locator.mjs'
4
+ import type { Injectors } from './utils/index.mjs'
5
+
6
+ export interface FactoryContext {
7
+ inject: Injectors['inject']
8
+ on: ServiceLocatorEventBus['on']
9
+ getDependencies: () => string[]
10
+ invalidate: () => void
11
+ addEffect: (listener: () => void) => void
12
+ getDestroyListeners: () => (() => void)[]
13
+ setTtl: (ttl: number) => void
14
+ getTtl: () => number
15
+ locator: ServiceLocator
16
+ }