@tstdl/base 0.93.22 → 0.93.24
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/application/application.js +2 -2
- package/injector/injector.d.ts +2 -0
- package/injector/injector.js +17 -13
- package/injector/resolve-chain.d.ts +10 -6
- package/injector/resolve-chain.js +40 -20
- package/package.json +1 -1
- package/test6.js +3 -1
|
@@ -110,7 +110,7 @@ let Application = Application_1 = class Application {
|
|
|
110
110
|
async runModules(modules) {
|
|
111
111
|
const promises = modules.map(async (module) => {
|
|
112
112
|
try {
|
|
113
|
-
this.#logger.info(`Starting module ${module.name}
|
|
113
|
+
this.#logger.info(`Starting module ${module.name}.`);
|
|
114
114
|
await runInInjectionContext(this.#injector, async () => await module.run());
|
|
115
115
|
this.#logger.info(`Module ${module.name} stopped.`);
|
|
116
116
|
}
|
|
@@ -126,7 +126,7 @@ let Application = Application_1 = class Application {
|
|
|
126
126
|
if (module.state == ModuleState.Stopped) {
|
|
127
127
|
return;
|
|
128
128
|
}
|
|
129
|
-
this.#logger.info(`Stopping module ${module.name}
|
|
129
|
+
this.#logger.info(`Stopping module ${module.name}.`);
|
|
130
130
|
try {
|
|
131
131
|
await module.stop();
|
|
132
132
|
}
|
package/injector/injector.d.ts
CHANGED
|
@@ -36,7 +36,9 @@ export type ResolveManyReturnType<T extends ResolveManyItem<any, any>[]> = {
|
|
|
36
36
|
export type AddDisposeHandler = (handler: Disposable | AsyncDisposable | (() => any)) => void;
|
|
37
37
|
export declare class Injector implements AsyncDisposable {
|
|
38
38
|
#private;
|
|
39
|
+
readonly id: number;
|
|
39
40
|
readonly name: string;
|
|
41
|
+
readonly nameWithId: string;
|
|
40
42
|
get disposed(): boolean;
|
|
41
43
|
constructor(name: string, parent?: Injector | null);
|
|
42
44
|
/**
|
package/injector/injector.js
CHANGED
|
@@ -18,6 +18,7 @@ import { ResolveChain } from './resolve-chain.js';
|
|
|
18
18
|
import { ResolveError } from './resolve.error.js';
|
|
19
19
|
import { injectMetadataSymbol, injectableMetadataSymbol } from './symbols.js';
|
|
20
20
|
import { getTokenName } from './token.js';
|
|
21
|
+
let _id = 1;
|
|
21
22
|
export class Injector {
|
|
22
23
|
static #globalRegistrations = new Map();
|
|
23
24
|
#parent;
|
|
@@ -28,12 +29,15 @@ export class Injector {
|
|
|
28
29
|
#registrations = new Map();
|
|
29
30
|
#injectorScopedResolutions = new MultiKeyMap();
|
|
30
31
|
#addDisposeHandler;
|
|
32
|
+
id = _id++;
|
|
31
33
|
name;
|
|
34
|
+
nameWithId;
|
|
32
35
|
get disposed() {
|
|
33
36
|
return this.#disposableStack.disposed;
|
|
34
37
|
}
|
|
35
38
|
constructor(name, parent = null) {
|
|
36
39
|
this.name = name;
|
|
40
|
+
this.nameWithId = `${name}#${this.id}`;
|
|
37
41
|
this.#parent = parent;
|
|
38
42
|
this.register(Injector, { useValue: this });
|
|
39
43
|
this.register(CancellationSignal, { useValue: this.#disposeToken.signal });
|
|
@@ -171,7 +175,7 @@ export class Injector {
|
|
|
171
175
|
}
|
|
172
176
|
resolve(token, argument, options = {}) {
|
|
173
177
|
const context = newInternalResolveContext();
|
|
174
|
-
const value = this._resolve(token, argument, options, context, ResolveChain.startWith(token));
|
|
178
|
+
const value = this._resolve(token, argument, options, context, ResolveChain.startWith(this, token));
|
|
175
179
|
try {
|
|
176
180
|
postProcess(context);
|
|
177
181
|
context.$done.resolve();
|
|
@@ -187,7 +191,7 @@ export class Injector {
|
|
|
187
191
|
}
|
|
188
192
|
resolveAll(token, argument, options = {}) {
|
|
189
193
|
const context = newInternalResolveContext();
|
|
190
|
-
const values = this._resolveAll(token, argument, options, context, ResolveChain.startWith(token));
|
|
194
|
+
const values = this._resolveAll(token, argument, options, context, ResolveChain.startWith(this, token));
|
|
191
195
|
try {
|
|
192
196
|
postProcess(context);
|
|
193
197
|
context.$done.resolve();
|
|
@@ -218,7 +222,7 @@ export class Injector {
|
|
|
218
222
|
}
|
|
219
223
|
async resolveAsync(token, argument, options = {}) {
|
|
220
224
|
const context = newInternalResolveContext();
|
|
221
|
-
const value = this._resolve(token, argument, options, context, ResolveChain.startWith(token));
|
|
225
|
+
const value = this._resolve(token, argument, options, context, ResolveChain.startWith(this, token));
|
|
222
226
|
try {
|
|
223
227
|
await postProcessAsync(context);
|
|
224
228
|
context.$done.resolve();
|
|
@@ -234,7 +238,7 @@ export class Injector {
|
|
|
234
238
|
}
|
|
235
239
|
async resolveAllAsync(token, argument, options = {}) {
|
|
236
240
|
const context = newInternalResolveContext();
|
|
237
|
-
const values = this._resolveAll(token, argument, options, context, ResolveChain.startWith(token));
|
|
241
|
+
const values = this._resolveAll(token, argument, options, context, ResolveChain.startWith(this, token));
|
|
238
242
|
try {
|
|
239
243
|
await postProcessAsync(context);
|
|
240
244
|
context.$done.resolve();
|
|
@@ -265,8 +269,8 @@ export class Injector {
|
|
|
265
269
|
}
|
|
266
270
|
_resolveManyTokens(tokens, context) {
|
|
267
271
|
return tokens.map((token) => (isArray(token)
|
|
268
|
-
? this._resolve(token[0], token[1], token[2] ?? {}, context, ResolveChain.startWith(token[0]))
|
|
269
|
-
: this._resolve(token, undefined, {}, context, ResolveChain.startWith(token))));
|
|
272
|
+
? this._resolve(token[0], token[1], token[2] ?? {}, context, ResolveChain.startWith(this, token[0]))
|
|
273
|
+
: this._resolve(token, undefined, {}, context, ResolveChain.startWith(this, token))));
|
|
270
274
|
}
|
|
271
275
|
_resolve(token, argument, options, context, chain) {
|
|
272
276
|
this.assertNotDisposed();
|
|
@@ -416,9 +420,9 @@ export class Injector {
|
|
|
416
420
|
const arg = resolveArgument ?? provider.defaultArgument ?? provider.defaultArgumentProvider?.();
|
|
417
421
|
injectionContext.argument = arg;
|
|
418
422
|
if (provider.resolveAll == true) {
|
|
419
|
-
return this._resolveAll(innerToken, arg, options, context, chain.addToken(innerToken));
|
|
423
|
+
return this._resolveAll(innerToken, arg, options, context, chain.addToken(this, innerToken));
|
|
420
424
|
}
|
|
421
|
-
result = { value: this._resolve(innerToken, arg, options, context, chain.addToken(innerToken)) };
|
|
425
|
+
result = { value: this._resolve(innerToken, arg, options, context, chain.addToken(this, innerToken)) };
|
|
422
426
|
}
|
|
423
427
|
else if (isFactoryProvider(provider)) {
|
|
424
428
|
const arg = resolveArgument ?? provider.defaultArgument ?? provider.defaultArgumentProvider?.();
|
|
@@ -440,7 +444,7 @@ export class Injector {
|
|
|
440
444
|
}
|
|
441
445
|
}
|
|
442
446
|
resolveClassInjection(resolutionTag, context, constructor, metadata, resolveArgument, chain) {
|
|
443
|
-
const getChain = (injectToken) => chain.addParameter(constructor, metadata.index, injectToken);
|
|
447
|
+
const getChain = (injectToken) => chain.addParameter(this, constructor, metadata.index, injectToken);
|
|
444
448
|
const injectMetadata = metadata.data.tryGet(injectMetadataSymbol) ?? {};
|
|
445
449
|
const injectToken = (injectMetadata.injectToken ?? metadata.type);
|
|
446
450
|
if (isDefined(injectMetadata.injectArgumentMapper) && (!this.hasRegistration(injectToken) || isDefined(resolveArgument) || isUndefined(injectToken))) {
|
|
@@ -460,7 +464,7 @@ export class Injector {
|
|
|
460
464
|
return resolved;
|
|
461
465
|
}
|
|
462
466
|
resolveInjection(token, argument, options, context, injectIndex, chain) {
|
|
463
|
-
return this._resolve(token, argument, options, context, chain.addInject(token, injectIndex));
|
|
467
|
+
return this._resolve(token, argument, options, context, chain.addInject(this, token, injectIndex));
|
|
464
468
|
}
|
|
465
469
|
async resolveInjectionAsync(token, argument, options, context, injectIndex, chain) {
|
|
466
470
|
const value = this.resolveInjection(token, argument, options, context, injectIndex, chain);
|
|
@@ -468,7 +472,7 @@ export class Injector {
|
|
|
468
472
|
return value;
|
|
469
473
|
}
|
|
470
474
|
resolveInjectionAll(token, argument, options, context, injectIndex, chain) {
|
|
471
|
-
return this._resolveAll(token, argument, options, context, chain.addInject(token, injectIndex));
|
|
475
|
+
return this._resolveAll(token, argument, options, context, chain.addInject(this, token, injectIndex));
|
|
472
476
|
}
|
|
473
477
|
async resolveInjectionAllAsync(token, argument, options, context, injectIndex, chain) {
|
|
474
478
|
const values = this.resolveInjectionAll(token, argument, options, context, injectIndex, chain);
|
|
@@ -477,8 +481,8 @@ export class Injector {
|
|
|
477
481
|
}
|
|
478
482
|
getResolveContext(resolutionTag, resolveContext, chain) {
|
|
479
483
|
return {
|
|
480
|
-
resolve: (token, argument, options) => this._resolve(token, argument, options ?? {}, resolveContext, chain.addToken(token)),
|
|
481
|
-
resolveAll: (token, argument, options) => this._resolveAll(token, argument, options ?? {}, resolveContext, chain.addToken(token)),
|
|
484
|
+
resolve: (token, argument, options) => this._resolve(token, argument, options ?? {}, resolveContext, chain.addToken(this, token)),
|
|
485
|
+
resolveAll: (token, argument, options) => this._resolveAll(token, argument, options ?? {}, resolveContext, chain.addToken(this, token)),
|
|
482
486
|
cancellationSignal: this.#disposeToken,
|
|
483
487
|
addDisposeHandler: this.#addDisposeHandler,
|
|
484
488
|
get data() {
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { AbstractConstructor } from '../types/index.js';
|
|
2
|
+
import type { Injector } from './injector.js';
|
|
2
3
|
import type { InjectionToken } from './token.js';
|
|
3
4
|
export type ResolveChainNodeBase<Type extends string> = {
|
|
5
|
+
injector: Injector;
|
|
4
6
|
type: Type;
|
|
5
7
|
forwardRef?: true;
|
|
6
8
|
};
|
|
7
|
-
export type ResolveChainNode =
|
|
9
|
+
export type ResolveChainNode = {
|
|
10
|
+
type: 'ellipsis';
|
|
11
|
+
} | ResolveChainNodeBase<'token'> & {
|
|
8
12
|
token: InjectionToken;
|
|
9
13
|
} | ResolveChainNodeBase<'inject'> & {
|
|
10
14
|
token: InjectionToken;
|
|
@@ -23,11 +27,11 @@ export declare class ResolveChain {
|
|
|
23
27
|
get length(): number;
|
|
24
28
|
/** @deprecated for internal use only */
|
|
25
29
|
constructor(nodes: ResolveChainNode[]);
|
|
26
|
-
static startWith(token: InjectionToken): ResolveChain;
|
|
27
|
-
addToken(token: InjectionToken): ResolveChain;
|
|
28
|
-
addInject(token: InjectionToken, index: number): ResolveChain;
|
|
29
|
-
addParameter(constructor: AbstractConstructor, index: number, token: InjectionToken): ResolveChain;
|
|
30
|
-
addProperty(constructor: AbstractConstructor, property: PropertyKey, token: InjectionToken): ResolveChain;
|
|
30
|
+
static startWith(injector: Injector, token: InjectionToken): ResolveChain;
|
|
31
|
+
addToken(injector: Injector, token: InjectionToken): ResolveChain;
|
|
32
|
+
addInject(injector: Injector, token: InjectionToken, index: number): ResolveChain;
|
|
33
|
+
addParameter(injector: Injector, constructor: AbstractConstructor, index: number, token: InjectionToken): ResolveChain;
|
|
34
|
+
addProperty(injector: Injector, constructor: AbstractConstructor, property: PropertyKey, token: InjectionToken): ResolveChain;
|
|
31
35
|
markAsForwardRef(forwardToken: InjectionToken): ResolveChain;
|
|
32
36
|
format(truncate?: number): string;
|
|
33
37
|
truncate(tokenCount: number): ResolveChain;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { reflectionRegistry } from '../reflection/index.js';
|
|
2
2
|
import { assertDefinedPass, isDefined } from '../utils/type-guards.js';
|
|
3
|
+
import { start } from 'repl';
|
|
3
4
|
import { getTokenName } from './token.js';
|
|
4
5
|
export class ResolveChain {
|
|
5
6
|
nodes;
|
|
@@ -10,24 +11,24 @@ export class ResolveChain {
|
|
|
10
11
|
constructor(nodes) {
|
|
11
12
|
this.nodes = nodes;
|
|
12
13
|
}
|
|
13
|
-
static startWith(token) {
|
|
14
|
+
static startWith(injector, token) {
|
|
14
15
|
const chain = new ResolveChain([]);
|
|
15
|
-
return chain.addToken(token);
|
|
16
|
+
return chain.addToken(injector, token);
|
|
16
17
|
}
|
|
17
|
-
addToken(token) {
|
|
18
|
-
const node = { type: 'token', token };
|
|
18
|
+
addToken(injector, token) {
|
|
19
|
+
const node = { injector, type: 'token', token };
|
|
19
20
|
return new ResolveChain([...this.nodes, node]);
|
|
20
21
|
}
|
|
21
|
-
addInject(token, index) {
|
|
22
|
-
const node = { type: 'inject', token, index };
|
|
22
|
+
addInject(injector, token, index) {
|
|
23
|
+
const node = { injector, type: 'inject', token, index };
|
|
23
24
|
return new ResolveChain([...this.nodes, node]);
|
|
24
25
|
}
|
|
25
|
-
addParameter(constructor, index, token) {
|
|
26
|
-
const node = { type: 'parameter', constructor, index, token };
|
|
26
|
+
addParameter(injector, constructor, index, token) {
|
|
27
|
+
const node = { injector, type: 'parameter', constructor, index, token };
|
|
27
28
|
return new ResolveChain([...this.nodes, node]);
|
|
28
29
|
}
|
|
29
|
-
addProperty(constructor, property, token) {
|
|
30
|
-
const node = { type: 'property', constructor, property, token };
|
|
30
|
+
addProperty(injector, constructor, property, token) {
|
|
31
|
+
const node = { injector, type: 'property', constructor, property, token };
|
|
31
32
|
return new ResolveChain([...this.nodes, node]);
|
|
32
33
|
}
|
|
33
34
|
markAsForwardRef(forwardToken) {
|
|
@@ -41,16 +42,22 @@ export class ResolveChain {
|
|
|
41
42
|
if (chain.length < this.length) {
|
|
42
43
|
chainString += '\n [...]';
|
|
43
44
|
}
|
|
45
|
+
const longestInjectorName = Math.max(...chain.nodes.map((node) => (node.type == 'ellipsis') ? 0 : node.injector.nameWithId.length));
|
|
44
46
|
for (const node of chain.nodes) {
|
|
47
|
+
if (node.type == 'ellipsis') {
|
|
48
|
+
chainString += `\n ${'[...]'.padStart(longestInjectorName)}`;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const paddedInjectorName = node.injector.name.padStart(longestInjectorName);
|
|
45
52
|
const tokenName = getTokenName(node.token);
|
|
46
53
|
const forwardRefPrefix = node.forwardRef ? 'ForwardRef::' : '';
|
|
47
54
|
switch (node.type) {
|
|
48
55
|
case 'token':
|
|
49
|
-
chainString += `\n -> ${forwardRefPrefix}${tokenName}`;
|
|
56
|
+
chainString += `\n ${paddedInjectorName} -> ${forwardRefPrefix}${tokenName}`;
|
|
50
57
|
break;
|
|
51
58
|
case 'inject':
|
|
52
59
|
chainString += ` => inject(${forwardRefPrefix}${tokenName}) [${node.index + 1}]`;
|
|
53
|
-
chainString += `\n -> ${tokenName}`;
|
|
60
|
+
chainString += `\n ${paddedInjectorName} -> ${tokenName}`;
|
|
54
61
|
break;
|
|
55
62
|
case 'parameter':
|
|
56
63
|
const metadata = reflectionRegistry.getMetadata(node.constructor);
|
|
@@ -58,12 +65,12 @@ export class ResolveChain {
|
|
|
58
65
|
const suffix = ', _'.repeat(assertDefinedPass(metadata?.parameters, `missing parameters metadata for ${node.constructor.name}`).length - node.index - 1);
|
|
59
66
|
chainString += `(${prefix}${forwardRefPrefix}${tokenName}${suffix})`;
|
|
60
67
|
// chainString += `\n ${' '.repeat(1 + node.constructor.name.length + forwardRefPrefix.length + prefix.length + (tokenName.length / 2))}↓`;
|
|
61
|
-
chainString += `\n -> ${tokenName}`;
|
|
68
|
+
chainString += `\n ${paddedInjectorName} -> ${tokenName}`;
|
|
62
69
|
break;
|
|
63
70
|
case 'property':
|
|
64
71
|
const constructorName = getTokenName(node.constructor);
|
|
65
72
|
const key = getPropertyKeyString(node.property);
|
|
66
|
-
chainString += `\n -> ${constructorName}[${key}]: ${forwardRefPrefix}${tokenName}`;
|
|
73
|
+
chainString += `\n ${paddedInjectorName} -> ${constructorName}[${key}]: ${forwardRefPrefix}${tokenName}`;
|
|
67
74
|
break;
|
|
68
75
|
default:
|
|
69
76
|
throw new Error(`unknown chain node type ${node.type}`);
|
|
@@ -72,16 +79,29 @@ export class ResolveChain {
|
|
|
72
79
|
return chainString;
|
|
73
80
|
}
|
|
74
81
|
truncate(tokenCount) {
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
const startOfChain = [];
|
|
83
|
+
const endOfChain = [];
|
|
84
|
+
let startCounter = 0;
|
|
85
|
+
let index = 0;
|
|
86
|
+
for (index = 0; (index < this.nodes.length) && (startCounter < tokenCount); index++) {
|
|
87
|
+
const node = this.nodes[index];
|
|
88
|
+
startOfChain.push(node);
|
|
89
|
+
if (node.type == 'token') {
|
|
90
|
+
startCounter++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
let endCounter = 0;
|
|
94
|
+
for (let i = this.nodes.length - 1; (i > index) && (endCounter < tokenCount); i--) {
|
|
78
95
|
const node = this.nodes[i];
|
|
79
|
-
|
|
96
|
+
endOfChain.unshift(node);
|
|
80
97
|
if (node.type == 'token') {
|
|
81
|
-
|
|
98
|
+
endCounter++;
|
|
82
99
|
}
|
|
83
100
|
}
|
|
84
|
-
|
|
101
|
+
if (endOfChain.length > 0) {
|
|
102
|
+
startOfChain.push({ type: 'ellipsis' });
|
|
103
|
+
}
|
|
104
|
+
return new ResolveChain([...startOfChain, ...endOfChain]);
|
|
85
105
|
}
|
|
86
106
|
}
|
|
87
107
|
function getPropertyKeyString(key) {
|
package/package.json
CHANGED
package/test6.js
CHANGED
|
@@ -6,10 +6,12 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
6
6
|
};
|
|
7
7
|
import { Application } from './application/application.js';
|
|
8
8
|
import { provideModule, provideSignalHandler } from './application/providers.js';
|
|
9
|
+
import { NotImplementedError } from './errors/not-implemented.error.js';
|
|
9
10
|
import { Singleton } from './injector/decorators.js';
|
|
10
11
|
import { inject } from './injector/index.js';
|
|
11
12
|
import { injectionToken } from './injector/token.js';
|
|
12
13
|
import { PrettyPrintLogFormatter, provideConsoleLogTransport } from './logger/index.js';
|
|
14
|
+
import { _throw } from './utils/throw.js';
|
|
13
15
|
const MY_INTEGER = injectionToken('MyInteger');
|
|
14
16
|
// current method
|
|
15
17
|
let Foo = class Foo {
|
|
@@ -18,7 +20,7 @@ let Foo = class Foo {
|
|
|
18
20
|
Foo = __decorate([
|
|
19
21
|
Singleton({
|
|
20
22
|
providers: [
|
|
21
|
-
{ provide: MY_INTEGER,
|
|
23
|
+
{ provide: MY_INTEGER, useFactory: () => _throw(new NotImplementedError()) },
|
|
22
24
|
],
|
|
23
25
|
})
|
|
24
26
|
], Foo);
|