@lppedd/di-wise-neo 0.9.4 → 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 +12 -28
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.d.mts +1 -2
- package/dist/es/index.mjs +12 -28
- 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
28
|
const tokenName = getTokenName(dependency.tokenRef.getRefToken());
|
29
|
-
const
|
30
|
-
throw new Error(`${
|
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"
|
@@ -478,7 +477,7 @@ function isDisposable(value) {
|
|
478
477
|
this.myParent = parent;
|
479
478
|
this.myOptions = {
|
480
479
|
autoRegister: false,
|
481
|
-
defaultScope: Scope.
|
480
|
+
defaultScope: Scope.Transient,
|
482
481
|
...options
|
483
482
|
};
|
484
483
|
this.myTokenRegistry = new TokenRegistry(this.myParent?.myTokenRegistry);
|
@@ -744,16 +743,15 @@ function isDisposable(value) {
|
|
744
743
|
}
|
745
744
|
const resolution = context.resolution;
|
746
745
|
const provider = registration.provider;
|
747
|
-
const options = registration.options;
|
748
746
|
if (resolution.stack.has(provider)) {
|
749
747
|
const dependentRef = resolution.dependents.get(provider);
|
750
748
|
check(dependentRef, ()=>{
|
751
|
-
const path = resolution.tokenStack.map(getTokenName).join(" → ");
|
752
|
-
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}`;
|
753
751
|
});
|
754
752
|
return dependentRef.current;
|
755
753
|
}
|
756
|
-
const scope =
|
754
|
+
const scope = registration.options?.scope ?? this.myOptions.defaultScope;
|
757
755
|
const cleanups = [
|
758
756
|
provideInjectionContext(context),
|
759
757
|
resolution.tokenStack.push(token) && (()=>resolution.tokenStack.pop()),
|
@@ -800,13 +798,6 @@ function isDisposable(value) {
|
|
800
798
|
cleanups.forEach((cleanup)=>cleanup && cleanup());
|
801
799
|
}
|
802
800
|
}
|
803
|
-
resolveScope(scope = this.myOptions.defaultScope, context = useInjectionContext()) {
|
804
|
-
if (scope === Scope.Inherited) {
|
805
|
-
const dependentFrame = context?.resolution.stack.peek();
|
806
|
-
return dependentFrame?.scope || Scope.Transient;
|
807
|
-
}
|
808
|
-
return scope;
|
809
|
-
}
|
810
801
|
resolveCtorDependencies(registration) {
|
811
802
|
const dependencies = registration.dependencies;
|
812
803
|
if (dependencies) {
|
@@ -884,7 +875,7 @@ function isDisposable(value) {
|
|
884
875
|
* Creates a new container.
|
885
876
|
*/ function createContainer(options = {
|
886
877
|
autoRegister: false,
|
887
|
-
defaultScope: Scope.
|
878
|
+
defaultScope: Scope.Transient
|
888
879
|
}) {
|
889
880
|
return new ContainerImpl(undefined, options);
|
890
881
|
}
|
@@ -1183,22 +1174,15 @@ function OptionalAll(token) {
|
|
1183
1174
|
* ```
|
1184
1175
|
*/ const Injector = /*@__PURE__*/ build(()=>{
|
1185
1176
|
const context = ensureInjectionContext("Injector factory");
|
1186
|
-
const resolution = context.resolution;
|
1187
|
-
const dependentFrame = resolution.stack.peek();
|
1188
|
-
const dependentRef = dependentFrame && resolution.dependents.get(dependentFrame.provider);
|
1189
1177
|
const runInContext = (fn)=>{
|
1190
1178
|
if (useInjectionContext()) {
|
1191
1179
|
return fn();
|
1192
1180
|
}
|
1193
|
-
const
|
1194
|
-
provideInjectionContext(context),
|
1195
|
-
dependentFrame && resolution.stack.push(dependentFrame.provider, dependentFrame),
|
1196
|
-
dependentRef && resolution.dependents.set(dependentFrame.provider, dependentRef)
|
1197
|
-
];
|
1181
|
+
const cleanup = provideInjectionContext(context);
|
1198
1182
|
try {
|
1199
1183
|
return fn();
|
1200
1184
|
} finally{
|
1201
|
-
|
1185
|
+
cleanup();
|
1202
1186
|
}
|
1203
1187
|
};
|
1204
1188
|
return {
|