@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.
@@ -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.Inherited
205
+ * @defaultValue Scope.Transient
207
206
  */
208
207
  readonly defaultScope: Scope;
209
208
  }
package/dist/es/index.mjs CHANGED
@@ -15,17 +15,17 @@ function throwUnregisteredError(token, name) {
15
15
  }
16
16
  // @internal
17
17
  function throwExistingUnregisteredError(token, cause) {
18
- const message = tag(`failed to resolve token ${getTokenName(token)}`);
19
- throw isError(cause) ? new Error(`${message}\n [cause] ${untag(cause.message)}`, {
18
+ const msg = tag(`failed to resolve token ${getTokenName(token)}`);
19
+ throw isError(cause) ? new Error(`${msg}\n [cause] ${untag(cause.message)}`, {
20
20
  cause
21
- }) : new Error(`${message}\n [cause] the aliased token ${getTokenName(cause)} is not registered`);
21
+ }) : new Error(`${msg}\n [cause] the aliased token ${getTokenName(cause)} is not registered`);
22
22
  }
23
23
  // @internal
24
24
  function throwParameterResolutionError(ctor, methodKey, dependency, cause) {
25
25
  const location = getLocation(ctor, methodKey);
26
- const token = dependency.tokenRef.getRefToken();
27
- const message = tag(`failed to resolve dependency for ${location}(parameter #${dependency.index}: ${token.name})`);
28
- throw new Error(`${message}\n [cause] ${untag(cause.message)}`, {
26
+ const tokenName = getTokenName(dependency.tokenRef.getRefToken());
27
+ const msg = tag(`failed to resolve dependency for ${location}(parameter #${dependency.index}: ${tokenName})`);
28
+ throw new Error(`${msg}\n [cause] ${untag(cause.message)}`, {
29
29
  cause
30
30
  });
31
31
  }
@@ -296,7 +296,6 @@ function isExistingProvider(provider) {
296
296
  }
297
297
 
298
298
  const Scope = {
299
- Inherited: "Inherited",
300
299
  Transient: "Transient",
301
300
  Resolution: "Resolution",
302
301
  Container: "Container"
@@ -373,11 +372,14 @@ class TokenRegistry {
373
372
  set(token, registration) {
374
373
  check(!internals.has(token), `cannot register reserved token ${token.name}`);
375
374
  let registrations = this.myMap.get(token);
376
- if (!registrations) {
375
+ if (registrations) {
376
+ const name = registration.name;
377
+ if (name !== undefined) {
378
+ const existing = registrations.filter((r)=>r.name === name);
379
+ check(existing.length === 0, `token ${getTokenName(token)} with name '${name}' is already registered`);
380
+ }
381
+ } else {
377
382
  this.myMap.set(token, registrations = []);
378
- } else if (registration.name !== undefined) {
379
- const existing = registrations.filter((r)=>r.name === registration.name);
380
- check(existing.length === 0, `a ${token.name} token named '${registration.name}' is already registered`);
381
383
  }
382
384
  registrations.push(registration);
383
385
  }
@@ -473,7 +475,7 @@ function isDisposable(value) {
473
475
  this.myParent = parent;
474
476
  this.myOptions = {
475
477
  autoRegister: false,
476
- defaultScope: Scope.Inherited,
478
+ defaultScope: Scope.Transient,
477
479
  ...options
478
480
  };
479
481
  this.myTokenRegistry = new TokenRegistry(this.myParent?.myTokenRegistry);
@@ -592,7 +594,7 @@ function isDisposable(value) {
592
594
  }
593
595
  } else {
594
596
  if (existingProvider) {
595
- check(token !== provider.useExisting, `the useExisting token ${token.name} cannot be the same as the token being registered`);
597
+ check(token !== provider.useExisting, `token ${getTokenName(token)} cannot alias itself via useExisting`);
596
598
  }
597
599
  this.myTokenRegistry.set(token, {
598
600
  name: name,
@@ -739,16 +741,15 @@ function isDisposable(value) {
739
741
  }
740
742
  const resolution = context.resolution;
741
743
  const provider = registration.provider;
742
- const options = registration.options;
743
744
  if (resolution.stack.has(provider)) {
744
745
  const dependentRef = resolution.dependents.get(provider);
745
746
  check(dependentRef, ()=>{
746
- const path = resolution.tokenStack.map((t)=>t.name).join(" → ");
747
- return `circular dependency detected while resolving ${path} → ${token.name}`;
747
+ const path = resolution.tokenStack.map(getTokenName).concat(getTokenName(token)).join(" → ");
748
+ return `circular dependency detected while resolving ${path}`;
748
749
  });
749
750
  return dependentRef.current;
750
751
  }
751
- const scope = this.resolveScope(options?.scope, context);
752
+ const scope = registration.options?.scope ?? this.myOptions.defaultScope;
752
753
  const cleanups = [
753
754
  provideInjectionContext(context),
754
755
  resolution.tokenStack.push(token) && (()=>resolution.tokenStack.pop()),
@@ -795,13 +796,6 @@ function isDisposable(value) {
795
796
  cleanups.forEach((cleanup)=>cleanup && cleanup());
796
797
  }
797
798
  }
798
- resolveScope(scope = this.myOptions.defaultScope, context = useInjectionContext()) {
799
- if (scope === Scope.Inherited) {
800
- const dependentFrame = context?.resolution.stack.peek();
801
- return dependentFrame?.scope || Scope.Transient;
802
- }
803
- return scope;
804
- }
805
799
  resolveCtorDependencies(registration) {
806
800
  const dependencies = registration.dependencies;
807
801
  if (dependencies) {
@@ -879,7 +873,7 @@ function isDisposable(value) {
879
873
  * Creates a new container.
880
874
  */ function createContainer(options = {
881
875
  autoRegister: false,
882
- defaultScope: Scope.Inherited
876
+ defaultScope: Scope.Transient
883
877
  }) {
884
878
  return new ContainerImpl(undefined, options);
885
879
  }
@@ -1178,22 +1172,15 @@ function OptionalAll(token) {
1178
1172
  * ```
1179
1173
  */ const Injector = /*@__PURE__*/ build(()=>{
1180
1174
  const context = ensureInjectionContext("Injector factory");
1181
- const resolution = context.resolution;
1182
- const dependentFrame = resolution.stack.peek();
1183
- const dependentRef = dependentFrame && resolution.dependents.get(dependentFrame.provider);
1184
1175
  const runInContext = (fn)=>{
1185
1176
  if (useInjectionContext()) {
1186
1177
  return fn();
1187
1178
  }
1188
- const cleanups = [
1189
- provideInjectionContext(context),
1190
- dependentFrame && resolution.stack.push(dependentFrame.provider, dependentFrame),
1191
- dependentRef && resolution.dependents.set(dependentFrame.provider, dependentRef)
1192
- ];
1179
+ const cleanup = provideInjectionContext(context);
1193
1180
  try {
1194
1181
  return fn();
1195
1182
  } finally{
1196
- cleanups.forEach((cleanup)=>cleanup?.());
1183
+ cleanup();
1197
1184
  }
1198
1185
  };
1199
1186
  return {