@travetto/di 7.1.3 → 8.0.0-alpha.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/di",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Dependency registration/management and injection support.",
|
|
6
6
|
"keywords": [
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"directory": "module/di"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@travetto/registry": "^
|
|
31
|
+
"@travetto/registry": "^8.0.0-alpha.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@travetto/transformer": "^
|
|
34
|
+
"@travetto/transformer": "^8.0.0-alpha.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependenciesMeta": {
|
|
37
37
|
"@travetto/transformer": {
|
package/src/error.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RuntimeError, type Class } from '@travetto/runtime';
|
|
2
2
|
|
|
3
3
|
function getName(symbol: symbol): string {
|
|
4
4
|
return symbol.toString().split(/[()]/g)[1];
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
export class InjectionError extends
|
|
7
|
+
export class InjectionError extends RuntimeError {
|
|
8
8
|
constructor(message: string, target: Class, qualifiers?: symbol[]) {
|
|
9
9
|
super(`${message}: [${target.Ⲑid}]${qualifiers ? `[${qualifiers.map(getName)}]` : ''}`, {
|
|
10
10
|
category: 'notfound',
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type RegistryIndex, RegistryIndexStore, Registry } from '@travetto/registry';
|
|
2
|
-
import {
|
|
2
|
+
import { RuntimeError, castKey, castTo, type Class, describeFunction, getParentClass, hasFunction, TypedObject } from '@travetto/runtime';
|
|
3
3
|
import { type SchemaFieldConfig, type SchemaParameterConfig, SchemaRegistryIndex } from '@travetto/schema';
|
|
4
4
|
|
|
5
5
|
import type { Dependency, InjectableCandidate, InjectableClassMetadata, InjectableConfig, ResolutionType } from '../types.ts';
|
|
@@ -184,29 +184,27 @@ export class DependencyRegistryIndex implements RegistryIndex {
|
|
|
184
184
|
*/
|
|
185
185
|
async getInstance<T>(candidateType: Class<T>, requestedQualifier?: symbol, resolution?: ResolutionType): Promise<T> {
|
|
186
186
|
if (!candidateType) {
|
|
187
|
-
throw new
|
|
187
|
+
throw new RuntimeError('Unable to get instance when target is undefined');
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
const { target, qualifier } = this.#resolver.resolveCandidate(candidateType, requestedQualifier, resolution);
|
|
191
191
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
this.#instancePromises.set(target, new Map());
|
|
195
|
-
}
|
|
192
|
+
const instances = this.#instances.getOrInsert(target, new Map());
|
|
193
|
+
const instancePromises = this.#instancePromises.getOrInsert(target, new Map());
|
|
196
194
|
|
|
197
|
-
if (
|
|
198
|
-
return castTo(
|
|
195
|
+
if (instancePromises.has(qualifier)) {
|
|
196
|
+
return castTo(instancePromises.get(qualifier));
|
|
199
197
|
}
|
|
200
198
|
|
|
201
199
|
const instancePromise = this.construct(candidateType, qualifier);
|
|
202
|
-
|
|
200
|
+
instancePromises.set(qualifier, instancePromise);
|
|
203
201
|
try {
|
|
204
202
|
const instance = await instancePromise;
|
|
205
|
-
|
|
203
|
+
instances.set(qualifier, instance);
|
|
206
204
|
return instance;
|
|
207
205
|
} catch (error) {
|
|
208
206
|
// Clear it out, don't save failed constructions
|
|
209
|
-
|
|
207
|
+
instancePromises.delete(qualifier);
|
|
210
208
|
throw error;
|
|
211
209
|
}
|
|
212
210
|
}
|
|
@@ -7,10 +7,7 @@ import { InjectionError } from '../error.ts';
|
|
|
7
7
|
type Resolved<T> = { candidate: InjectableCandidate<T>, qualifier: symbol, target: Class };
|
|
8
8
|
|
|
9
9
|
function setInMap<T>(map: Map<Class, Map<typeof key, T>>, cls: Class, key: symbol | string, dest: T): void {
|
|
10
|
-
|
|
11
|
-
map.set(cls, new Map());
|
|
12
|
-
}
|
|
13
|
-
map.get(cls)!.set(key, dest);
|
|
10
|
+
map.getOrInsert(cls, new Map()).set(key, dest);
|
|
14
11
|
}
|
|
15
12
|
|
|
16
13
|
export class DependencyRegistryResolver {
|
package/support/test/suite.ts
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Class } from '@travetto/runtime';
|
|
2
2
|
import { Registry } from '@travetto/registry';
|
|
3
|
-
import { SuiteRegistryIndex } from '@travetto/test';
|
|
3
|
+
import { SuiteRegistryIndex, type SuitePhaseHandler } from '@travetto/test';
|
|
4
4
|
|
|
5
5
|
import { DependencyRegistryIndex } from '../../src/registry/registry-index.ts';
|
|
6
6
|
|
|
7
|
+
class ModelSuiteHandler<T extends object> implements SuitePhaseHandler<T> {
|
|
8
|
+
target: Class;
|
|
9
|
+
constructor(target: Class<T>) {
|
|
10
|
+
this.target = target;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async beforeEach(instance: T) {
|
|
14
|
+
await Registry.init();
|
|
15
|
+
await DependencyRegistryIndex.injectFields(instance, this.target);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
7
19
|
/**
|
|
8
20
|
* Registers a suite as injectable
|
|
9
21
|
* @kind decorator
|
|
@@ -11,12 +23,7 @@ import { DependencyRegistryIndex } from '../../src/registry/registry-index.ts';
|
|
|
11
23
|
export function InjectableSuite() {
|
|
12
24
|
return (cls: Class) => {
|
|
13
25
|
SuiteRegistryIndex.getForRegister(cls).register({
|
|
14
|
-
|
|
15
|
-
async function (this: unknown) {
|
|
16
|
-
await Registry.init();
|
|
17
|
-
await DependencyRegistryIndex.injectFields(this, cls);
|
|
18
|
-
},
|
|
19
|
-
]
|
|
26
|
+
phaseHandlers: [new ModelSuiteHandler(cls)]
|
|
20
27
|
});
|
|
21
28
|
};
|
|
22
29
|
}
|