@travetto/di 6.0.0 → 7.0.0-rc.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/README.md CHANGED
@@ -16,7 +16,7 @@ yarn add @travetto/di
16
16
  [Dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) is a framework primitive. When used in conjunction with automatic file scanning, it provides for handling of application dependency wiring. Due to the nature of [Typescript](https://typescriptlang.org) and type erasure of interfaces, dependency injection only supports `class`es as a type signifier. The primary goal of dependency injection is to allow for separation of concerns of object creation and it's usage.
17
17
 
18
18
  ## Declaration
19
- The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) and [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L70) decorators provide the registration of dependencies. Dependency declaration revolves around exposing `class`es and subtypes thereof to provide necessary functionality. Additionally, the framework will utilize dependencies to satisfy contracts with various implementation.
19
+ The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) and [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L47) decorators provide the registration of dependencies. Dependency declaration revolves around exposing `class`es and subtypes thereof to provide necessary functionality. Additionally, the framework will utilize dependencies to satisfy contracts with various implementation.
20
20
 
21
21
  **Code: Example Injectable**
22
22
  ```typescript
@@ -77,7 +77,7 @@ class SpecificService extends BaseService {
77
77
  }
78
78
  ```
79
79
 
80
- In this scenario, `SpecificService` is a valid candidate for `BaseService` due to the abstract inheritance. Sometimes, you may want to provide a slight variation to a dependency without extending a class. To this end, the [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L70) decorator denotes a `static` class method that produces an [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29).
80
+ In this scenario, `SpecificService` is a valid candidate for `BaseService` due to the abstract inheritance. Sometimes, you may want to provide a slight variation to a dependency without extending a class. To this end, the [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L47) decorator denotes a `static` class method that produces an [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15).
81
81
 
82
82
  **Code: Example InjectableFactory**
83
83
  ```typescript
@@ -125,12 +125,12 @@ class RuntimeService {
125
125
 
126
126
  In this example, the enabled flag is specified in relationship to the deployment environment. When coupled with optional properties, and optional chaining, allows for seamless inclusion of optional dependencies at runtime.
127
127
 
128
- **Note**: Other modules are able to provide aliases to [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) that also provide additional functionality. For example, the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support") module @Config or the [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative api for Web Applications with support for the dependency injection.") module @Controller decorator registers the associated class as an injectable element.
128
+ **Note**: Other modules are able to provide aliases to [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) that also provide additional functionality. For example, the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support") module @Config or the [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") module @Controller decorator registers the associated class as an injectable element.
129
129
 
130
130
  ## Injection
131
- Once all of your necessary dependencies are defined, now is the time to provide those [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) instances to your code. There are three primary methods for injection:
131
+ Once all of your necessary dependencies are defined, now is the time to provide those [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) instances to your code. There are three primary methods for injection:
132
132
 
133
- The [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) decorator, which denotes a desire to inject a value directly. These will be set post construction.
133
+ The [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) decorator, which denotes a desire to inject a value directly. These will be set post construction.
134
134
 
135
135
  **Code: Example Injectable with dependencies as Inject fields**
136
136
  ```typescript
@@ -148,7 +148,7 @@ class CustomService {
148
148
  }
149
149
  ```
150
150
 
151
- The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) constructor params, which will be provided as the instance is being constructed.
151
+ The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) constructor params, which will be provided as the instance is being constructed.
152
152
 
153
153
  **Code: Example Injectable with dependencies in constructor**
154
154
  ```typescript
@@ -170,7 +170,7 @@ class CustomService {
170
170
  }
171
171
  ```
172
172
 
173
- Via [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L70) params, which are comparable to constructor params
173
+ Via [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L47) params, which are comparable to constructor params
174
174
 
175
175
  **Code: Example InjectableFactory with parameters as dependencies**
176
176
  ```typescript
@@ -228,9 +228,9 @@ class Config {
228
228
  ```
229
229
 
230
230
  ## Non-Framework Dependencies
231
- The module is built around the framework's management of class registration, and being able to decorate the code with [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) decorators. There may also be a desire to leverage external code and pull it into the dependency injection framework. This could easily be achieved using a wrapper class that is owned by the framework.
231
+ The module is built around the framework's management of class registration, and being able to decorate the code with [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) decorators. There may also be a desire to leverage external code and pull it into the dependency injection framework. This could easily be achieved using a wrapper class that is owned by the framework.
232
232
 
233
- It is also possible to directly reference external types, and they will be converted into unique symbols. These symbols cannot be used manually, but can be leveraged using [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L29) decorators.
233
+ It is also possible to directly reference external types, and they will be converted into unique symbols. These symbols cannot be used manually, but can be leveraged using [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L15) decorators.
234
234
 
235
235
  **Code: Example External Dependencies**
236
236
  ```typescript
@@ -268,18 +268,18 @@ class Service {
268
268
  ```
269
269
 
270
270
  ## Manual Invocation
271
- Some times you will need to lookup a dependency dynamically, or you want to control the injection process at a more granular level. To achieve that you will need to directly access the [DependencyRegistry](https://github.com/travetto/travetto/tree/main/module/di/src/registry.ts#L25). The registry allows for requesting a dependency by class reference:
271
+ Some times you will need to lookup a dependency dynamically, or you want to control the injection process at a more granular level. To achieve that you will need to directly access the [DependencyRegistryIndex](https://github.com/travetto/travetto/tree/main/module/di/src/registry/registry-index.ts#L21). The registry allows for requesting a dependency by class reference:
272
272
 
273
273
  **Code: Example of Manual Lookup**
274
274
  ```typescript
275
- import { Injectable, DependencyRegistry } from '@travetto/di';
275
+ import { Injectable, DependencyRegistryIndex } from '@travetto/di';
276
276
 
277
277
  @Injectable()
278
278
  class Complex { }
279
279
 
280
280
  class ManualLookup {
281
281
  async invoke() {
282
- const complex = await DependencyRegistry.getInstance(Complex);
282
+ const complex = await DependencyRegistryIndex.getInstance(Complex);
283
283
  return complex;
284
284
  }
285
285
  }
@@ -289,7 +289,7 @@ Additionally, support for interfaces (over class inheritance) is provided, but r
289
289
 
290
290
  **Code: Example Interface Injection**
291
291
  ```typescript
292
- import { DependencyRegistry, Inject, Injectable, InjectableFactory } from '@travetto/di';
292
+ import { DependencyRegistryIndex, Inject, Injectable, InjectableFactory } from '@travetto/di';
293
293
  import { toConcrete } from '@travetto/runtime';
294
294
 
295
295
  /**
@@ -315,7 +315,7 @@ class SpecificService {
315
315
  class ManualInvocationOfInterface {
316
316
  @InjectableFactory()
317
317
  static getCustomService(): Promise<ServiceContract> {
318
- return DependencyRegistry.getInstance<ServiceContract>(toConcrete<ServiceContract>());
318
+ return DependencyRegistryIndex.getInstance(toConcrete<ServiceContract>());
319
319
  }
320
320
  }
321
321
  ```
package/__index__.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './src/decorator.ts';
2
2
  export * from './src/error.ts';
3
- export * from './src/registry.ts';
3
+ export * from './src/registry/registry-index.ts';
4
+ export * from './src/registry/registry-adapter.ts';
4
5
  export * from './src/types.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/di",
3
- "version": "6.0.0",
3
+ "version": "7.0.0-rc.0",
4
4
  "description": "Dependency registration/management and injection support.",
5
5
  "keywords": [
6
6
  "ast-transformations",
@@ -27,10 +27,10 @@
27
27
  "directory": "module/di"
28
28
  },
29
29
  "dependencies": {
30
- "@travetto/registry": "^6.0.0"
30
+ "@travetto/registry": "^7.0.0-rc.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^6.0.0"
33
+ "@travetto/transformer": "^7.0.0-rc.0"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
package/src/decorator.ts CHANGED
@@ -1,80 +1,53 @@
1
- import { asConstructable, asFull, TypedFunction, type Class } from '@travetto/runtime';
1
+ import { Any, castTo, ClassInstance, getClass, type Class } from '@travetto/runtime';
2
+ import { CONSTRUCTOR_PROPERTY } from '@travetto/schema';
2
3
 
3
- import { InjectableFactoryConfig, InjectableConfig, Dependency } from './types.ts';
4
- import { DependencyRegistry, ResolutionType } from './registry.ts';
4
+ import { InjectableCandidate, ResolutionType } from './types.ts';
5
+ import { DependencyRegistryIndex } from './registry/registry-index.ts';
5
6
 
6
- function collapseConfig<T extends { qualifier?: symbol }>(...args: (symbol | Partial<InjectConfig> | undefined)[]): T {
7
- let out: Partial<T> = {};
8
- if (args) {
9
- if (Array.isArray(args)) {
10
- for (const arg of args) {
11
- if (typeof arg === 'symbol') {
12
- out.qualifier = arg;
13
- } else if (arg) {
14
- Object.assign(out, arg);
15
- }
16
- }
17
- } else {
18
- out = args;
19
- }
20
- }
21
- return asFull(out);
22
- }
7
+ const fromArg = <T extends { qualifier?: symbol }>(arg?: T | symbol): T =>
8
+ typeof arg === 'symbol' ? castTo({ qualifier: arg }) : (arg ?? castTo<T>({}));
23
9
 
24
10
  /**
25
11
  * Indicate that a class is able to be injected
26
- *
27
- * @augments `@travetto/di:Injectable`
12
+ * @augments `@travetto/schema:Schema`
13
+ * @kind decorator
28
14
  */
29
- export function Injectable(first?: Partial<InjectableConfig> | symbol, ...args: (Partial<InjectableConfig> | undefined)[]) {
30
- return <T extends Class>(target: T): T => {
31
- const config = {
32
- ...collapseConfig<Partial<InjectableConfig>>(first, ...args),
33
- class: target
34
- };
35
- DependencyRegistry.registerClass(target, config);
36
- return target;
15
+ export function Injectable(config?: Partial<InjectableCandidate> | symbol) {
16
+ return <T extends Class>(cls: T): void => {
17
+ DependencyRegistryIndex.getForRegister(cls).registerClass(fromArg(config));
37
18
  };
38
19
  }
39
20
 
40
- export type InjectConfig = { qualifier?: symbol, optional?: boolean, resolution?: ResolutionType };
41
-
42
- export function InjectArgs(configs?: InjectConfig[][]) {
43
- return <T extends Class>(target: T): void => {
44
- DependencyRegistry.registerConstructor(target, configs?.map(x => collapseConfig(...x)));
45
- };
46
- }
21
+ export type InjectConfig = { qualifier?: symbol, resolution?: ResolutionType };
47
22
 
48
23
  /**
49
- * Indicate that a field is able to be injected
50
- *
24
+ * Indicate that a field or parameter is able to be injected
51
25
  * @augments `@travetto/di:Inject`
26
+ * @augments `@travetto/schema:Input`
27
+ * @kind decorator
52
28
  */
53
- export function Inject(first?: InjectConfig | symbol, ...args: (InjectConfig | undefined)[]) {
54
- return (target: unknown, propertyKey?: string, idx?: number | PropertyDescriptor): void => {
55
- if (typeof idx !== 'number') { // Only register if on property
56
- const config = collapseConfig<Dependency>(first, ...args);
57
-
58
- DependencyRegistry.registerProperty(
59
- asConstructable(target).constructor, propertyKey!, config
60
- );
29
+ export function Inject(config?: InjectConfig | symbol) {
30
+ return (instanceOrCls: Class | ClassInstance, property?: string | symbol, idx?: number | PropertyDescriptor): void => {
31
+ const cfg = fromArg(config);
32
+ const cls = getClass(instanceOrCls);
33
+ const propertyKey = property ?? CONSTRUCTOR_PROPERTY;
34
+ if (typeof idx !== 'number') {
35
+ DependencyRegistryIndex.registerFieldMetadata(cls, propertyKey, cfg);
36
+ } else {
37
+ DependencyRegistryIndex.registerParameterMetadata(cls, propertyKey, idx, cfg);
61
38
  }
62
39
  };
63
40
  }
64
41
 
65
42
  /**
66
43
  * Identifies a static method that is able to produce a dependency
67
- *
68
- * @augments `@travetto/di:InjectableFactory`
44
+ * @augments `@travetto/schema:Method`
45
+ * @kind decorator
69
46
  */
70
- export function InjectableFactory(first?: Partial<InjectableFactoryConfig> | symbol, ...args: (Partial<InjectableFactoryConfig> | undefined)[]) {
71
- return <T extends Class>(target: T, property: string | symbol, descriptor: TypedPropertyDescriptor<TypedFunction>): void => {
72
- const config: InjectableFactoryConfig = collapseConfig(first, ...args);
73
- DependencyRegistry.registerFactory({
74
- ...config,
75
- dependencies: config.dependencies?.map(x => Array.isArray(x) ? collapseConfig(...x) : collapseConfig(x)),
76
- fn: descriptor.value!,
77
- id: `${target.Ⲑid}#${property.toString()}`
47
+ export function InjectableFactory(config?: Partial<InjectableCandidate> | symbol) {
48
+ return <T extends Class>(cls: T, property: string | symbol, descriptor: TypedPropertyDescriptor<(...args: Any[]) => Any>): void => {
49
+ DependencyRegistryIndex.getForRegister(cls).registerFactory(property, fromArg(config), {
50
+ factory: (...params: unknown[]) => descriptor.value!.apply(cls, params),
78
51
  });
79
52
  };
80
53
  }
package/src/error.ts CHANGED
@@ -1,12 +1,11 @@
1
- import { AppError } from '@travetto/runtime';
2
- import { ClassTarget } from './types.ts';
1
+ import { AppError, Class } from '@travetto/runtime';
3
2
 
4
3
  function getName(symbol: symbol): string {
5
4
  return symbol.toString().split(/[()]/g)[1];
6
5
  }
7
6
 
8
7
  export class InjectionError extends AppError {
9
- constructor(message: string, target: ClassTarget, qualifiers?: symbol[]) {
8
+ constructor(message: string, target: Class, qualifiers?: symbol[]) {
10
9
  super(`${message}: [${target.Ⲑid}]${qualifiers ? `[${qualifiers.map(getName)}]` : ''}`, {
11
10
  category: 'notfound',
12
11
  details: {
@@ -0,0 +1,79 @@
1
+ import { RegistryAdapter } from '@travetto/registry';
2
+ import { Class, classConstruct, describeFunction, getAllEntries, safeAssign } from '@travetto/runtime';
3
+ import { CONSTRUCTOR_PROPERTY, SchemaRegistryIndex } from '@travetto/schema';
4
+
5
+ import { InjectableConfig, getDefaultQualifier, InjectableCandidate } from '../types';
6
+
7
+ function combineInjectableCandidates<T extends InjectableCandidate>(base: T, ...override: Partial<T>[]): typeof base {
8
+ for (const o of override) {
9
+ safeAssign(base, o);
10
+ }
11
+ return base;
12
+ }
13
+
14
+ function combineClasses<T extends InjectableConfig>(base: T, ...override: Partial<T>[]): typeof base {
15
+ for (const o of override) {
16
+ Object.assign(base, {
17
+ ...base,
18
+ ...o,
19
+ candidates: {
20
+ ...base.candidates,
21
+ ...o.candidates,
22
+ }
23
+ });
24
+ }
25
+ return base;
26
+ }
27
+
28
+ export class DependencyRegistryAdapter implements RegistryAdapter<InjectableConfig> {
29
+ #cls: Class;
30
+ #config: InjectableConfig;
31
+
32
+ constructor(cls: Class) {
33
+ this.#cls = cls;
34
+ }
35
+
36
+ register(...data: Partial<InjectableConfig<unknown>>[]): InjectableConfig<unknown> {
37
+ this.#config ??= { class: this.#cls, candidates: {} };
38
+ return combineClasses(this.#config, ...data);
39
+ }
40
+
41
+ registerFactory(method: string | symbol, ...data: Partial<InjectableCandidate<unknown>>[]): InjectableCandidate {
42
+ const { candidates } = this.register();
43
+ candidates[method] ??= {
44
+ class: this.#cls,
45
+ method,
46
+ enabled: true,
47
+ factory: undefined!,
48
+ candidateType: undefined!,
49
+ };
50
+ return combineInjectableCandidates(candidates[method], ...data);
51
+ }
52
+
53
+ registerClass(...data: Partial<InjectableCandidate<unknown>>[]): InjectableCandidate {
54
+ return this.registerFactory(CONSTRUCTOR_PROPERTY, ...data, {
55
+ factory: (...args: unknown[]) => classConstruct(this.#cls, args),
56
+ candidateType: this.#cls,
57
+ });
58
+ }
59
+
60
+ get(): InjectableConfig<unknown> {
61
+ return this.#config;
62
+ }
63
+
64
+ finalize(): void {
65
+ for (const [k] of getAllEntries(this.#config.candidates)) {
66
+ const v = this.#config.candidates[k];
67
+ const candidateType = SchemaRegistryIndex.get(v.class).getMethodReturnType(k);
68
+ v.candidateType = candidateType;
69
+ v.qualifier ??= getDefaultQualifier(candidateType);
70
+ }
71
+ }
72
+
73
+ getCandidateConfigs(): InjectableCandidate[] {
74
+ const entries = getAllEntries(this.#config.candidates).map(([_, item]) => item);
75
+ return entries
76
+ .filter(item => (item.enabled ?? true) === true || (typeof item.enabled === 'function' && item.enabled()))
77
+ .filter(item => item.method !== CONSTRUCTOR_PROPERTY || !describeFunction(item.candidateType)?.abstract);
78
+ }
79
+ }
@@ -0,0 +1,288 @@
1
+ import { ChangeEvent, RegistryIndex, RegistryIndexStore, Registry, RetargettingProxy } from '@travetto/registry';
2
+ import { AppError, castKey, castTo, Class, describeFunction, getParentClass, hasFunction, Runtime, TypedObject, Util } from '@travetto/runtime';
3
+ import { SchemaFieldConfig, SchemaParameterConfig, SchemaRegistryIndex } from '@travetto/schema';
4
+
5
+ import { Dependency, InjectableCandidate, InjectableClassMetadata, InjectableConfig, ResolutionType } from '../types';
6
+ import { DependencyRegistryAdapter } from './registry-adapter';
7
+ import { InjectionError } from '../error';
8
+ import { DependencyRegistryResolver } from './registry-resolver';
9
+
10
+ const MetadataSymbol = Symbol();
11
+
12
+ type ClassId = string;
13
+ const hasPostConstruct = hasFunction<{ postConstruct: () => Promise<unknown> }>('postConstruct');
14
+ const hasPreDestroy = hasFunction<{ preDestroy: () => Promise<unknown> }>('preDestroy');
15
+
16
+
17
+ function readMetadata(item: { metadata?: Record<symbol, unknown> }): Dependency | undefined {
18
+ return castTo<Dependency | undefined>(item.metadata?.[MetadataSymbol]);
19
+ }
20
+
21
+ export class DependencyRegistryIndex implements RegistryIndex {
22
+
23
+ static #instance = Registry.registerIndex(DependencyRegistryIndex);
24
+
25
+ static getForRegister(cls: Class): DependencyRegistryAdapter {
26
+ return this.#instance.store.getForRegister(cls);
27
+ }
28
+
29
+ static getInstance<T>(candidateType: Class<T>, qualifier?: symbol, resolution?: ResolutionType): Promise<T> {
30
+ return this.#instance.getInstance(candidateType, qualifier, resolution);
31
+ }
32
+
33
+ static getCandidates<T>(candidateType: Class<T>): InjectableCandidate<T>[] {
34
+ return this.#instance.getCandidates<T>(candidateType);
35
+ }
36
+
37
+ static getCandidateTypes<T>(candidateType: Class<T>): Class<T>[] {
38
+ return this.#instance.getCandidates(candidateType).map(c => c.candidateType);
39
+ }
40
+
41
+ static getInstances<T>(candidateType: Class<T>, predicate?: (cfg: InjectableCandidate<T>) => boolean): Promise<T[]> {
42
+ return this.#instance.getInstances<T>(candidateType, predicate);
43
+ }
44
+
45
+ static injectFields<T extends { constructor: Class<T> }>(o: T, cls = o.constructor): Promise<T> {
46
+ return this.#instance.injectFields(cls, o, cls);
47
+ }
48
+
49
+ static getOptional(cls: Class): InjectableConfig | undefined {
50
+ return this.#instance.store.getOptional(cls)?.get();
51
+ }
52
+
53
+ static registerClassMetadata(cls: Class, metadata: InjectableClassMetadata): void {
54
+ SchemaRegistryIndex.getForRegister(cls).registerMetadata<InjectableClassMetadata>(MetadataSymbol, metadata);
55
+ }
56
+
57
+ static registerParameterMetadata(cls: Class, method: string | symbol, index: number, metadata: Dependency): void {
58
+ SchemaRegistryIndex.getForRegister(cls).registerParameterMetadata(method, index, MetadataSymbol, metadata);
59
+ }
60
+
61
+ static registerFieldMetadata(cls: Class, field: string | symbol, metadata: Dependency): void {
62
+ SchemaRegistryIndex.getForRegister(cls).registerFieldMetadata(field, MetadataSymbol, metadata);
63
+ }
64
+
65
+ #instances = new Map<ClassId, Map<symbol, unknown>>();
66
+ #instancePromises = new Map<ClassId, Map<symbol, Promise<unknown>>>();
67
+ #proxies = new Map<ClassId, Map<symbol | undefined, RetargettingProxy<unknown>>>();
68
+ #resolver = new DependencyRegistryResolver();
69
+
70
+ #proxyInstance<T>(target: Class<unknown>, qualifier: symbol, instance: T): T {
71
+ const classId = target.Ⲑid;
72
+ let proxy: RetargettingProxy<unknown>;
73
+
74
+ if (!this.#proxies.has(classId)) {
75
+ this.#proxies.set(classId, new Map());
76
+ }
77
+
78
+ if (!this.#proxies.get(classId)!.has(qualifier)) {
79
+ proxy = new RetargettingProxy(instance);
80
+ this.#proxies.get(classId)!.set(qualifier, proxy);
81
+ console.debug('Registering proxy', { id: target.Ⲑid, qualifier: qualifier.toString() });
82
+ } else {
83
+ proxy = this.#proxies.get(classId)!.get(qualifier)!;
84
+ proxy.setTarget(instance);
85
+ console.debug('Updating target', {
86
+ id: target.Ⲑid, qualifier: qualifier.toString(), instanceType: target.name
87
+ });
88
+ }
89
+
90
+ return proxy.get();
91
+ }
92
+
93
+ #addClass(cls: Class): void {
94
+ const adapter = this.store.get(cls);
95
+
96
+ for (const config of adapter.getCandidateConfigs()) {
97
+ const parentClass = getParentClass(config.candidateType);
98
+ const parentConfig = parentClass ? this.store.getOptional(parentClass) : undefined;
99
+ const hasParentBase = (parentConfig || (parentClass && !!describeFunction(parentClass)?.abstract));
100
+ const baseParentId = hasParentBase ? parentClass?.Ⲑid : undefined;
101
+ this.#resolver.registerClass(config, baseParentId);
102
+ if (config.autoInject) {
103
+ // Don't wait
104
+ this.getInstance(config.candidateType, config.qualifier);
105
+ }
106
+ }
107
+ }
108
+
109
+ #changedClass(cls: Class, _prev: Class): void {
110
+ // Reload instances
111
+ for (const qualifier of this.#proxies.get(cls.Ⲑid)?.keys() ?? []) {
112
+ // Timing matters due to create instance being asynchronous
113
+ Util.queueMacroTask().then(() => { this.getInstance(cls, qualifier); });
114
+ }
115
+ }
116
+
117
+ #removeClass(cls: Class): void {
118
+ const classId = cls.Ⲑid;
119
+
120
+ if (this.#instances.has(classId)) {
121
+ for (const [qualifier, config] of this.#resolver.getContainerEntries(cls)) {
122
+ this.destroyInstance(config.candidateType, qualifier);
123
+ }
124
+ }
125
+ }
126
+
127
+
128
+ async #resolveDependencyValue(dependency: Dependency, input: SchemaFieldConfig | SchemaParameterConfig, src: Class): Promise<unknown> {
129
+ try {
130
+ const target = dependency.target ?? input.type;
131
+ return await this.getInstance(target, dependency.qualifier, dependency.resolution);
132
+ } catch (err) {
133
+ if (input.required?.active === false && err instanceof InjectionError && err.category === 'notfound') {
134
+ return undefined;
135
+ } else {
136
+ if (err && err instanceof Error) {
137
+ err.message = `${err.message} via=${src.Ⲑid}[${input.name?.toString() ?? 'constructor'}]`;
138
+ }
139
+ throw err;
140
+ }
141
+ }
142
+ }
143
+
144
+ store = new RegistryIndexStore(DependencyRegistryAdapter);
145
+
146
+ getConfig(cls: Class): InjectableConfig {
147
+ return this.store.get(cls).get();
148
+ }
149
+
150
+ process(events: ChangeEvent<Class>[]): void {
151
+ for (const ev of events) {
152
+ if (ev.type === 'added') {
153
+ this.#addClass(ev.curr);
154
+ } else if (ev.type === 'removing') {
155
+ this.#removeClass(ev.prev);
156
+ } else if (ev.type === 'changed') {
157
+ this.#changedClass(ev.curr, ev.prev);
158
+ }
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Get all available candidates for a given type
164
+ */
165
+ getCandidates<T>(candidateType: Class<T>): InjectableCandidate<T>[] {
166
+ return this.#resolver.getCandidateEntries(candidateType).map(([_, x]) => castTo<InjectableCandidate<T>>(x));
167
+ }
168
+
169
+ /**
170
+ * Get candidate instances by target type, with an optional filter
171
+ */
172
+ getInstances<T>(candidateType: Class<T>, predicate?: (cfg: InjectableCandidate<T>) => boolean): Promise<T[]> {
173
+ const inputs = this.getCandidates<T>(candidateType).filter(x => !predicate || predicate(x));
174
+ return Promise.all(inputs.map(l => this.getInstance<T>(l.class, l.qualifier)));
175
+ }
176
+
177
+ /**
178
+ * Retrieve list dependencies
179
+ */
180
+ async fetchDependencyParameters<T>(candidate: InjectableCandidate<T>): Promise<unknown[]> {
181
+ const inputs = SchemaRegistryIndex.has(candidate.class) ?
182
+ SchemaRegistryIndex.getMethodConfig(candidate.class, candidate.method).parameters : [];
183
+
184
+ const promises = inputs
185
+ .map(input => this.#resolveDependencyValue(readMetadata(input) ?? {}, input, candidate.class));
186
+
187
+ return await Promise.all(promises);
188
+ }
189
+
190
+ /**
191
+ * Retrieve mapped dependencies
192
+ */
193
+ async injectFields<T>(candidateType: Class, instance: T, srcClass: Class): Promise<T> {
194
+ const inputs = SchemaRegistryIndex.has(candidateType) ? SchemaRegistryIndex.getFieldMap(candidateType) : {};
195
+
196
+ const promises = TypedObject.entries(inputs)
197
+ .filter(([k, input]) => readMetadata(input) !== undefined && (input.access !== 'readonly' && instance[castKey(k)] === undefined))
198
+ .map(async ([k, input]) => [k, await this.#resolveDependencyValue(readMetadata(input) ?? {}, input, srcClass)] as const);
199
+
200
+ const pairs = await Promise.all(promises);
201
+
202
+ for (const [k, v] of pairs) {
203
+ instance[castKey(k)] = castTo(v);
204
+ }
205
+ return instance;
206
+ }
207
+
208
+ /**
209
+ * Actually construct an instance while resolving the dependencies
210
+ */
211
+ async construct<T>(candidateType: Class<T>, qualifier: symbol): Promise<T> {
212
+ const { candidate } = this.#resolver.resolveCandidate(candidateType, qualifier);
213
+ const targetType = candidate.candidateType;
214
+ const params = await this.fetchDependencyParameters(candidate);
215
+ const inst = await candidate.factory(...params);
216
+
217
+ // And auto-wire fields
218
+ await this.injectFields(targetType, inst, candidate.class);
219
+
220
+ // Run post construct, if it wasn't passed in, otherwise it was already created
221
+ if (hasPostConstruct(inst) && !params.includes(inst)) {
222
+ await inst.postConstruct();
223
+ }
224
+
225
+ const metadata = SchemaRegistryIndex.has(targetType) ?
226
+ SchemaRegistryIndex.get(targetType).getMetadata<InjectableClassMetadata>(MetadataSymbol) : undefined;
227
+
228
+ // Run post constructors
229
+ for (const op of Object.values(metadata?.postConstruct ?? {})) {
230
+ await op(inst);
231
+ }
232
+
233
+ // Proxy if necessary
234
+ return Runtime.dynamic ? this.#proxyInstance(targetType, qualifier, inst) : inst;
235
+ }
236
+
237
+ /**
238
+ * Get or create the instance
239
+ */
240
+ async getInstance<T>(candidateType: Class<T>, requestedQualifier?: symbol, resolution?: ResolutionType): Promise<T> {
241
+ if (!candidateType) {
242
+ throw new AppError('Unable to get instance when target is undefined');
243
+ }
244
+
245
+ const { targetId, qualifier } = this.#resolver.resolveCandidate(candidateType, requestedQualifier, resolution);
246
+
247
+ if (!this.#instances.has(targetId)) {
248
+ this.#instances.set(targetId, new Map());
249
+ this.#instancePromises.set(targetId, new Map());
250
+ }
251
+
252
+ if (this.#instancePromises.get(targetId)!.has(qualifier)) {
253
+ return castTo(this.#instancePromises.get(targetId)!.get(qualifier));
254
+ }
255
+
256
+ const instancePromise = this.construct(candidateType, qualifier);
257
+ this.#instancePromises.get(targetId)!.set(qualifier, instancePromise);
258
+ try {
259
+ const instance = await instancePromise;
260
+ this.#instances.get(targetId)!.set(qualifier, instance);
261
+ return instance;
262
+ } catch (err) {
263
+ // Clear it out, don't save failed constructions
264
+ this.#instancePromises.get(targetId)!.delete(qualifier);
265
+ throw err;
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Destroy an instance
271
+ */
272
+ destroyInstance(candidateType: Class, requestedQualifier: symbol): void {
273
+ const { targetId, qualifier } = this.#resolver.resolveCandidate(candidateType, requestedQualifier);
274
+
275
+ const activeInstance = this.#instances.get(targetId)!.get(qualifier);
276
+ if (hasPreDestroy(activeInstance)) {
277
+ activeInstance.preDestroy();
278
+ }
279
+
280
+ this.#resolver.removeClass(candidateType, qualifier);
281
+ this.#instances.get(targetId)!.delete(qualifier);
282
+ this.#instancePromises.get(targetId)!.delete(qualifier);
283
+
284
+ // May not exist
285
+ this.#proxies.get(targetId)?.get(qualifier)?.setTarget(null);
286
+ console.debug('On uninstall', { id: targetId, qualifier: qualifier.toString(), classId: targetId });
287
+ }
288
+ }