injectus 0.2.0 → 0.2.1
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/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -1
- package/dist/injector.js +11 -13
- package/package.json +1 -1
package/dist/errors.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare abstract class DependencyPathError extends Error {
|
|
|
8
8
|
constructor(leaf: Token);
|
|
9
9
|
/** Full dependency path, root-to-leaf. */
|
|
10
10
|
get path(): readonly Token[];
|
|
11
|
-
/** Prepend `token` to `error`'s path as the exception unwinds one frame. */
|
|
11
|
+
/** @internal Prepend `token` to `error`'s path as the exception unwinds one frame. */
|
|
12
12
|
static prepend(error: DependencyPathError, token: Token): void;
|
|
13
13
|
}
|
|
14
14
|
/** Thrown when a cycle is detected in the dependency graph. The message renders the full path root-to-leaf. */
|
package/dist/errors.js
CHANGED
|
@@ -13,7 +13,7 @@ export class DependencyPathError extends Error {
|
|
|
13
13
|
get path() {
|
|
14
14
|
return this.#path;
|
|
15
15
|
}
|
|
16
|
-
/** Prepend `token` to `error`'s path as the exception unwinds one frame. */
|
|
16
|
+
/** @internal Prepend `token` to `error`'s path as the exception unwinds one frame. */
|
|
17
17
|
static prepend(error, token) {
|
|
18
18
|
error.#path.unshift(token);
|
|
19
19
|
}
|
package/dist/injector.js
CHANGED
|
@@ -60,17 +60,21 @@ export class Injector {
|
|
|
60
60
|
throw new TokenNotFoundError(token, this.name);
|
|
61
61
|
}
|
|
62
62
|
const { binding, injector } = found;
|
|
63
|
-
const
|
|
63
|
+
const prevLifetime = getInjectionContext()?.effectiveLifetime;
|
|
64
|
+
if (prevLifetime === Lifetime.Singleton &&
|
|
65
|
+
binding.lifetime === Lifetime.Scoped) {
|
|
66
|
+
throw new CaptiveDependencyError(token);
|
|
67
|
+
}
|
|
68
|
+
const isNotSelf = injector !== this;
|
|
69
|
+
const owner =
|
|
64
70
|
// Singleton caches on owner; factory must run under owner's chain or child shadow poisons parent cache
|
|
65
|
-
|
|
66
|
-
? injector
|
|
67
|
-
: this;
|
|
71
|
+
isNotSelf && binding.lifetime === Lifetime.Singleton ? injector : this;
|
|
68
72
|
const prevInjectContext = setInjectionContext({
|
|
69
73
|
injector: owner,
|
|
70
|
-
effectiveLifetime: minLifetime(binding.lifetime,
|
|
74
|
+
effectiveLifetime: minLifetime(binding.lifetime, prevLifetime),
|
|
71
75
|
});
|
|
72
76
|
try {
|
|
73
|
-
if (binding.lifetime === Lifetime.Scoped
|
|
77
|
+
if (isNotSelf && binding.lifetime === Lifetime.Scoped) {
|
|
74
78
|
const scopedBinding = makeBinding(binding.factory, EMPTY, Lifetime.Scoped);
|
|
75
79
|
this.#bindings.set(token, scopedBinding);
|
|
76
80
|
return this.hydrate(token, scopedBinding, options);
|
|
@@ -100,12 +104,6 @@ export class Injector {
|
|
|
100
104
|
if (binding.value !== EMPTY) {
|
|
101
105
|
return binding.value;
|
|
102
106
|
}
|
|
103
|
-
const lifetime = binding.lifetime;
|
|
104
|
-
const ctx = getInjectionContext();
|
|
105
|
-
if (lifetime === Lifetime.Scoped &&
|
|
106
|
-
ctx.effectiveLifetime === Lifetime.Singleton) {
|
|
107
|
-
throw new CaptiveDependencyError(token);
|
|
108
|
-
}
|
|
109
107
|
binding.value = CIRCULAR;
|
|
110
108
|
let instance;
|
|
111
109
|
try {
|
|
@@ -118,7 +116,7 @@ export class Injector {
|
|
|
118
116
|
}
|
|
119
117
|
throw e;
|
|
120
118
|
}
|
|
121
|
-
if (lifetime === Lifetime.Transient) {
|
|
119
|
+
if (binding.lifetime === Lifetime.Transient) {
|
|
122
120
|
binding.value = EMPTY;
|
|
123
121
|
}
|
|
124
122
|
else {
|
package/package.json
CHANGED