@travetto/di 7.0.0-rc.2 → 7.0.0-rc.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/di",
3
- "version": "7.0.0-rc.2",
3
+ "version": "7.0.0-rc.4",
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": "^7.0.0-rc.2"
30
+ "@travetto/registry": "^7.0.0-rc.4"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^7.0.0-rc.2"
33
+ "@travetto/transformer": "^7.0.0-rc.3"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
@@ -1,5 +1,5 @@
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';
1
+ import { RegistryIndex, RegistryIndexStore, Registry } from '@travetto/registry';
2
+ import { AppError, castKey, castTo, Class, describeFunction, getParentClass, hasFunction, TypedObject } from '@travetto/runtime';
3
3
  import { SchemaFieldConfig, SchemaParameterConfig, SchemaRegistryIndex } from '@travetto/schema';
4
4
 
5
5
  import { Dependency, InjectableCandidate, InjectableClassMetadata, InjectableConfig, ResolutionType } from '../types';
@@ -32,10 +32,6 @@ export class DependencyRegistryIndex implements RegistryIndex {
32
32
  return this.#instance.getCandidates<T>(candidateType);
33
33
  }
34
34
 
35
- static getCandidateTypes<T>(candidateType: Class<T>): Class<T>[] {
36
- return this.#instance.getCandidates(candidateType).map(candidate => candidate.candidateType);
37
- }
38
-
39
35
  static getInstances<T>(candidateType: Class<T>, predicate?: (config: InjectableCandidate<T>) => boolean): Promise<T[]> {
40
36
  return this.#instance.getInstances<T>(candidateType, predicate);
41
37
  }
@@ -44,10 +40,6 @@ export class DependencyRegistryIndex implements RegistryIndex {
44
40
  return this.#instance.injectFields(cls, item, cls);
45
41
  }
46
42
 
47
- static getOptional(cls: Class): InjectableConfig | undefined {
48
- return this.#instance.store.getOptional(cls)?.get();
49
- }
50
-
51
43
  static registerClassMetadata(cls: Class, metadata: InjectableClassMetadata): void {
52
44
  SchemaRegistryIndex.getForRegister(cls).registerMetadata<InjectableClassMetadata>(MetadataSymbol, metadata);
53
45
  }
@@ -60,63 +52,10 @@ export class DependencyRegistryIndex implements RegistryIndex {
60
52
  SchemaRegistryIndex.getForRegister(cls).registerFieldMetadata(field, MetadataSymbol, metadata);
61
53
  }
62
54
 
63
- #proxies = new Map<string, Map<symbol | undefined, RetargettingProxy<unknown>>>();
64
55
  #instances = new Map<Class, Map<symbol, unknown>>();
65
56
  #instancePromises = new Map<Class, Map<symbol, Promise<unknown>>>();
66
57
  #resolver = new DependencyRegistryResolver();
67
58
 
68
- #proxyInstance<T>(target: Class<unknown>, qualifier: symbol, instance: T): T {
69
- let proxy: RetargettingProxy<unknown>;
70
- const targetId = target.Ⲑid;
71
-
72
- if (!this.#proxies.has(targetId)) {
73
- this.#proxies.set(targetId, new Map());
74
- }
75
-
76
- if (!this.#proxies.get(targetId)!.has(qualifier)) {
77
- proxy = new RetargettingProxy(instance);
78
- this.#proxies.get(targetId)!.set(qualifier, proxy);
79
- console.debug('Registering proxy', { id: target.Ⲑid, qualifier: qualifier.toString() });
80
- } else {
81
- proxy = this.#proxies.get(targetId)!.get(qualifier)!;
82
- proxy.setTarget(instance);
83
- console.debug('Updating target', {
84
- id: target.Ⲑid, qualifier: qualifier.toString(), instanceType: target.name
85
- });
86
- }
87
-
88
- return proxy.get();
89
- }
90
-
91
- #addClass(cls: Class, forceCreate: boolean = false): void {
92
- const adapter = this.store.get(cls);
93
-
94
- for (const config of adapter.getCandidateConfigs()) {
95
- const parentClass = getParentClass(config.candidateType);
96
- const parentConfig = parentClass ? this.store.getOptional(parentClass) : undefined;
97
- const hasParentBase = (parentConfig || (parentClass && !!describeFunction(parentClass)?.abstract));
98
- const baseParent = hasParentBase ? parentClass : undefined;
99
- this.#resolver.registerClass(config, baseParent);
100
- if (config.autoInject || forceCreate) {
101
- // Don't wait
102
- Util.queueMacroTask().then(() => {
103
- this.getInstance(config.candidateType, config.qualifier);
104
- });
105
- }
106
- }
107
- }
108
-
109
- #removeClass(cls: Class): void {
110
- if (this.#instances.has(cls)) {
111
- for (const [qualifier, config] of this.#resolver.getContainerEntries(cls)) {
112
- try {
113
- this.destroyInstance(config.candidateType, qualifier);
114
- } catch { }
115
- }
116
- }
117
- }
118
-
119
-
120
59
  async #resolveDependencyValue(dependency: Dependency, input: SchemaFieldConfig | SchemaParameterConfig, cls: Class): Promise<unknown> {
121
60
  try {
122
61
  const target = dependency.target ?? input.type;
@@ -135,17 +74,32 @@ export class DependencyRegistryIndex implements RegistryIndex {
135
74
 
136
75
  store = new RegistryIndexStore(DependencyRegistryAdapter);
137
76
 
77
+ /** @private */ constructor(source: unknown) { Registry.validateConstructor(source); }
78
+
138
79
  getConfig(cls: Class): InjectableConfig {
139
80
  return this.store.get(cls).get();
140
81
  }
141
82
 
142
- process(events: ChangeEvent<Class>[]): void {
143
- for (const event of events) {
144
- if ('previous' in event) {
145
- this.#removeClass(event.previous);
146
- }
147
- if ('current' in event) {
148
- this.#addClass(event.current, 'previous' in event);
83
+ onCreate(cls: Class): void {
84
+ const adapter = this.store.get(cls);
85
+
86
+ for (const config of adapter.getCandidateConfigs()) {
87
+ const parentClass = getParentClass(config.candidateType);
88
+ const parentConfig = parentClass ? this.store.getOptional(parentClass) : undefined;
89
+ const hasParentBase = (parentConfig || (parentClass && !!describeFunction(parentClass)?.abstract));
90
+ const baseParent = hasParentBase ? parentClass : undefined;
91
+ this.#resolver.registerClass(config, baseParent);
92
+ }
93
+ }
94
+
95
+ // Setup instances after change set complete
96
+ onChangeSetComplete(classes: Class[]): void {
97
+ for (const cls of classes) {
98
+ const adapter = this.store.get(cls);
99
+ for (const config of adapter.getCandidateConfigs()) {
100
+ if (config.autoInject) {
101
+ this.getInstance(config.candidateType, config.qualifier);
102
+ }
149
103
  }
150
104
  }
151
105
  }
@@ -222,7 +176,7 @@ export class DependencyRegistryIndex implements RegistryIndex {
222
176
  }
223
177
 
224
178
  // Proxy if necessary
225
- return Runtime.dynamic ? this.#proxyInstance(targetType, qualifier, inst) : inst;
179
+ return inst;
226
180
  }
227
181
 
228
182
  /**
@@ -273,7 +227,6 @@ export class DependencyRegistryIndex implements RegistryIndex {
273
227
  this.#instancePromises.get(target)?.delete(qualifier);
274
228
 
275
229
  // May not exist
276
- this.#proxies.get(target.Ⲑid)?.get(qualifier)?.setTarget(null);
277
230
  console.debug('On uninstall', { id: target, qualifier: qualifier.toString(), classId: target });
278
231
  }
279
232
  }