@lppedd/di-wise-neo 0.11.1 → 0.12.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
@@ -171,10 +171,9 @@ function injectBy(thisArg, token, name) {
171
171
  if (!currentFrame) {
172
172
  return inject(token, name);
173
173
  }
174
- const currentRef = {
174
+ const cleanup = resolution.dependents.set(currentFrame.provider, {
175
175
  current: thisArg
176
- };
177
- const cleanup = resolution.dependents.set(currentFrame.provider, currentRef);
176
+ });
178
177
  try {
179
178
  return inject(token, name);
180
179
  } finally{
@@ -264,8 +263,8 @@ function getMetadata(Class) {
264
263
  * This allows libraries or consumers that manipulate constructors, such as through
265
264
  * class decorators, to inform the DI system about the real "identity" of a class.
266
265
  *
267
- * @param transformedClass The constructor function returned by a class decorator or factory.
268
- * @param originalClass The original constructor function.
266
+ * @param transformedClass - The constructor function returned by a class decorator or factory.
267
+ * @param originalClass - The original constructor function.
269
268
  *
270
269
  * @remarks
271
270
  * This API affects the core class identity resolution mechanism of the DI system.
@@ -280,7 +279,7 @@ const metadataMap = new WeakMap();
280
279
 
281
280
  function optional(token, name) {
282
281
  const context = ensureInjectionContext("optional()");
283
- return context.container.resolve(token, true, name);
282
+ return context.container.tryResolve(token, name);
284
283
  }
285
284
  function optionalBy(thisArg, token, name) {
286
285
  const context = ensureInjectionContext("optionalBy()");
@@ -289,10 +288,9 @@ function optionalBy(thisArg, token, name) {
289
288
  if (!currentFrame) {
290
289
  return optional(token, name);
291
290
  }
292
- const currentRef = {
291
+ const cleanup = resolution.dependents.set(currentFrame.provider, {
293
292
  current: thisArg
294
- };
295
- const cleanup = resolution.dependents.set(currentFrame.provider, currentRef);
293
+ });
296
294
  try {
297
295
  return optional(token, name);
298
296
  } finally{
@@ -302,7 +300,7 @@ function optionalBy(thisArg, token, name) {
302
300
 
303
301
  function optionalAll(token) {
304
302
  const context = ensureInjectionContext("optionalAll()");
305
- return context.container.resolveAll(token, true);
303
+ return context.container.tryResolveAll(token);
306
304
  }
307
305
 
308
306
  // @internal
@@ -323,9 +321,20 @@ function isExistingProvider(provider) {
323
321
  }
324
322
 
325
323
  const Scope = {
326
- Transient: "Transient",
327
- Resolution: "Resolution",
328
- Container: "Container"
324
+ /**
325
+ * Creates a new value every time the token is resolved.
326
+ */ Transient: "Transient",
327
+ /**
328
+ * Creates and caches a single value per token resolution graph.
329
+ *
330
+ * The same value is reused during a single resolution request and is subsequently discarded.
331
+ */ Resolution: "Resolution",
332
+ /**
333
+ * Creates and caches a single value per container.
334
+ *
335
+ * If the value is not found in the current container, it is looked up in the parent container,
336
+ * and so on. It effectively behaves like a _singleton_ scope but allows container-specific overrides.
337
+ */ Container: "Container"
329
338
  };
330
339
 
331
340
  /**
@@ -648,40 +657,21 @@ function isDisposable(value) {
648
657
  }
649
658
  return Array.from(values);
650
659
  }
651
- resolve(token, optionalOrName, name) {
660
+ resolve(token, name) {
652
661
  this.checkDisposed();
653
- let localOptional;
654
- let localName;
655
- if (typeof optionalOrName === "string") {
656
- localName = optionalOrName;
657
- } else {
658
- localOptional = optionalOrName;
659
- localName = name;
660
- }
661
- let registration = this.myTokenRegistry.get(token, localName);
662
- if (!registration && isConstructor(token)) {
663
- registration = this.autoRegisterClass(token, localName);
664
- }
665
- return this.resolveRegistration(token, registration, localOptional, localName);
662
+ return this.resolveToken(token, name, false);
666
663
  }
667
- resolveAll(token, optional) {
664
+ tryResolve(token, name) {
668
665
  this.checkDisposed();
669
- let registrations = this.myTokenRegistry.getAll(token);
670
- if (registrations.length === 0 && isConstructor(token)) {
671
- const registration = this.autoRegisterClass(token);
672
- if (registration) {
673
- registrations = [
674
- registration
675
- ];
676
- }
677
- }
678
- if (registrations.length === 0 && !optional) {
679
- throwUnregisteredError([
680
- token
681
- ]);
682
- }
683
- return registrations //
684
- .map((registration)=>this.resolveRegistration(token, registration, optional)).filter((value)=>value != null);
666
+ return this.resolveToken(token, name, true);
667
+ }
668
+ resolveAll(token) {
669
+ this.checkDisposed();
670
+ return this.resolveAllToken(token, false);
671
+ }
672
+ tryResolveAll(token) {
673
+ this.checkDisposed();
674
+ return this.resolveAllToken(token, true);
685
675
  }
686
676
  dispose() {
687
677
  if (this.myDisposed) {
@@ -708,6 +698,30 @@ function isDisposable(value) {
708
698
  // Allow values to be GCed
709
699
  disposedRefs.clear();
710
700
  }
701
+ resolveToken(token, name, optional) {
702
+ let registration = this.myTokenRegistry.get(token, name);
703
+ if (!registration && isConstructor(token)) {
704
+ registration = this.autoRegisterClass(token, name);
705
+ }
706
+ return this.resolveRegistration(token, registration, optional, name)?.value;
707
+ }
708
+ resolveAllToken(token, optional) {
709
+ let registrations = this.myTokenRegistry.getAll(token);
710
+ if (registrations.length === 0 && isConstructor(token)) {
711
+ const registration = this.autoRegisterClass(token);
712
+ if (registration) {
713
+ registrations = [
714
+ registration
715
+ ];
716
+ }
717
+ }
718
+ if (registrations.length === 0 && !optional) {
719
+ throwUnregisteredError([
720
+ token
721
+ ]);
722
+ }
723
+ return registrations.map((registration)=>this.resolveRegistration(token, registration, optional)).filter((result)=>result !== undefined).map((result)=>result.value);
724
+ }
711
725
  resolveRegistration(token, registration, optional, name) {
712
726
  const aliases = [];
713
727
  while(registration && isExistingProvider(registration.provider)){
@@ -741,7 +755,9 @@ function isDisposable(value) {
741
755
  ]);
742
756
  }
743
757
  try {
744
- return this.resolveProviderValue(token, registration);
758
+ return {
759
+ value: this.resolveProviderValue(token, registration)
760
+ };
745
761
  } catch (e) {
746
762
  throwResolutionError([
747
763
  token,
@@ -917,9 +933,9 @@ function isDisposable(value) {
917
933
  case "InjectAll":
918
934
  return instance ? injectAll(token) : this.resolveAll(token);
919
935
  case "Optional":
920
- return instance ? optionalBy(instance, token, name) : this.resolve(token, true, name);
936
+ return instance ? optionalBy(instance, token, name) : this.tryResolve(token, name);
921
937
  case "OptionalAll":
922
- return instance ? optionalAll(token) : this.resolveAll(token, true);
938
+ return instance ? optionalAll(token) : this.tryResolveAll(token);
923
939
  }
924
940
  }
925
941
  checkDisposed() {
@@ -937,7 +953,7 @@ function isDisposable(value) {
937
953
  }
938
954
 
939
955
  /**
940
- * Class decorator that enables auto-registration of an unregistered class,
956
+ * Class decorator that enables auto-registration of an unregistered class
941
957
  * when the class is first resolved from the container.
942
958
  *
943
959
  * @example
@@ -1245,14 +1261,14 @@ function OptionalAll(token) {
1245
1261
  injectAll: (token)=>runInContext(()=>injectAll(token)),
1246
1262
  optional: (token, name)=>runInContext(()=>optional(token, name)),
1247
1263
  optionalAll: (token)=>runInContext(()=>optionalAll(token)),
1248
- runInContext
1264
+ runInContext: runInContext
1249
1265
  };
1250
1266
  }, "Injector");
1251
1267
 
1252
1268
  /**
1253
1269
  * Applies middleware functions to a container.
1254
1270
  *
1255
- * Middlewares are applied in array order, but execute in reverse order.
1271
+ * Middlewares are applied in array order but execute in reverse order.
1256
1272
  *
1257
1273
  * @example
1258
1274
  * ```ts