@codefast/di 0.5.0-canary.3 → 0.5.0-canary.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/CHANGELOG.md +6 -0
- package/README.md +3 -3
- package/dist/binding-select.d.mts +3 -3
- package/dist/binding-select.mjs +32 -32
- package/dist/container.d.mts +8 -8
- package/dist/container.mjs +32 -32
- package/dist/decorators/inject.mjs +2 -2
- package/dist/environment.d.mts +15 -15
- package/dist/environment.mjs +23 -23
- package/dist/errors.d.mts +2 -2
- package/dist/errors.mjs +5 -5
- package/dist/inspector.d.mts +3 -3
- package/dist/inspector.mjs +7 -7
- package/dist/resolve-options.d.mts +1 -1
- package/dist/resolve-options.mjs +1 -1
- package/dist/resolver.d.mts +9 -9
- package/dist/resolver.mjs +110 -110
- package/dist/types.d.mts +8 -8
- package/package.json +3 -3
- package/src/binding-select.ts +46 -39
- package/src/container.ts +48 -42
- package/src/decorators/inject.ts +4 -2
- package/src/environment.ts +32 -29
- package/src/errors.ts +5 -5
- package/src/inspector.ts +8 -8
- package/src/resolve-options.ts +1 -1
- package/src/resolver.ts +138 -121
- package/src/types.ts +11 -8
package/dist/resolver.mjs
CHANGED
|
@@ -13,7 +13,7 @@ const ROOT_CONSTRAINT_CONTEXT = {
|
|
|
13
13
|
resolutionStack: EMPTY_FRAME_LIST,
|
|
14
14
|
parent: void 0,
|
|
15
15
|
ancestors: EMPTY_FRAME_LIST,
|
|
16
|
-
|
|
16
|
+
currentResolveOptions: void 0
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* @since 0.3.16-canary.0
|
|
@@ -48,23 +48,23 @@ var DependencyResolver = class {
|
|
|
48
48
|
this._container = _container;
|
|
49
49
|
this._parent = _parent;
|
|
50
50
|
}
|
|
51
|
-
_findBinding(token,
|
|
52
|
-
if (
|
|
51
|
+
_findBinding(token, options, resolutionPath, resolutionStack) {
|
|
52
|
+
if (options === void 0) {
|
|
53
53
|
const fastDefaultBinding = this._registry.getFastDefault(token);
|
|
54
54
|
if (fastDefaultBinding !== void 0) return {
|
|
55
55
|
binding: fastDefaultBinding,
|
|
56
56
|
owner: this
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
|
-
if (
|
|
60
|
-
const namedBinding = this._registry.getSimpleNamed(token,
|
|
61
|
-
if (namedBinding !== void 0 && this._matchesBindingFast(namedBinding,
|
|
59
|
+
if (options?.name !== void 0 && options.tag === void 0 && (options.tags?.length ?? 0) === 0) {
|
|
60
|
+
const namedBinding = this._registry.getSimpleNamed(token, options.name);
|
|
61
|
+
if (namedBinding !== void 0 && this._matchesBindingFast(namedBinding, options, resolutionPath, resolutionStack)) return {
|
|
62
62
|
binding: namedBinding,
|
|
63
63
|
owner: this
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
|
-
if (
|
|
67
|
-
const [tagKey, tagValue] =
|
|
66
|
+
if (options !== void 0 && options.name === void 0 && options.tag === void 0 && (options.tags?.length ?? 0) === 1) {
|
|
67
|
+
const [tagKey, tagValue] = options.tags[0];
|
|
68
68
|
const tagged = this._registry.getSimpleTagged(token, tagKey, tagValue);
|
|
69
69
|
if (tagged !== void 0) return {
|
|
70
70
|
binding: tagged,
|
|
@@ -76,37 +76,37 @@ var DependencyResolver = class {
|
|
|
76
76
|
if (bindings.length === 1) {
|
|
77
77
|
const onlyBinding = bindings[0];
|
|
78
78
|
const isDefaultSlot = onlyBinding.slot.name === void 0 && onlyBinding.slot.tags.length === 0;
|
|
79
|
-
if (
|
|
79
|
+
if (options === void 0 && isDefaultSlot && onlyBinding.predicate === void 0) return {
|
|
80
80
|
binding: onlyBinding,
|
|
81
81
|
owner: this
|
|
82
82
|
};
|
|
83
|
-
if (this._matchesBindingFast(onlyBinding,
|
|
83
|
+
if (this._matchesBindingFast(onlyBinding, options, resolutionPath, resolutionStack)) return {
|
|
84
84
|
binding: onlyBinding,
|
|
85
85
|
owner: this
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
|
-
const binding = selectBinding(bindings,
|
|
88
|
+
const binding = selectBinding(bindings, options, this._makeConstraintContext(resolutionPath, resolutionStack, options), this._getTokenName(token));
|
|
89
89
|
if (binding !== void 0) return {
|
|
90
90
|
binding,
|
|
91
91
|
owner: this
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
|
-
if (this._parent !== void 0) return this._parent._findBinding(token,
|
|
94
|
+
if (this._parent !== void 0) return this._parent._findBinding(token, options, resolutionPath, resolutionStack);
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
97
97
|
* Binding lookup aligned with `resolve` — used by `Container.validate` without instantiating.
|
|
98
98
|
*/
|
|
99
|
-
peekBindingForValidate(token,
|
|
100
|
-
return this._findBinding(token,
|
|
99
|
+
peekBindingForValidate(token, options) {
|
|
100
|
+
return this._findBinding(token, options, [], []);
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
103
|
* Mirrors {@link DependencyResolver.resolveAll} candidate selection only (no instantiation).
|
|
104
104
|
*/
|
|
105
|
-
peekCandidateBindingsForValidate(token,
|
|
106
|
-
if (
|
|
105
|
+
peekCandidateBindingsForValidate(token, options) {
|
|
106
|
+
if (options?.name !== void 0 && options.tag === void 0 && (options.tags?.length ?? 0) === 0) return this._getSimpleNamedBindingsFromChain(token, options.name);
|
|
107
107
|
const allBindings = this._getAllBindingsFromChain(token);
|
|
108
108
|
if (allBindings.length === 0) return [];
|
|
109
|
-
return selectAllBindings(allBindings,
|
|
109
|
+
return selectAllBindings(allBindings, options, this._makeConstraintContext([], [], options));
|
|
110
110
|
}
|
|
111
111
|
resolveFromContext(token, resolutionPath, resolutionStack) {
|
|
112
112
|
const fastBinding = this._registry.getFastDefault(token);
|
|
@@ -123,18 +123,18 @@ var DependencyResolver = class {
|
|
|
123
123
|
}
|
|
124
124
|
return this.resolve(token, void 0, resolutionPath, resolutionStack);
|
|
125
125
|
}
|
|
126
|
-
resolve(token,
|
|
127
|
-
const found = this._findBinding(token,
|
|
126
|
+
resolve(token, options, resolutionPath, resolutionStack) {
|
|
127
|
+
const found = this._findBinding(token, options, resolutionPath, resolutionStack);
|
|
128
128
|
if (found === void 0) {
|
|
129
|
-
if (this._registry.getAll(token).length > 0) throw new NoMatchingBindingError(this._getTokenName(token),
|
|
129
|
+
if (this._registry.getAll(token).length > 0) throw new NoMatchingBindingError(this._getTokenName(token), options ?? {}, this._getAvailableSlots(token));
|
|
130
130
|
throw new TokenNotBoundError(this._getTokenName(token));
|
|
131
131
|
}
|
|
132
132
|
const { binding, owner } = found;
|
|
133
|
-
if (binding.kind === "alias") return this.resolve(binding.target,
|
|
134
|
-
if ((binding.scope ?? "transient") === "singleton" && owner !== this) return owner._resolveBinding(binding,
|
|
135
|
-
return this._resolveBinding(binding,
|
|
133
|
+
if (binding.kind === "alias") return this.resolve(binding.target, options, resolutionPath, resolutionStack);
|
|
134
|
+
if ((binding.scope ?? "transient") === "singleton" && owner !== this) return owner._resolveBinding(binding, options, resolutionPath, resolutionStack);
|
|
135
|
+
return this._resolveBinding(binding, options, resolutionPath, resolutionStack);
|
|
136
136
|
}
|
|
137
|
-
_resolveBinding(binding,
|
|
137
|
+
_resolveBinding(binding, options, resolutionPath, resolutionStack) {
|
|
138
138
|
if (binding.kind === "constant" && binding.onActivation === void 0 && !this._lifecycle.hasActivationHandlers(binding.token)) return binding.value;
|
|
139
139
|
const scope = binding.scope ?? "transient";
|
|
140
140
|
if (scope === "singleton") {
|
|
@@ -158,7 +158,7 @@ var DependencyResolver = class {
|
|
|
158
158
|
resolutionStack.push(frame);
|
|
159
159
|
const needsActivation = this._needsActivation(binding);
|
|
160
160
|
if (!needsActivation && scope === "transient" && binding.kind === "dynamic") {
|
|
161
|
-
const resolutionCtx = this._acquireSyncResolutionContext(resolutionPath, resolutionStack,
|
|
161
|
+
const resolutionCtx = this._acquireSyncResolutionContext(resolutionPath, resolutionStack, options);
|
|
162
162
|
try {
|
|
163
163
|
const dynamicResult = binding.factory(resolutionCtx);
|
|
164
164
|
if (dynamicResult instanceof Promise) throw new AsyncResolutionError(tokenName(binding.token), tokenName(binding.token));
|
|
@@ -174,7 +174,7 @@ var DependencyResolver = class {
|
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
try {
|
|
177
|
-
const resolutionCtx = needsActivation || this._requiresResolutionContext(binding) ? this._acquireSyncResolutionContext(resolutionPath, resolutionStack,
|
|
177
|
+
const resolutionCtx = needsActivation || this._requiresResolutionContext(binding) ? this._acquireSyncResolutionContext(resolutionPath, resolutionStack, options) : void 0;
|
|
178
178
|
const instance = this._instantiateSync(binding, resolutionCtx, resolutionPath, resolutionStack);
|
|
179
179
|
const activated = this._refreshActivationCacheIfNeeded(binding, needsActivation) ? this._lifecycle.runActivationSync(resolutionCtx, binding, instance, this._metadataReader) : instance;
|
|
180
180
|
if (scope === "singleton") this._scope.setSingleton(binding.id, activated);
|
|
@@ -219,25 +219,25 @@ var DependencyResolver = class {
|
|
|
219
219
|
if (meta.params.length === 0) return [];
|
|
220
220
|
if (meta.params.length === 1) {
|
|
221
221
|
const param = meta.params[0];
|
|
222
|
-
const
|
|
223
|
-
if (param.multi) return [this.resolveAll(param.token,
|
|
224
|
-
if (param.optional) return [this.resolveOptional(param.token,
|
|
225
|
-
if (
|
|
226
|
-
return [this.resolve(param.token,
|
|
222
|
+
const paramOptions = injectionSlotToResolveOptions(param);
|
|
223
|
+
if (param.multi) return [this.resolveAll(param.token, paramOptions, resolutionPath, resolutionStack)];
|
|
224
|
+
if (param.optional) return [this.resolveOptional(param.token, paramOptions, resolutionPath, resolutionStack)];
|
|
225
|
+
if (paramOptions === void 0) return [this.resolveFromContext(param.token, resolutionPath, resolutionStack)];
|
|
226
|
+
return [this.resolve(param.token, paramOptions, resolutionPath, resolutionStack)];
|
|
227
227
|
}
|
|
228
228
|
const deps = new Array(meta.params.length);
|
|
229
229
|
for (let index = 0; index < meta.params.length; index += 1) {
|
|
230
230
|
const param = meta.params[index];
|
|
231
|
-
const
|
|
231
|
+
const paramOptions = injectionSlotToResolveOptions(param);
|
|
232
232
|
if (param.multi) {
|
|
233
|
-
deps[index] = this.resolveAll(param.token,
|
|
233
|
+
deps[index] = this.resolveAll(param.token, paramOptions, resolutionPath, resolutionStack);
|
|
234
234
|
continue;
|
|
235
235
|
}
|
|
236
236
|
if (param.optional) {
|
|
237
|
-
deps[index] = this.resolveOptional(param.token,
|
|
237
|
+
deps[index] = this.resolveOptional(param.token, paramOptions, resolutionPath, resolutionStack);
|
|
238
238
|
continue;
|
|
239
239
|
}
|
|
240
|
-
deps[index] =
|
|
240
|
+
deps[index] = paramOptions === void 0 ? this.resolveFromContext(param.token, resolutionPath, resolutionStack) : this.resolve(param.token, paramOptions, resolutionPath, resolutionStack);
|
|
241
241
|
}
|
|
242
242
|
return deps;
|
|
243
243
|
}
|
|
@@ -245,36 +245,36 @@ var DependencyResolver = class {
|
|
|
245
245
|
const resolved = new Array(deps.length);
|
|
246
246
|
for (let index = 0; index < deps.length; index += 1) {
|
|
247
247
|
const dep = deps[index];
|
|
248
|
-
const
|
|
248
|
+
const depOptions = injectionSlotToResolveOptions(dep);
|
|
249
249
|
if (dep.multi) {
|
|
250
|
-
resolved[index] = this.resolveAll(dep.token,
|
|
250
|
+
resolved[index] = this.resolveAll(dep.token, depOptions, resolutionPath, resolutionStack);
|
|
251
251
|
continue;
|
|
252
252
|
}
|
|
253
253
|
if (dep.optional) {
|
|
254
|
-
resolved[index] = this.resolveOptional(dep.token,
|
|
254
|
+
resolved[index] = this.resolveOptional(dep.token, depOptions, resolutionPath, resolutionStack);
|
|
255
255
|
continue;
|
|
256
256
|
}
|
|
257
|
-
resolved[index] =
|
|
257
|
+
resolved[index] = depOptions === void 0 ? this.resolveFromContext(dep.token, resolutionPath, resolutionStack) : this.resolve(dep.token, depOptions, resolutionPath, resolutionStack);
|
|
258
258
|
}
|
|
259
259
|
return resolved;
|
|
260
260
|
}
|
|
261
|
-
resolveOptional(token,
|
|
262
|
-
if (this._findBinding(token,
|
|
263
|
-
return this.resolve(token,
|
|
261
|
+
resolveOptional(token, options, resolutionPath, resolutionStack) {
|
|
262
|
+
if (this._findBinding(token, options, resolutionPath, resolutionStack) === void 0) return;
|
|
263
|
+
return this.resolve(token, options, resolutionPath, resolutionStack);
|
|
264
264
|
}
|
|
265
|
-
resolveAll(token,
|
|
266
|
-
if (
|
|
267
|
-
const namedCandidates = this._getSimpleNamedBindingsFromChain(token,
|
|
265
|
+
resolveAll(token, options, resolutionPath, resolutionStack) {
|
|
266
|
+
if (options?.name !== void 0 && options.tag === void 0 && (options.tags?.length ?? 0) === 0) {
|
|
267
|
+
const namedCandidates = this._getSimpleNamedBindingsFromChain(token, options.name);
|
|
268
268
|
if (namedCandidates.length === 0) return [];
|
|
269
269
|
const resolved = new Array(namedCandidates.length);
|
|
270
|
-
for (let index = 0; index < namedCandidates.length; index += 1) resolved[index] = this._resolveCandidateSync(namedCandidates[index],
|
|
270
|
+
for (let index = 0; index < namedCandidates.length; index += 1) resolved[index] = this._resolveCandidateSync(namedCandidates[index], options, resolutionPath, resolutionStack);
|
|
271
271
|
return resolved;
|
|
272
272
|
}
|
|
273
273
|
const allBindings = this._getAllBindingsFromChain(token);
|
|
274
274
|
if (allBindings.length === 0) return [];
|
|
275
|
-
const candidates = selectAllBindings(allBindings,
|
|
275
|
+
const candidates = selectAllBindings(allBindings, options, this._makeConstraintContext(resolutionPath, resolutionStack, options));
|
|
276
276
|
const resolved = new Array(candidates.length);
|
|
277
|
-
for (let index = 0; index < candidates.length; index += 1) resolved[index] = this._resolveCandidateSync(candidates[index],
|
|
277
|
+
for (let index = 0; index < candidates.length; index += 1) resolved[index] = this._resolveCandidateSync(candidates[index], options, resolutionPath, resolutionStack);
|
|
278
278
|
return resolved;
|
|
279
279
|
}
|
|
280
280
|
resolveAsyncFromContext(token, resolutionPath, resolutionStack) {
|
|
@@ -292,18 +292,18 @@ var DependencyResolver = class {
|
|
|
292
292
|
}
|
|
293
293
|
return this.resolveAsync(token, void 0, resolutionPath, resolutionStack);
|
|
294
294
|
}
|
|
295
|
-
async resolveAsync(token,
|
|
296
|
-
const found = this._findBinding(token,
|
|
295
|
+
async resolveAsync(token, options, resolutionPath, resolutionStack) {
|
|
296
|
+
const found = this._findBinding(token, options, resolutionPath, resolutionStack);
|
|
297
297
|
if (found === void 0) {
|
|
298
|
-
if (this._registry.getAll(token).length > 0) throw new NoMatchingBindingError(this._getTokenName(token),
|
|
298
|
+
if (this._registry.getAll(token).length > 0) throw new NoMatchingBindingError(this._getTokenName(token), options ?? {}, this._getAvailableSlots(token));
|
|
299
299
|
throw new TokenNotBoundError(this._getTokenName(token));
|
|
300
300
|
}
|
|
301
301
|
const { binding, owner } = found;
|
|
302
|
-
if (binding.kind === "alias") return this.resolveAsync(binding.target,
|
|
303
|
-
if ((binding.scope ?? "transient") === "singleton" && owner !== this) return owner._resolveBindingAsync(binding,
|
|
304
|
-
return this._resolveBindingAsync(binding,
|
|
302
|
+
if (binding.kind === "alias") return this.resolveAsync(binding.target, options, resolutionPath, resolutionStack);
|
|
303
|
+
if ((binding.scope ?? "transient") === "singleton" && owner !== this) return owner._resolveBindingAsync(binding, options, resolutionPath, resolutionStack);
|
|
304
|
+
return this._resolveBindingAsync(binding, options, resolutionPath, resolutionStack);
|
|
305
305
|
}
|
|
306
|
-
async _resolveBindingAsync(binding,
|
|
306
|
+
async _resolveBindingAsync(binding, options, resolutionPath, resolutionStack) {
|
|
307
307
|
if (binding.kind === "constant" && binding.onActivation === void 0 && !this._lifecycle.hasActivationHandlers(binding.token)) return binding.value;
|
|
308
308
|
const scope = binding.scope ?? "transient";
|
|
309
309
|
if (scope === "singleton") {
|
|
@@ -329,7 +329,7 @@ var DependencyResolver = class {
|
|
|
329
329
|
resolutionStack.push(frame);
|
|
330
330
|
const needsActivation = this._needsActivation(binding);
|
|
331
331
|
if (!needsActivation && scope === "transient" && (binding.kind === "dynamic" || binding.kind === "dynamic-async")) {
|
|
332
|
-
const resolutionCtx = new DefaultResolutionContext(this, resolutionPath, resolutionStack,
|
|
332
|
+
const resolutionCtx = new DefaultResolutionContext(this, resolutionPath, resolutionStack, options);
|
|
333
333
|
try {
|
|
334
334
|
if (binding.kind === "dynamic-async") return await binding.factory(resolutionCtx);
|
|
335
335
|
const dynamicResult = binding.factory(resolutionCtx);
|
|
@@ -340,7 +340,7 @@ var DependencyResolver = class {
|
|
|
340
340
|
resolutionSet?.delete(frameName);
|
|
341
341
|
}
|
|
342
342
|
}
|
|
343
|
-
const resolutionCtx = needsActivation || this._requiresResolutionContext(binding) ? new DefaultResolutionContext(this, resolutionPath, resolutionStack,
|
|
343
|
+
const resolutionCtx = needsActivation || this._requiresResolutionContext(binding) ? new DefaultResolutionContext(this, resolutionPath, resolutionStack, options) : void 0;
|
|
344
344
|
try {
|
|
345
345
|
if (scope === "singleton") {
|
|
346
346
|
const createSingletonPromise = async () => {
|
|
@@ -404,20 +404,20 @@ var DependencyResolver = class {
|
|
|
404
404
|
if (meta.params.length === 0) return [];
|
|
405
405
|
if (meta.params.length === 1) {
|
|
406
406
|
const param = meta.params[0];
|
|
407
|
-
const
|
|
408
|
-
if (param.multi) return [await this.resolveAllAsync(param.token,
|
|
409
|
-
if (param.optional) return [await this.resolveOptionalAsync(param.token,
|
|
410
|
-
if (
|
|
411
|
-
return [await this.resolveAsync(param.token,
|
|
407
|
+
const paramOptions = injectionSlotToResolveOptions(param);
|
|
408
|
+
if (param.multi) return [await this.resolveAllAsync(param.token, paramOptions, resolutionPath, resolutionStack)];
|
|
409
|
+
if (param.optional) return [await this.resolveOptionalAsync(param.token, paramOptions, resolutionPath, resolutionStack)];
|
|
410
|
+
if (paramOptions === void 0) return [await this.resolveAsyncFromContext(param.token, resolutionPath, resolutionStack)];
|
|
411
|
+
return [await this.resolveAsync(param.token, paramOptions, resolutionPath, resolutionStack)];
|
|
412
412
|
}
|
|
413
413
|
const pending = new Array(meta.params.length);
|
|
414
414
|
const shouldCloneContext = meta.params.length > 1;
|
|
415
415
|
for (let index = 0; index < meta.params.length; index += 1) {
|
|
416
416
|
const param = meta.params[index];
|
|
417
|
-
const
|
|
418
|
-
if (param.multi) pending[index] = this.resolveAllAsync(param.token,
|
|
419
|
-
else if (param.optional) pending[index] = this.resolveOptionalAsync(param.token,
|
|
420
|
-
else pending[index] =
|
|
417
|
+
const paramOptions = injectionSlotToResolveOptions(param);
|
|
418
|
+
if (param.multi) pending[index] = this.resolveAllAsync(param.token, paramOptions, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack);
|
|
419
|
+
else if (param.optional) pending[index] = this.resolveOptionalAsync(param.token, paramOptions, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack);
|
|
420
|
+
else pending[index] = paramOptions === void 0 ? this.resolveAsyncFromContext(param.token, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack) : this.resolveAsync(param.token, paramOptions, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack);
|
|
421
421
|
}
|
|
422
422
|
return Promise.all(pending);
|
|
423
423
|
}
|
|
@@ -426,30 +426,30 @@ var DependencyResolver = class {
|
|
|
426
426
|
const shouldCloneContext = deps.length > 1;
|
|
427
427
|
for (let index = 0; index < deps.length; index += 1) {
|
|
428
428
|
const dep = deps[index];
|
|
429
|
-
const
|
|
430
|
-
if (dep.multi) pending[index] = this.resolveAllAsync(dep.token,
|
|
431
|
-
else if (dep.optional) pending[index] = this.resolveOptionalAsync(dep.token,
|
|
432
|
-
else pending[index] =
|
|
429
|
+
const depOptions = injectionSlotToResolveOptions(dep);
|
|
430
|
+
if (dep.multi) pending[index] = this.resolveAllAsync(dep.token, depOptions, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack);
|
|
431
|
+
else if (dep.optional) pending[index] = this.resolveOptionalAsync(dep.token, depOptions, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack);
|
|
432
|
+
else pending[index] = depOptions === void 0 ? this.resolveAsyncFromContext(dep.token, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack) : this.resolveAsync(dep.token, depOptions, shouldCloneContext ? [...resolutionPath] : resolutionPath, shouldCloneContext ? [...resolutionStack] : resolutionStack);
|
|
433
433
|
}
|
|
434
434
|
return Promise.all(pending);
|
|
435
435
|
}
|
|
436
|
-
async resolveOptionalAsync(token,
|
|
437
|
-
if (this._findBinding(token,
|
|
438
|
-
return this.resolveAsync(token,
|
|
436
|
+
async resolveOptionalAsync(token, options, resolutionPath, resolutionStack) {
|
|
437
|
+
if (this._findBinding(token, options, resolutionPath, resolutionStack) === void 0) return;
|
|
438
|
+
return this.resolveAsync(token, options, resolutionPath, resolutionStack);
|
|
439
439
|
}
|
|
440
|
-
async resolveAllAsync(token,
|
|
441
|
-
if (
|
|
442
|
-
const namedCandidates = this._getSimpleNamedBindingsFromChain(token,
|
|
440
|
+
async resolveAllAsync(token, options, resolutionPath, resolutionStack) {
|
|
441
|
+
if (options?.name !== void 0 && options.tag === void 0 && (options.tags?.length ?? 0) === 0) {
|
|
442
|
+
const namedCandidates = this._getSimpleNamedBindingsFromChain(token, options.name);
|
|
443
443
|
if (namedCandidates.length === 0) return [];
|
|
444
444
|
const pending = new Array(namedCandidates.length);
|
|
445
|
-
for (let index = 0; index < namedCandidates.length; index += 1) pending[index] = this._resolveCandidateAsync(namedCandidates[index],
|
|
445
|
+
for (let index = 0; index < namedCandidates.length; index += 1) pending[index] = this._resolveCandidateAsync(namedCandidates[index], options, resolutionPath, resolutionStack);
|
|
446
446
|
return Promise.all(pending);
|
|
447
447
|
}
|
|
448
448
|
const allBindings = this._getAllBindingsFromChain(token);
|
|
449
449
|
if (allBindings.length === 0) return [];
|
|
450
|
-
const candidates = selectAllBindings(allBindings,
|
|
450
|
+
const candidates = selectAllBindings(allBindings, options, this._makeConstraintContext(resolutionPath, resolutionStack, options));
|
|
451
451
|
const pending = new Array(candidates.length);
|
|
452
|
-
for (let index = 0; index < candidates.length; index += 1) pending[index] = this._resolveCandidateAsync(candidates[index],
|
|
452
|
+
for (let index = 0; index < candidates.length; index += 1) pending[index] = this._resolveCandidateAsync(candidates[index], options, resolutionPath, resolutionStack);
|
|
453
453
|
return Promise.all(pending);
|
|
454
454
|
}
|
|
455
455
|
_getAllBindingsFromChain(token) {
|
|
@@ -480,34 +480,34 @@ var DependencyResolver = class {
|
|
|
480
480
|
_getAvailableSlots(token) {
|
|
481
481
|
return this._registry.availableSlotStrings(token);
|
|
482
482
|
}
|
|
483
|
-
_makeConstraintContext(resolutionPath, resolutionStack,
|
|
484
|
-
if (
|
|
483
|
+
_makeConstraintContext(resolutionPath, resolutionStack, options) {
|
|
484
|
+
if (options === void 0 && resolutionPath.length === 0 && resolutionStack.length === 0) return ROOT_CONSTRAINT_CONTEXT;
|
|
485
485
|
return {
|
|
486
486
|
resolutionPath,
|
|
487
487
|
resolutionStack,
|
|
488
488
|
parent: resolutionStack.at(-1),
|
|
489
489
|
ancestors: resolutionStack.length > 1 ? resolutionStack.slice(0, -1) : [],
|
|
490
|
-
|
|
490
|
+
currentResolveOptions: options
|
|
491
491
|
};
|
|
492
492
|
}
|
|
493
|
-
_matchesBindingFast(binding,
|
|
494
|
-
if (!this._matchesSlotFast(binding.slot,
|
|
493
|
+
_matchesBindingFast(binding, options, resolutionPath, resolutionStack) {
|
|
494
|
+
if (!this._matchesSlotFast(binding.slot, options)) return false;
|
|
495
495
|
if (binding.predicate === void 0) return true;
|
|
496
|
-
const ctx = this._makeConstraintContext(resolutionPath, resolutionStack,
|
|
496
|
+
const ctx = this._makeConstraintContext(resolutionPath, resolutionStack, options);
|
|
497
497
|
return binding.predicate(ctx);
|
|
498
498
|
}
|
|
499
|
-
_matchesSlotFast(slot,
|
|
500
|
-
const
|
|
501
|
-
const
|
|
502
|
-
const
|
|
503
|
-
const
|
|
499
|
+
_matchesSlotFast(slot, options) {
|
|
500
|
+
const requestedName = options?.name;
|
|
501
|
+
const requestedTags = options?.tags;
|
|
502
|
+
const singleRequestedTag = options?.tag;
|
|
503
|
+
const hasRequestedTags = (requestedTags?.length ?? 0) > 0 || singleRequestedTag !== void 0;
|
|
504
504
|
if (slot.name !== void 0) {
|
|
505
|
-
if (
|
|
506
|
-
} else if (
|
|
505
|
+
if (requestedName === void 0 || slot.name !== requestedName) return false;
|
|
506
|
+
} else if (requestedName !== void 0) return false;
|
|
507
507
|
if (slot.tags.length > 0) {
|
|
508
|
-
if (!
|
|
509
|
-
for (const [tagKey, tagValue] of slot.tags) if (!this.
|
|
510
|
-
} else if (
|
|
508
|
+
if (!hasRequestedTags) return false;
|
|
509
|
+
for (const [tagKey, tagValue] of slot.tags) if (!this._matchesRequestedTag(tagKey, tagValue, requestedTags, singleRequestedTag)) return false;
|
|
510
|
+
} else if (hasRequestedTags) return false;
|
|
511
511
|
return true;
|
|
512
512
|
}
|
|
513
513
|
_getTokenName(token) {
|
|
@@ -530,12 +530,12 @@ var DependencyResolver = class {
|
|
|
530
530
|
if (!needsActiveContainer) return new invokable(...deps);
|
|
531
531
|
return runWithContainer(this._container, () => new invokable(...deps));
|
|
532
532
|
}
|
|
533
|
-
|
|
534
|
-
if (
|
|
535
|
-
if (
|
|
536
|
-
for (let index = 0; index <
|
|
537
|
-
const
|
|
538
|
-
if (
|
|
533
|
+
_matchesRequestedTag(tagKey, tagValue, requestedTags, singleRequestedTag) {
|
|
534
|
+
if (singleRequestedTag !== void 0 && singleRequestedTag[0] === tagKey && Object.is(singleRequestedTag[1], tagValue)) return true;
|
|
535
|
+
if (requestedTags === void 0 || requestedTags.length === 0) return false;
|
|
536
|
+
for (let index = 0; index < requestedTags.length; index += 1) {
|
|
537
|
+
const requestedTag = requestedTags[index];
|
|
538
|
+
if (requestedTag[0] === tagKey && Object.is(requestedTag[1], tagValue)) return true;
|
|
539
539
|
}
|
|
540
540
|
return false;
|
|
541
541
|
}
|
|
@@ -676,29 +676,29 @@ var DependencyResolver = class {
|
|
|
676
676
|
resolutionSet.delete(tokenDisplayName);
|
|
677
677
|
}
|
|
678
678
|
}
|
|
679
|
-
_resolveCandidateSync(binding,
|
|
679
|
+
_resolveCandidateSync(binding, options, resolutionPath, resolutionStack) {
|
|
680
680
|
if (binding.kind === "constant" && binding.onActivation === void 0 && !this._lifecycle.hasActivationHandlers(binding.token)) return binding.value;
|
|
681
|
-
if (binding.kind === "alias") return this.resolve(binding.target,
|
|
681
|
+
if (binding.kind === "alias") return this.resolve(binding.target, options, resolutionPath, resolutionStack);
|
|
682
682
|
const scope = binding.scope ?? "transient";
|
|
683
683
|
if (scope === "singleton" && this._scope.hasSingleton(binding.id)) return this._scope.getSingleton(binding.id);
|
|
684
684
|
if (scope === "scoped") {
|
|
685
685
|
if (!this._scope.isChild) throw new MissingScopeContextError(this._getTokenName(binding.token));
|
|
686
686
|
if (this._scope.hasScoped(binding.id)) return this._scope.getScoped(binding.id);
|
|
687
687
|
}
|
|
688
|
-
return this._resolveBinding(binding,
|
|
688
|
+
return this._resolveBinding(binding, options, resolutionPath, resolutionStack);
|
|
689
689
|
}
|
|
690
|
-
_resolveCandidateAsync(binding,
|
|
690
|
+
_resolveCandidateAsync(binding, options, resolutionPath, resolutionStack) {
|
|
691
691
|
if (binding.kind === "constant" && binding.onActivation === void 0 && !this._lifecycle.hasActivationHandlers(binding.token)) return Promise.resolve(binding.value);
|
|
692
692
|
const isolatedPath = [...resolutionPath];
|
|
693
693
|
const isolatedStack = [...resolutionStack];
|
|
694
|
-
if (binding.kind === "alias") return this.resolveAsync(binding.target,
|
|
694
|
+
if (binding.kind === "alias") return this.resolveAsync(binding.target, options, isolatedPath, isolatedStack);
|
|
695
695
|
const scope = binding.scope ?? "transient";
|
|
696
696
|
if (scope === "singleton" && this._scope.hasSingleton(binding.id)) return Promise.resolve(this._scope.getSingleton(binding.id));
|
|
697
697
|
if (scope === "scoped") {
|
|
698
698
|
if (!this._scope.isChild) return Promise.reject(new MissingScopeContextError(this._getTokenName(binding.token)));
|
|
699
699
|
if (this._scope.hasScoped(binding.id)) return Promise.resolve(this._scope.getScoped(binding.id));
|
|
700
700
|
}
|
|
701
|
-
return this._resolveBindingAsync(binding,
|
|
701
|
+
return this._resolveBindingAsync(binding, options, isolatedPath, isolatedStack);
|
|
702
702
|
}
|
|
703
703
|
_getResolutionFrame(binding) {
|
|
704
704
|
const existing = this._frameByBindingId.get(binding.id);
|
|
@@ -751,14 +751,14 @@ var DependencyResolver = class {
|
|
|
751
751
|
_requiresResolutionContext(binding) {
|
|
752
752
|
return binding.kind === "dynamic" || binding.kind === "dynamic-async";
|
|
753
753
|
}
|
|
754
|
-
_acquireSyncResolutionContext(resolutionPath, resolutionStack,
|
|
754
|
+
_acquireSyncResolutionContext(resolutionPath, resolutionStack, options) {
|
|
755
755
|
const depth = resolutionStack.length;
|
|
756
756
|
const existing = this._syncResolutionContextPool[depth];
|
|
757
757
|
if (existing !== void 0) {
|
|
758
|
-
existing.reset(this, resolutionPath, resolutionStack,
|
|
758
|
+
existing.reset(this, resolutionPath, resolutionStack, options);
|
|
759
759
|
return existing;
|
|
760
760
|
}
|
|
761
|
-
const created = new DefaultResolutionContext(this, resolutionPath, resolutionStack,
|
|
761
|
+
const created = new DefaultResolutionContext(this, resolutionPath, resolutionStack, options);
|
|
762
762
|
this._syncResolutionContextPool[depth] = created;
|
|
763
763
|
return created;
|
|
764
764
|
}
|
package/dist/types.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { Token } from "./token.mjs";
|
|
|
3
3
|
|
|
4
4
|
//#region src/types.d.ts
|
|
5
5
|
/**
|
|
6
|
-
* A single [tag, value] pair used in slot constraints and resolve
|
|
6
|
+
* A single [tag, value] pair used in slot constraints and resolve options.
|
|
7
7
|
*
|
|
8
8
|
* @since 0.3.16-canary.0
|
|
9
9
|
*/
|
|
@@ -70,18 +70,18 @@ interface ConstraintContext {
|
|
|
70
70
|
readonly resolutionStack: ReadonlyArray<ResolutionFrame>;
|
|
71
71
|
readonly parent: ResolutionFrame | undefined;
|
|
72
72
|
readonly ancestors: ReadonlyArray<ResolutionFrame>;
|
|
73
|
-
readonly
|
|
73
|
+
readonly currentResolveOptions: ResolveOptions | undefined;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* @since 0.3.16-canary.0
|
|
77
77
|
*/
|
|
78
78
|
interface ResolutionContext {
|
|
79
|
-
resolve<const Value>(token: Token<Value> | Constructor<Value>,
|
|
80
|
-
resolveAsync<const Value>(token: Token<Value> | Constructor<Value>,
|
|
81
|
-
resolveOptional<const Value>(token: Token<Value> | Constructor<Value>,
|
|
82
|
-
resolveOptionalAsync<const Value>(token: Token<Value> | Constructor<Value>,
|
|
83
|
-
resolveAll<const Value>(token: Token<Value> | Constructor<Value>,
|
|
84
|
-
resolveAllAsync<const Value>(token: Token<Value> | Constructor<Value>,
|
|
79
|
+
resolve<const Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Value;
|
|
80
|
+
resolveAsync<const Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<Value>;
|
|
81
|
+
resolveOptional<const Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Value | undefined;
|
|
82
|
+
resolveOptionalAsync<const Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<Value | undefined>;
|
|
83
|
+
resolveAll<const Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Array<Value>;
|
|
84
|
+
resolveAllAsync<const Value>(token: Token<Value> | Constructor<Value>, options?: ResolveOptions): Promise<Array<Value>>;
|
|
85
85
|
readonly graph: ConstraintContext;
|
|
86
86
|
}
|
|
87
87
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/di",
|
|
3
|
-
"version": "0.5.0-canary.
|
|
3
|
+
"version": "0.5.0-canary.4",
|
|
4
4
|
"description": "Lightweight dependency injection primitives for Codefast",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"codefast",
|
|
@@ -200,14 +200,14 @@
|
|
|
200
200
|
"@babel/plugin-proposal-decorators": "8.0.2",
|
|
201
201
|
"@rolldown/plugin-babel": "^0.2.3",
|
|
202
202
|
"@types/node": "^26.1.0",
|
|
203
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
203
|
+
"@typescript/native-preview": "7.0.0-dev.20260704.1",
|
|
204
204
|
"@vitest/coverage-v8": "^4.1.9",
|
|
205
205
|
"expect-type": "^1.4.0",
|
|
206
206
|
"tsdown": "^0.22.3",
|
|
207
207
|
"tsx": "^4.23.0",
|
|
208
208
|
"typescript": "^6.0.3",
|
|
209
209
|
"vitest": "^4.1.9",
|
|
210
|
-
"@codefast/typescript-config": "0.5.0-canary.
|
|
210
|
+
"@codefast/typescript-config": "0.5.0-canary.4"
|
|
211
211
|
},
|
|
212
212
|
"engines": {
|
|
213
213
|
"node": ">=26.0.0"
|