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