-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.ts
573 lines (530 loc) · 15 KB
/
container.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import { z } from 'zod';
import {
InvalidSchemaError,
CircularDependencyError,
UnresolvedDependencyError
} from './errors.ts';
import {
Bindable,
Binding,
ContextualBinding,
Middleware,
WithParamTypes,
IContainer,
IContextualBindingBuilder,
IContextualBindingNeedsBuilder,
IObserver,
Lifetime, // Import Lifetime enum directly
} from './types.ts';
/**
* Dependency Injection Container
*
* The Container class provides a way to manage dependencies and their lifecycles.
* It supports various binding types, contextual bindings, and middleware.
*/
export class Container implements IContainer {
private bindings = new Map<Bindable, Binding>();
private instances = new Map<Bindable, any>();
private aliases: Map<string | symbol, Bindable> = new Map();
private tags = new Map<string, Bindable[]>();
public contextualBindings: Map<Bindable, ContextualBinding[]> = new Map<Bindable, ContextualBinding[]>();
private resolvingStack: Bindable[] = [];
private parent: Container | null = null;
private children: Container[] = [];
private middlewares: Middleware[] = [];
// Binding Methods
/**
* Bind a type to a factory function.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @param schema - Optional Zod schema for validation.
* @returns The Container instance.
*
* @example
* ```typescript
* container.bind('MyService', () => new MyService());
* ```
*/
bind(abstract: Bindable, factory: Function, schema?: z.ZodType<any>): this {
if (schema) {
try {
const instance = factory(this);
schema.parse(instance);
} catch (error) {
if (error instanceof z.ZodError) {
throw new InvalidSchemaError(`Invalid schema for ${String(abstract)}: ${error.message}`);
}
throw error;
}
}
this.bindings.set(abstract, {
factory,
lifetime: Lifetime.Transient, // Use Lifetime.Transient as default
resolver: (container: Container) => factory(container),
});
return this;
}
/**
* Bind a singleton.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @returns The Container instance.
*
* @example
* ```typescript
* container.singleton('MySingletonService', () => new MySingletonService());
* ```
*/
singleton(abstract: Bindable, factory: Function): this {
this.bindings.set(abstract, {
factory,
lifetime: Lifetime.Singleton,
resolver: factory,
});
return this;
}
/**
* Bind an instance.
*
* @param abstract - The abstract type to bind.
* @param instance - The instance to bind.
* @returns The Container instance.
*
* @example
* ```typescript
* container.instance('MyInstance', new MyInstance());
* ```
*/
instance(abstract: Bindable, instance: any): this {
this.instances.set(abstract, instance);
return this;
}
/**
* Alias an abstract type.
*
* @param alias - The alias to create.
* @param abstract - The abstract type to alias.
*
* @example
* ```typescript
* container.alias('Logger', 'ConsoleLogger');
* ```
*/
alias(alias: string | symbol, abstract: Bindable): void {
this.aliases.set(alias, abstract);
}
/**
* Tag multiple abstracts with a tag.
*
* @param abstracts - The abstracts to tag.
* @param tag - The tag to assign.
* @returns The Container instance.
*
* @example
* ```typescript
* container.tag(['ServiceA', 'ServiceB'], 'services');
* ```
*/
tag(abstracts: Bindable[], tag: string): this {
if (!this.tags.has(tag)) {
this.tags.set(tag, []);
}
this.tags.get(tag)?.push(...abstracts);
return this;
}
// Resolution Methods
/**
* Resolve a type.
*
* @param abstract - The abstract type to resolve.
* @param context - Optional context for contextual bindings.
* @returns The resolved instance.
*
* @example
* ```typescript
* const myService = container.resolve<MyService>('MyService');
* ```
*/
resolve<T = any>(abstract: Bindable, context?: Bindable): T {
const originalAbstract = abstract;
abstract = this.getAlias(abstract);
if (this.instances.has(abstract)) {
return this.instances.get(abstract);
}
if (this.resolvingStack.includes(abstract)) {
// Return a proxy object for circular dependencies
return new Proxy({} as any, {
get: (target, prop) => {
if (prop === 'isCircularDependency') return true;
if (typeof prop === 'string' && prop !== 'then') {
return (...args: any[]) => {
const resolvedInstance = this.resolve(abstract);
return (resolvedInstance as any)[prop](...args);
};
}
return undefined;
},
});
}
this.resolvingStack.push(abstract);
try {
let binding = this.bindings.get(abstract);
if (!binding) {
if (this.parent) {
return this.parent.resolve<T>(originalAbstract);
}
throw new UnresolvedDependencyError(originalAbstract);
}
let instance;
if (binding.lifetime === Lifetime.Singleton && binding.instance) {
instance = binding.instance;
} else {
if (context) {
const contextualBindings = this.contextualBindings.get(context) || [];
const contextualBinding = contextualBindings.find(b => b.need === abstract);
if (contextualBinding) {
instance = typeof contextualBinding.give === 'function'
? contextualBinding.give(this)
: contextualBinding.give;
}
}
if (!instance) {
instance = this.build(binding, context);
}
if (binding.lifetime === Lifetime.Singleton) {
binding.instance = instance;
this.instances.set(abstract, instance);
}
}
return this.applyMiddleware(() => instance);
} finally {
this.resolvingStack.pop();
}
}
/**
* Resolve a type asynchronously.
*
* @param abstract - The abstract type to resolve.
* @param context - Optional context for contextual bindings.
* @returns A promise that resolves to the instance.
*
* @example
* ```typescript
* const myService = await container.resolveAsync<MyService>('MyService');
* ```
*/
async resolveAsync<T = any>(abstract: Bindable, context?: Bindable): Promise<T> {
const instance = await this.resolve<T>(abstract, context);
return instance instanceof Promise ? await instance : instance;
}
/**
* Create an instance of a class with dependencies.
*
* @param Target - The class to instantiate.
* @returns The created instance.
*
* @example
* ```typescript
* const myClassInstance = container.createInstance(MyClass);
* ```
*/
createInstance<T>(Target: Bindable & WithParamTypes): T {
const paramTypes = Target.paramTypes || [];
const injections = paramTypes.map((param: any) => this.resolveWithContext(param, Target));
return new (Target as any)(...injections);
}
// Contextual Binding Methods
/**
* Create a contextual binding.
*
* @param concrete - The concrete type to bind.
* @returns A builder for defining the contextual binding.
*
* @example
* ```typescript
* container.when('MyService').needs('Config').give(() => new Config());
* ```
*/
when(concrete: Bindable): IContextualBindingBuilder {
return new ContextualBindingBuilder(this, concrete);
}
// Lifecycle Methods
/**
* Create a new scope.
*
* @returns A new Container instance representing the scope.
*
* @example
* ```typescript
* const scopedContainer = container.createScope();
* ```
*/
createScope(): IContainer {
const childContainer = new Container();
childContainer.parent = this;
this.children.push(childContainer);
return childContainer;
}
/**
* Dispose of the container and its bindings.
*
* @example
* ```typescript
* container.dispose();
* ```
*/
dispose(): void {
for (const [, binding] of this.bindings) {
if (binding.instance && typeof binding.instance.dispose === 'function') {
binding.instance.dispose();
}
}
for (const child of this.children) {
child.dispose();
}
this.bindings.clear();
this.instances.clear();
this.aliases.clear();
this.tags.clear();
this.contextualBindings.clear();
this.children = [];
}
/**
* Create a child container.
*
* @returns A new Container instance representing the child container.
*
* @example
* ```typescript
* const childContainer = container.createChild();
* ```
*/
createChild(): IContainer {
const child = new Container();
child.parent = this;
return child;
}
/**
* Bind a transient.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @returns The Container instance.
*
* @example
* ```typescript
* container.transient('MyService', () => new MyService());
* ```
*/
transient(abstract: Bindable, factory: Function): this {
this.bindings.set(abstract, {
factory,
lifetime: Lifetime.Transient,
resolver: factory,
});
return this;
}
/**
* Bind a scoped instance.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @returns The Container instance.
*
* @example
* ```typescript
* container.scoped('MyService', () => new MyService());
* ```
*/
scoped(abstract: Bindable, factory: Function): this {
this.bindings.set(abstract, {
factory,
lifetime: Lifetime.Scoped,
resolver: (container: Container) => {
if (container.instances.has(abstract)) {
return container.instances.get(abstract);
}
const instance = factory(container);
container.instances.set(abstract, instance);
return instance;
},
});
return this;
}
/**
* Lazy bind a type to a factory function.
*
* @param token - The token to bind.
* @param factory - The factory function to create the instance.
*
* @example
* ```typescript
* container.lazyBind('MyService', () => new MyService());
* ```
*/
lazyBind<T>(token: any, factory: () => T): void {
this.bind(token, () => {
const instance = factory();
this.instance(token, instance);
return instance;
});
}
// Middleware Methods
/**
* Use a middleware function.
*
* @param middleware - The middleware function to use.
*
* @example
* ```typescript
* container.use(next => {
* console.log('Before');
* const result = next();
* console.log('After');
* return result;
* });
* ```
*/
use(middleware: Middleware): void {
this.middlewares.push(middleware);
}
// Private Methods
private resolveWithContext(abstract: Bindable, context: Bindable): any {
const contextualBindings = this.contextualBindings.get(context) || [];
const binding = contextualBindings.find(b => b.need === abstract);
if (binding) {
return typeof binding.give === 'function' ? binding.give(this) : binding.give;
}
return this.resolve(abstract);
}
private build(binding: Binding, context?: Bindable): any {
return binding.resolver(this, context);
}
private getAlias(abstract: Bindable): Bindable {
if (typeof abstract === 'string' || typeof abstract === 'symbol') {
return this.aliases.get(abstract) || abstract;
}
return abstract;
}
private applyMiddleware(factory: () => any): any {
let index = -1;
const dispatch = (i: number): any => {
if (i <= index) return Promise.reject(new Error('next() called multiple times'));
index = i;
let fn = this.middlewares[i];
if (i === this.middlewares.length) fn = factory;
if (!fn) return;
try {
return fn(() => dispatch(i + 1));
} catch (err) {
return Promise.reject(err);
}
};
return dispatch(0);
}
// Add this method
tagged(tag: string): any[] {
const taggedBindables = this.tags.get(tag) || [];
return taggedBindables.map(bindable => this.resolve(bindable));
}
}
/**
* Contextual Binding Builder
*
* The ContextualBindingBuilder class provides a way to define contextual bindings.
*/
class ContextualBindingBuilder implements IContextualBindingBuilder {
constructor(private container: Container, private concrete: Bindable) {}
/**
* Define the dependency that the concrete type needs.
*
* @param need - The dependency that the concrete type needs.
* @returns A builder for defining the contextual binding.
*
* @example
* ```typescript
* container.when('MyService').needs('Config').give(() => new Config());
* ```
*/
needs(need: Bindable): IContextualBindingNeedsBuilder {
return new ContextualBindingNeedsBuilder(this.container, this.concrete, need);
}
}
/**
* Contextual Binding Needs Builder
*
* The ContextualBindingNeedsBuilder class provides a way to define the value or factory function for a contextual binding.
*/
class ContextualBindingNeedsBuilder implements IContextualBindingNeedsBuilder {
constructor(
private container: Container,
private concrete: Bindable,
private need: Bindable,
) {}
/**
* Define the value or factory function for the contextual binding.
*
* @param give - The value or factory function to provide.
*
* @example
* ```typescript
* container.when('MyService').needs('Config').give(() => new Config());
* ```
*/
give(give: Function | any): void {
if (!this.container.contextualBindings.has(this.concrete)) {
this.container.contextualBindings.set(this.concrete, []);
}
this.container.contextualBindings.get(this.concrete)?.push({
when: this.concrete,
need: this.need,
give,
});
}
}
/**
* Observer
*
* The Observer class provides a way to implement the observer pattern.
*/
export class Observer implements IObserver {
private listeners: Map<string, Function[]> = new Map();
/**
* Subscribe to an event.
*
* @param event - The event to subscribe to.
* @param callback - The callback function to call when the event is published.
*
* @example
* ```typescript
* observer.subscribe('myEvent', data => {
* console.log('Event received:', data);
* });
* ```
*/
subscribe(event: string, callback: Function) {
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event)!.push(callback);
}
/**
* Publish an event.
*
* @param event - The event to publish.
* @param data - Optional data to pass to the event listeners.
*
* @example
* ```typescript
* observer.publish('myEvent', { key: 'value' });
* ```
*/
publish(event: string, data?: any) {
const eventListeners = this.listeners.get(event);
if (eventListeners) {
eventListeners.forEach(callback => callback(data));
}
}
}