@lppedd/di-wise-neo 0.9.3 → 0.10.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 +6 -6
- package/dist/cjs/index.d.ts +1 -2
- package/dist/cjs/index.js +21 -34
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.d.mts +1 -2
- package/dist/es/index.mjs +21 -34
- package/dist/es/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
@@ -168,14 +168,9 @@ registrar.registerCommand("my.command", () => { console.log("hey!"); });
|
|
168
168
|
|
169
169
|
## Container scopes
|
170
170
|
|
171
|
-
The [Container][source-container] supports
|
171
|
+
The [Container][source-container] supports three **scope** types that determine how and when
|
172
172
|
values are cached and reused.
|
173
173
|
|
174
|
-
### Inherited
|
175
|
-
|
176
|
-
Inherits the scope from the requesting (dependent) token.
|
177
|
-
If there is no dependent (i.e., during top-level resolution), it behaves like **Transient**.
|
178
|
-
|
179
174
|
### Transient
|
180
175
|
|
181
176
|
Creates a new value every time the dependency is resolved, which means values are never cached.
|
@@ -184,6 +179,11 @@ Creates a new value every time the dependency is resolved, which means values ar
|
|
184
179
|
- a factory function registered via `FactoryProvider` is invoked on each resolution
|
185
180
|
- a value registered via `ValueProvider` is always returned as-is
|
186
181
|
|
182
|
+
> [!NOTE]
|
183
|
+
> When a **Transient** or **Resolution**-scoped value is injected into a **Container**-scoped
|
184
|
+
> instance, it effectively inherits the lifecycle of that instance. The value will live as long
|
185
|
+
> as the containing instance, even though it is not cached by the container itself.
|
186
|
+
|
187
187
|
### Resolution
|
188
188
|
|
189
189
|
Creates and caches a single value per resolution graph.
|
package/dist/cjs/index.d.ts
CHANGED
@@ -155,7 +155,6 @@ interface ExistingProvider<Value> {
|
|
155
155
|
type Provider<Value = any> = ClassProvider<Value & object> | FactoryProvider<Value> | ValueProvider<Value> | ExistingProvider<Value>;
|
156
156
|
|
157
157
|
declare const Scope: {
|
158
|
-
readonly Inherited: "Inherited";
|
159
158
|
readonly Transient: "Transient";
|
160
159
|
readonly Resolution: "Resolution";
|
161
160
|
readonly Container: "Container";
|
@@ -203,7 +202,7 @@ interface ContainerOptions {
|
|
203
202
|
/**
|
204
203
|
* The default scope for registrations.
|
205
204
|
*
|
206
|
-
* @defaultValue Scope.
|
205
|
+
* @defaultValue Scope.Transient
|
207
206
|
*/
|
208
207
|
readonly defaultScope: Scope;
|
209
208
|
}
|
package/dist/cjs/index.js
CHANGED
@@ -17,17 +17,17 @@ function throwUnregisteredError(token, name) {
|
|
17
17
|
}
|
18
18
|
// @internal
|
19
19
|
function throwExistingUnregisteredError(token, cause) {
|
20
|
-
const
|
21
|
-
throw isError(cause) ? new Error(`${
|
20
|
+
const msg = tag(`failed to resolve token ${getTokenName(token)}`);
|
21
|
+
throw isError(cause) ? new Error(`${msg}\n [cause] ${untag(cause.message)}`, {
|
22
22
|
cause
|
23
|
-
}) : new Error(`${
|
23
|
+
}) : new Error(`${msg}\n [cause] the aliased token ${getTokenName(cause)} is not registered`);
|
24
24
|
}
|
25
25
|
// @internal
|
26
26
|
function throwParameterResolutionError(ctor, methodKey, dependency, cause) {
|
27
27
|
const location = getLocation(ctor, methodKey);
|
28
|
-
const
|
29
|
-
const
|
30
|
-
throw new Error(`${
|
28
|
+
const tokenName = getTokenName(dependency.tokenRef.getRefToken());
|
29
|
+
const msg = tag(`failed to resolve dependency for ${location}(parameter #${dependency.index}: ${tokenName})`);
|
30
|
+
throw new Error(`${msg}\n [cause] ${untag(cause.message)}`, {
|
31
31
|
cause
|
32
32
|
});
|
33
33
|
}
|
@@ -298,7 +298,6 @@ function isExistingProvider(provider) {
|
|
298
298
|
}
|
299
299
|
|
300
300
|
const Scope = {
|
301
|
-
Inherited: "Inherited",
|
302
301
|
Transient: "Transient",
|
303
302
|
Resolution: "Resolution",
|
304
303
|
Container: "Container"
|
@@ -375,11 +374,14 @@ class TokenRegistry {
|
|
375
374
|
set(token, registration) {
|
376
375
|
check(!internals.has(token), `cannot register reserved token ${token.name}`);
|
377
376
|
let registrations = this.myMap.get(token);
|
378
|
-
if (
|
377
|
+
if (registrations) {
|
378
|
+
const name = registration.name;
|
379
|
+
if (name !== undefined) {
|
380
|
+
const existing = registrations.filter((r)=>r.name === name);
|
381
|
+
check(existing.length === 0, `token ${getTokenName(token)} with name '${name}' is already registered`);
|
382
|
+
}
|
383
|
+
} else {
|
379
384
|
this.myMap.set(token, registrations = []);
|
380
|
-
} else if (registration.name !== undefined) {
|
381
|
-
const existing = registrations.filter((r)=>r.name === registration.name);
|
382
|
-
check(existing.length === 0, `a ${token.name} token named '${registration.name}' is already registered`);
|
383
385
|
}
|
384
386
|
registrations.push(registration);
|
385
387
|
}
|
@@ -475,7 +477,7 @@ function isDisposable(value) {
|
|
475
477
|
this.myParent = parent;
|
476
478
|
this.myOptions = {
|
477
479
|
autoRegister: false,
|
478
|
-
defaultScope: Scope.
|
480
|
+
defaultScope: Scope.Transient,
|
479
481
|
...options
|
480
482
|
};
|
481
483
|
this.myTokenRegistry = new TokenRegistry(this.myParent?.myTokenRegistry);
|
@@ -594,7 +596,7 @@ function isDisposable(value) {
|
|
594
596
|
}
|
595
597
|
} else {
|
596
598
|
if (existingProvider) {
|
597
|
-
check(token !== provider.useExisting, `
|
599
|
+
check(token !== provider.useExisting, `token ${getTokenName(token)} cannot alias itself via useExisting`);
|
598
600
|
}
|
599
601
|
this.myTokenRegistry.set(token, {
|
600
602
|
name: name,
|
@@ -741,16 +743,15 @@ function isDisposable(value) {
|
|
741
743
|
}
|
742
744
|
const resolution = context.resolution;
|
743
745
|
const provider = registration.provider;
|
744
|
-
const options = registration.options;
|
745
746
|
if (resolution.stack.has(provider)) {
|
746
747
|
const dependentRef = resolution.dependents.get(provider);
|
747
748
|
check(dependentRef, ()=>{
|
748
|
-
const path = resolution.tokenStack.map(
|
749
|
-
return `circular dependency detected while resolving ${path}
|
749
|
+
const path = resolution.tokenStack.map(getTokenName).concat(getTokenName(token)).join(" → ");
|
750
|
+
return `circular dependency detected while resolving ${path}`;
|
750
751
|
});
|
751
752
|
return dependentRef.current;
|
752
753
|
}
|
753
|
-
const scope =
|
754
|
+
const scope = registration.options?.scope ?? this.myOptions.defaultScope;
|
754
755
|
const cleanups = [
|
755
756
|
provideInjectionContext(context),
|
756
757
|
resolution.tokenStack.push(token) && (()=>resolution.tokenStack.pop()),
|
@@ -797,13 +798,6 @@ function isDisposable(value) {
|
|
797
798
|
cleanups.forEach((cleanup)=>cleanup && cleanup());
|
798
799
|
}
|
799
800
|
}
|
800
|
-
resolveScope(scope = this.myOptions.defaultScope, context = useInjectionContext()) {
|
801
|
-
if (scope === Scope.Inherited) {
|
802
|
-
const dependentFrame = context?.resolution.stack.peek();
|
803
|
-
return dependentFrame?.scope || Scope.Transient;
|
804
|
-
}
|
805
|
-
return scope;
|
806
|
-
}
|
807
801
|
resolveCtorDependencies(registration) {
|
808
802
|
const dependencies = registration.dependencies;
|
809
803
|
if (dependencies) {
|
@@ -881,7 +875,7 @@ function isDisposable(value) {
|
|
881
875
|
* Creates a new container.
|
882
876
|
*/ function createContainer(options = {
|
883
877
|
autoRegister: false,
|
884
|
-
defaultScope: Scope.
|
878
|
+
defaultScope: Scope.Transient
|
885
879
|
}) {
|
886
880
|
return new ContainerImpl(undefined, options);
|
887
881
|
}
|
@@ -1180,22 +1174,15 @@ function OptionalAll(token) {
|
|
1180
1174
|
* ```
|
1181
1175
|
*/ const Injector = /*@__PURE__*/ build(()=>{
|
1182
1176
|
const context = ensureInjectionContext("Injector factory");
|
1183
|
-
const resolution = context.resolution;
|
1184
|
-
const dependentFrame = resolution.stack.peek();
|
1185
|
-
const dependentRef = dependentFrame && resolution.dependents.get(dependentFrame.provider);
|
1186
1177
|
const runInContext = (fn)=>{
|
1187
1178
|
if (useInjectionContext()) {
|
1188
1179
|
return fn();
|
1189
1180
|
}
|
1190
|
-
const
|
1191
|
-
provideInjectionContext(context),
|
1192
|
-
dependentFrame && resolution.stack.push(dependentFrame.provider, dependentFrame),
|
1193
|
-
dependentRef && resolution.dependents.set(dependentFrame.provider, dependentRef)
|
1194
|
-
];
|
1181
|
+
const cleanup = provideInjectionContext(context);
|
1195
1182
|
try {
|
1196
1183
|
return fn();
|
1197
1184
|
} finally{
|
1198
|
-
|
1185
|
+
cleanup();
|
1199
1186
|
}
|
1200
1187
|
};
|
1201
1188
|
return {
|