@lppedd/di-wise-neo 0.20.0 → 0.21.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.
@@ -627,11 +627,13 @@ interface Container {
627
627
  * Disposes this container and all its cached values.
628
628
  *
629
629
  * Token values implementing a `Disposable` interface (e.g., objects with a `dispose()` function)
630
- * are automatically disposed during this process.
630
+ * are also disposed. All disposals, whether synchronous or asynchronous, are returned as promises
631
+ * in an array. Callers may await these promises (e.g., using `Promise.allSettled()`) if they want
632
+ * to ensure that all async work has completed.
631
633
  *
632
634
  * Note that children containers are disposed first, in creation order.
633
635
  */
634
- dispose(): void;
636
+ dispose(): Promise<unknown>[];
635
637
  }
636
638
  /**
637
639
  * Creates a new container.
package/dist/es/index.mjs CHANGED
@@ -55,7 +55,7 @@ function getTokenName(token) {
55
55
  }
56
56
  function getFullTokenName([token, name]) {
57
57
  const tokenName = token ? token.name || "<unnamed>" : "<undefined token>";
58
- return name ? `${tokenName}["${name}"]` : tokenName;
58
+ return name !== undefined ? `${tokenName}["${name}"]` : tokenName;
59
59
  }
60
60
  function getCause(error) {
61
61
  if (!error) {
@@ -102,7 +102,7 @@ class KeyedStack {
102
102
  return this.myEntries.at(-1)?.value;
103
103
  }
104
104
  push(key, value) {
105
- check(!this.has(key), "invariant violation");
105
+ check(!this.has(key), "internal: invariant violation");
106
106
  this.myKeys.add(key);
107
107
  this.myEntries.push({
108
108
  key,
@@ -133,7 +133,7 @@ class WeakRefMap {
133
133
  return undefined;
134
134
  }
135
135
  set(key, value) {
136
- check(!this.get(key), "invariant violation");
136
+ check(!this.get(key), "internal: invariant violation");
137
137
  this.myMap.set(key, new WeakRef(value));
138
138
  return ()=>{
139
139
  this.myMap.delete(key);
@@ -228,7 +228,7 @@ function tokenRef(token) {
228
228
  return {
229
229
  getRefToken: ()=>{
230
230
  const tokenOrTokens = token();
231
- check(!Array.isArray(tokenOrTokens), "internal error: single token expected");
231
+ check(!Array.isArray(tokenOrTokens), "internal: unexpected array of tokens");
232
232
  return tokenOrTokens;
233
233
  },
234
234
  getRefTokens: ()=>{
@@ -260,7 +260,7 @@ class Metadata {
260
260
  this.tokenRef = {
261
261
  // prettier-ignore
262
262
  getRefToken: ()=>{
263
- check(false, "internal error: unexpected call");
263
+ check(false, "internal: unexpected getRefToken call");
264
264
  },
265
265
  getRefTokens: ()=>new Set()
266
266
  };
@@ -469,6 +469,7 @@ function isConstructor(token) {
469
469
 
470
470
  // @internal
471
471
  function getTypeName(value) {
472
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
472
473
  switch(typeof value){
473
474
  case "string":
474
475
  return `"${value}"`;
@@ -509,7 +510,7 @@ class TokenRegistry {
509
510
  ] || this.getAllFromParent(token, name);
510
511
  }
511
512
  put(token, registration) {
512
- check(!internals.has(token), `cannot register reserved token ${token.name}`);
513
+ check(!internals.has(token), `internal: cannot register reserved token ${token.name}`);
513
514
  let registrations = this.myRegistrations.get(token);
514
515
  if (registrations) {
515
516
  const name = registration.name;
@@ -542,8 +543,12 @@ class TokenRegistry {
542
543
  return registrations;
543
544
  }
544
545
  deleteAll() {
545
- const tokens = Array.from(this.myRegistrations.keys());
546
- const registrations = Array.from(this.myRegistrations.values()).flat();
546
+ const tokens = [
547
+ ...this.myRegistrations.keys()
548
+ ];
549
+ const registrations = [
550
+ ...this.myRegistrations.values()
551
+ ].flat();
547
552
  this.myRegistrations.clear();
548
553
  return [
549
554
  tokens,
@@ -561,13 +566,15 @@ class TokenRegistry {
561
566
  registration.valueRef = undefined;
562
567
  }
563
568
  }
564
- return Array.from(values);
569
+ return [
570
+ ...values
571
+ ];
565
572
  }
566
573
  getAllFromParent(token, name) {
567
574
  let registrations = this.myRegistrations.get(token) || this.myParent?.getAllFromParent(token, name);
568
575
  if (registrations && name !== undefined) {
569
576
  registrations = registrations.filter((r)=>r.name === name);
570
- check(registrations.length < 2, `internal error: more than one registration named '${name}'`);
577
+ check(registrations.length < 2, `internal: multiple registrations with name '${name}'`);
571
578
  }
572
579
  return registrations ?? [];
573
580
  }
@@ -659,7 +666,9 @@ function isDisposable(value) {
659
666
  values.add(valueRef.current);
660
667
  }
661
668
  }
662
- return Array.from(values);
669
+ return [
670
+ ...values
671
+ ];
663
672
  }
664
673
  resetRegistry() {
665
674
  this.checkDisposed();
@@ -670,7 +679,9 @@ function isDisposable(value) {
670
679
  values.add(valueRef.current);
671
680
  }
672
681
  }
673
- return Array.from(values);
682
+ return [
683
+ ...values
684
+ ];
674
685
  }
675
686
  isRegistered(token, name) {
676
687
  this.checkDisposed();
@@ -699,7 +710,9 @@ function isDisposable(value) {
699
710
  values.add(valueRef.current);
700
711
  }
701
712
  }
702
- return Array.from(values);
713
+ return [
714
+ ...values
715
+ ];
703
716
  }
704
717
  resolve(token, name) {
705
718
  this.checkDisposed();
@@ -718,20 +731,23 @@ function isDisposable(value) {
718
731
  return this.resolveAllToken(token, true);
719
732
  }
720
733
  addHook(hook) {
734
+ this.checkDisposed();
721
735
  this.myHookRegistry.add(hook);
722
736
  }
723
737
  removeHook(hook) {
738
+ this.checkDisposed();
724
739
  this.myHookRegistry.delete(hook);
725
740
  }
726
741
  dispose() {
727
742
  if (this.myDisposed) {
728
- return;
743
+ return [];
729
744
  }
745
+ this.myDisposed = true;
746
+ const promises = [];
730
747
  // Dispose children containers first
731
748
  for (const child of this.myChildren){
732
- child.dispose();
749
+ promises.push(...child.dispose());
733
750
  }
734
- this.myDisposed = true;
735
751
  this.myChildren.clear();
736
752
  this.myParent?.myChildren?.delete(this);
737
753
  const [, registrations] = this.myTokenRegistry.deleteAll();
@@ -748,11 +764,18 @@ function isDisposable(value) {
748
764
  }
749
765
  for (const value of allValues){
750
766
  if (isDisposable(value)) {
751
- value.dispose();
767
+ try {
768
+ promises.push(Promise.resolve(value.dispose()));
769
+ } catch (e) {
770
+ promises.push(Promise.reject(e));
771
+ }
752
772
  }
753
773
  }
754
- this.notifyDisposeHooks(Array.from(cacheValues));
774
+ this.notifyDisposeHooks([
775
+ ...cacheValues
776
+ ]);
755
777
  this.myHookRegistry.clear();
778
+ return promises;
756
779
  }
757
780
  registerClass(Class) {
758
781
  const metadata = getMetadata(Class);
@@ -926,7 +949,7 @@ function isDisposable(value) {
926
949
  if (isValueProvider(provider)) {
927
950
  return provider.useValue;
928
951
  }
929
- check(false, "internal error: unexpected ExistingProvider");
952
+ check(false, "internal: unexpected ExistingProvider");
930
953
  }
931
954
  resolveScopedValue(token, registration, factory) {
932
955
  let context = useInjectionContext();
@@ -1002,7 +1025,7 @@ function isDisposable(value) {
1002
1025
  resolveCtorDependencies(registration) {
1003
1026
  const dependencies = registration.dependencies;
1004
1027
  if (dependencies) {
1005
- check(isClassProvider(registration.provider), `internal error: not a ClassProvider`);
1028
+ check(isClassProvider(registration.provider), `internal: expected a ClassProvider`);
1006
1029
  const ctorDeps = dependencies.ctor.filter((d)=>d.appliedBy);
1007
1030
  if (ctorDeps.length > 0) {
1008
1031
  // Let's check if all necessary constructor parameters are decorated.
@@ -1021,7 +1044,7 @@ function isDisposable(value) {
1021
1044
  injectMethodDependencies(registration, instance) {
1022
1045
  const dependencies = registration.dependencies;
1023
1046
  if (dependencies) {
1024
- check(isClassProvider(registration.provider), `internal error: not a ClassProvider`);
1047
+ check(isClassProvider(registration.provider), `internal: expected a ClassProvider`);
1025
1048
  const ctor = registration.provider.useClass;
1026
1049
  // Perform method injection
1027
1050
  for (const entry of dependencies.methods){
@@ -1068,6 +1091,8 @@ function isDisposable(value) {
1068
1091
  return instance ? optionalBy(instance, token, name) : this.tryResolve(token, name);
1069
1092
  case "OptionalAll":
1070
1093
  return instance ? optionalAll(token) : this.tryResolveAll(token);
1094
+ case undefined:
1095
+ check(false, "internal: unexpected undefined appliedBy");
1071
1096
  }
1072
1097
  }
1073
1098
  notifyProvideHooks(value, scope) {