@lppedd/di-wise-neo 0.8.1 → 0.9.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/dist/es/index.mjs CHANGED
@@ -36,9 +36,10 @@ function expectNever(value) {
36
36
  throw new TypeError(tag(`unexpected value ${String(value)}`));
37
37
  }
38
38
  // @internal
39
- function throwUnregisteredError(token) {
39
+ function throwUnregisteredError(token, name) {
40
40
  const type = isConstructor(token) ? "class" : "token";
41
- throw new Error(tag(`unregistered ${type} ${token.name}`));
41
+ const spec = name !== undefined ? `[name=${name}]` : "";
42
+ throw new Error(tag(`unregistered ${type} ${token.name}${spec}`));
42
43
  }
43
44
  // @internal
44
45
  function throwExistingUnregisteredError(sourceToken, targetTokenOrError) {
@@ -102,6 +103,7 @@ class WeakRefMap {
102
103
  }
103
104
  this.myMap.delete(key);
104
105
  }
106
+ return undefined;
105
107
  }
106
108
  set(key, value) {
107
109
  invariant(!this.get(key));
@@ -613,29 +615,30 @@ function isDisposable(value) {
613
615
  localOptional = optionalOrName;
614
616
  localName = name;
615
617
  }
616
- const registration = this.myTokenRegistry.get(token, localName);
618
+ let registration = this.myTokenRegistry.get(token, localName);
619
+ if (!registration && isConstructor(token)) {
620
+ registration = this.autoRegisterClass(token, localName);
621
+ }
617
622
  if (registration) {
618
623
  return this.resolveRegistration(token, registration, localName);
619
624
  }
620
- if (isConstructor(token)) {
621
- return this.instantiateClass(token, localOptional);
622
- }
623
- return localOptional ? undefined : throwUnregisteredError(token);
625
+ return localOptional ? undefined : throwUnregisteredError(token, localName);
624
626
  }
625
627
  resolveAll(token, optional) {
626
628
  this.checkDisposed();
627
- const registrations = this.myTokenRegistry.getAll(token);
629
+ let registrations = this.myTokenRegistry.getAll(token);
630
+ if (registrations.length === 0 && isConstructor(token)) {
631
+ const registration = this.autoRegisterClass(token);
632
+ if (registration) {
633
+ registrations = [
634
+ registration
635
+ ];
636
+ }
637
+ }
628
638
  if (registrations.length > 0) {
629
639
  return registrations //
630
640
  .map((registration)=>this.resolveRegistration(token, registration)).filter((value)=>value != null);
631
641
  }
632
- if (isConstructor(token)) {
633
- const instance = this.instantiateClass(token, optional);
634
- return instance === undefined // = could not resolve, but since it is optional
635
- ? [] : [
636
- instance
637
- ];
638
- }
639
642
  return optional ? [] : throwUnregisteredError(token);
640
643
  }
641
644
  dispose() {
@@ -685,37 +688,22 @@ function isDisposable(value) {
685
688
  throw e;
686
689
  }
687
690
  }
688
- instantiateClass(Class, optional) {
691
+ autoRegisterClass(Class, name) {
689
692
  const metadata = getMetadata(Class);
690
- if (metadata.autoRegister ?? this.myOptions.autoRegister) {
691
- // Temporarily set eagerInstantiate to false to avoid resolving the class two times:
692
- // one inside register(), and the other just below
693
+ const autoRegister = metadata.autoRegister ?? this.myOptions.autoRegister;
694
+ if (autoRegister && (name === undefined || metadata.name === name)) {
695
+ // Temporarily set eagerInstantiate to false to avoid potentially resolving
696
+ // the class inside register()
693
697
  const eagerInstantiate = metadata.eagerInstantiate;
694
698
  metadata.eagerInstantiate = false;
695
699
  try {
696
700
  this.register(Class);
697
- return this.resolve(Class);
701
+ return this.myTokenRegistry.get(Class, name ?? metadata.name);
698
702
  } finally{
699
703
  metadata.eagerInstantiate = eagerInstantiate;
700
704
  }
701
705
  }
702
- const scope = this.resolveScope(metadata.scope?.value);
703
- if (optional && scope === Scope.Container) {
704
- // It would not be possible to resolve the class in container scope,
705
- // as that would require prior registration.
706
- // However, since resolution is marked optional, we simply return undefined.
707
- return undefined;
708
- }
709
- assert(scope !== Scope.Container, `unregistered class ${Class.name} cannot be resolved in container scope`);
710
- const registration = {
711
- provider: metadata.provider,
712
- options: {
713
- scope: scope
714
- },
715
- dependencies: metadata.dependencies
716
- };
717
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
718
- return this.resolveScopedValue(registration, (args)=>new Class(...args));
706
+ return undefined;
719
707
  }
720
708
  resolveProviderValue(registration, provider) {
721
709
  assert(registration.provider === provider, "internal error: mismatching provider");