@lppedd/di-wise-neo 0.11.0 → 0.11.2

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.
@@ -116,11 +116,11 @@ interface FactoryProvider<Value> {
116
116
  /**
117
117
  * Provides a static - already constructed - value for a token.
118
118
  */
119
- interface ValueProvider<T> {
119
+ interface ValueProvider<Value> {
120
120
  /**
121
121
  * The static value to associate with the token.
122
122
  */
123
- readonly useValue: T;
123
+ readonly useValue: Value;
124
124
  /**
125
125
  * An optional name to qualify this provider.
126
126
  * If specified, the token must be resolved using the same name.
@@ -147,10 +147,7 @@ interface ExistingProvider<Value> {
147
147
  /**
148
148
  * The existing token to alias.
149
149
  */
150
- readonly useExisting: Token<Value> | {
151
- readonly token: Token<Value>;
152
- readonly name?: string;
153
- };
150
+ readonly useExisting: Token<Value> | [Token<Value>, string?];
154
151
  /**
155
152
  * An optional name to qualify this provider.
156
153
  * If specified, the token must be resolved using the same name.
@@ -434,9 +431,9 @@ interface Container {
434
431
  *
435
432
  * @see The documentation for `resolve(token: Token)`
436
433
  */
437
- resolveAll<Value>(token: Token<Value>, optional?: false): NonNullable<Value>[];
438
- resolveAll<Value>(token: Token<Value>, optional: true): NonNullable<Value>[];
439
- resolveAll<Value>(token: Token<Value>, optional?: boolean): NonNullable<Value>[];
434
+ resolveAll<Value>(token: Token<Value>, optional?: false): Value[];
435
+ resolveAll<Value>(token: Token<Value>, optional: true): Value[];
436
+ resolveAll<Value>(token: Token<Value>, optional?: boolean): Value[];
440
437
  /**
441
438
  * Disposes this container and all its cached values.
442
439
  *
@@ -453,7 +450,7 @@ interface Container {
453
450
  declare function createContainer(options?: Partial<ContainerOptions>): Container;
454
451
 
455
452
  /**
456
- * Class decorator that enables auto-registration of an unregistered class,
453
+ * Class decorator that enables auto-registration of an unregistered class
457
454
  * when the class is first resolved from the container.
458
455
  *
459
456
  * @example
@@ -497,12 +494,12 @@ interface TokenRef<Value = any> {
497
494
  readonly getRefToken: () => Token<Value>;
498
495
  }
499
496
  /**
500
- * Allows referencing tokens that are declared later in the file by wrapping them
497
+ * Allows referencing tokens declared later in the file by wrapping them
501
498
  * in a lazily evaluated function.
502
499
  */
503
500
  declare function forwardRef<Value>(token: () => Tokens<Value>): TokensRef<Value>;
504
501
  /**
505
- * Allows referencing a token that is declared later in the file by wrapping it
502
+ * Allows referencing a token declared later in the file by wrapping it
506
503
  * in a lazily evaluated function.
507
504
  */
508
505
  declare function forwardRef<Value>(token: () => Token<Value>): TokenRef<Value>;
@@ -522,8 +519,8 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
522
519
  /**
523
520
  * Parameter decorator that injects the value associated with the given token.
524
521
  *
525
- * Allows referencing a token that is declared later in the file by using
526
- * the {@link forwardRef} helper function.
522
+ * Allows referencing a token declared later in the file by using the
523
+ * {@link forwardRef} helper function.
527
524
  *
528
525
  * Throws an error if the token is not registered in the container.
529
526
  *
@@ -539,7 +536,7 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
539
536
  declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
540
537
 
541
538
  /**
542
- * Class decorator that registers additional aliasing tokens for the decorated type,
539
+ * Class decorator that registers additional aliasing tokens for the decorated type
543
540
  * when the type is first registered in the container.
544
541
  *
545
542
  * The container uses {@link ExistingProvider} under the hood.
@@ -552,7 +549,7 @@ declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
552
549
  */
553
550
  declare function Injectable<This extends object, Value extends This>(...tokens: Tokens<Value>): ClassDecorator;
554
551
  /**
555
- * Class decorator that registers additional aliasing tokens for the decorated type,
552
+ * Class decorator that registers additional aliasing tokens for the decorated type
556
553
  * when the type is first registered in the container.
557
554
  *
558
555
  * The container uses {@link ExistingProvider} under the hood.
@@ -588,8 +585,8 @@ declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
588
585
  * Parameter decorator that injects all values provided by the registrations
589
586
  * associated with the given token.
590
587
  *
591
- * Allows referencing a token that is declared later in the file by using
592
- * the {@link forwardRef} helper function.
588
+ * Allows referencing a token declared later in the file by using the
589
+ * {@link forwardRef} helper function.
593
590
  *
594
591
  * Throws an error if the token is not registered in the container.
595
592
  *
@@ -638,8 +635,8 @@ declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
638
635
  * Parameter decorator that injects the value associated with the given token,
639
636
  * or `undefined` if the token is not registered in the container.
640
637
  *
641
- * Allows referencing a token that is declared later in the file by using
642
- * the {@link forwardRef} helper function.
638
+ * Allows referencing a token declared later in the file by using the
639
+ * {@link forwardRef} helper function.
643
640
  *
644
641
  * @example
645
642
  * ```ts
@@ -654,23 +651,23 @@ declare function Optional<Value>(tokens: TokenRef<Value>): ParameterDecorator;
654
651
 
655
652
  /**
656
653
  * Parameter decorator that injects all instances provided by the registrations
657
- * associated with the given class, or an empty array if the class is not registered
654
+ * associated with the given class or an empty array if the class is not registered
658
655
  * in the container.
659
656
  */
660
657
  declare function OptionalAll<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
661
658
  /**
662
659
  * Parameter decorator that injects all values provided by the registrations
663
- * associated with the given token, or an empty array if the token is not registered
660
+ * associated with the given token or an empty array if the token is not registered
664
661
  * in the container.
665
662
  */
666
663
  declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
667
664
  /**
668
665
  * Parameter decorator that injects all values provided by the registrations
669
- * associated with the given token, or an empty array if the token is not registered
666
+ * associated with the given token or an empty array if the token is not registered
670
667
  * in the container.
671
668
  *
672
- * Allows referencing a token that is declared later in the file by using
673
- * the {@link forwardRef} helper function.
669
+ * Allows referencing a token declared later in the file by using the
670
+ * {@link forwardRef} helper function.
674
671
  *
675
672
  * @example
676
673
  * ```ts
@@ -779,7 +776,7 @@ declare function injectAll<Instance extends object>(Class: Constructor<Instance>
779
776
  *
780
777
  * Throws an error if the token is not registered in the container.
781
778
  */
782
- declare function injectAll<Value>(token: Token<Value>): NonNullable<Value>[];
779
+ declare function injectAll<Value>(token: Token<Value>): Value[];
783
780
 
784
781
  /**
785
782
  * Injector API.
@@ -808,7 +805,7 @@ interface Injector {
808
805
  *
809
806
  * Throws an error if the token is not registered in the container.
810
807
  */
811
- injectAll<Value>(token: Token<Value>): NonNullable<Value>[];
808
+ injectAll<Value>(token: Token<Value>): Value[];
812
809
  /**
813
810
  * Injects the instance associated with the given class,
814
811
  * or `undefined` if the class is not registered in the container.
@@ -820,15 +817,15 @@ interface Injector {
820
817
  */
821
818
  optional<Value>(token: Token<Value>, name?: string): Value | undefined;
822
819
  /**
823
- * Injects all instances provided by the registrations associated with the given class,
820
+ * Injects all instances provided by the registrations associated with the given class
824
821
  * or an empty array if the class is not registered in the container.
825
822
  */
826
823
  optionalAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
827
824
  /**
828
- * Injects all values provided by the registrations associated with the given token,
825
+ * Injects all values provided by the registrations associated with the given token
829
826
  * or an empty array if the token is not registered in the container.
830
827
  */
831
- optionalAll<Value>(token: Token<Value>): NonNullable<Value>[];
828
+ optionalAll<Value>(token: Token<Value>): Value[];
832
829
  /**
833
830
  * Runs a function inside the injection context of this injector.
834
831
  *
@@ -912,7 +909,7 @@ interface Middleware {
912
909
  /**
913
910
  * Applies middleware functions to a container.
914
911
  *
915
- * Middlewares are applied in array order, but execute in reverse order.
912
+ * Middlewares are applied in array order but execute in reverse order.
916
913
  *
917
914
  * @example
918
915
  * ```ts
@@ -970,15 +967,15 @@ declare function optionalBy<Instance extends object>(thisArg: any, Class: Constr
970
967
  declare function optionalBy<Value>(thisArg: any, token: Token<Value>, name?: string): Value | undefined;
971
968
 
972
969
  /**
973
- * Injects all instances provided by the registrations associated with the given class,
970
+ * Injects all instances provided by the registrations associated with the given class
974
971
  * or an empty array if the class is not registered in the container.
975
972
  */
976
973
  declare function optionalAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
977
974
  /**
978
- * Injects all values provided by the registrations associated with the given token,
975
+ * Injects all values provided by the registrations associated with the given token
979
976
  * or an empty array if the token is not registered in the container.
980
977
  */
981
- declare function optionalAll<Value>(token: Token<Value>): NonNullable<Value>[];
978
+ declare function optionalAll<Value>(token: Token<Value>): Value[];
982
979
 
983
980
  export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping };
984
981
  export type { ClassProvider, Constructor, Container, ContainerOptions, ExistingProvider, FactoryProvider, Middleware, MiddlewareComposer, Provider, RegistrationOptions, Token, TokenRef, Tokens, TokensRef, Type, ValueProvider };
package/dist/cjs/index.js CHANGED
@@ -55,8 +55,7 @@ function getTokenPath(tokens) {
55
55
  function getTokenName(token) {
56
56
  return token.name || "<unnamed>";
57
57
  }
58
- function getFullTokenName(tokenInfo) {
59
- const [token, name] = tokenInfo;
58
+ function getFullTokenName([token, name]) {
60
59
  const tokenName = token.name || "<unnamed>";
61
60
  return name ? `${tokenName}["${name}"]` : tokenName;
62
61
  }
@@ -83,8 +82,7 @@ class KeyedStack {
83
82
  return this.myKeys.has(key);
84
83
  }
85
84
  peek() {
86
- const entry = this.myEntries.at(-1);
87
- return entry?.value;
85
+ return this.myEntries.at(-1)?.value;
88
86
  }
89
87
  push(key, value) {
90
88
  check(!this.has(key), "invariant violation");
@@ -132,7 +130,7 @@ class WeakRefMap {
132
130
  // @internal
133
131
  function createResolution() {
134
132
  return {
135
- tokenStack: [],
133
+ tokens: [],
136
134
  stack: new KeyedStack(),
137
135
  values: new WeakRefMap(),
138
136
  dependents: new WeakRefMap()
@@ -330,8 +328,9 @@ const Scope = {
330
328
  Container: "Container"
331
329
  };
332
330
 
333
- const typeSymbol = Symbol("di-wise-neo.typeToken");
334
331
  /**
332
+ * Type API.
333
+ */ /**
335
334
  * Creates a type token.
336
335
  *
337
336
  * @example
@@ -345,7 +344,6 @@ const typeSymbol = Symbol("di-wise-neo.typeToken");
345
344
  name: `Type<${typeName}>`,
346
345
  inter: createType,
347
346
  union: createType,
348
- __type: typeSymbol,
349
347
  toString () {
350
348
  return type.name;
351
349
  }
@@ -353,10 +351,6 @@ const typeSymbol = Symbol("di-wise-neo.typeToken");
353
351
  return type;
354
352
  }
355
353
  // @internal
356
- function isType(token) {
357
- return token.__type === typeSymbol;
358
- }
359
- // @internal
360
354
  function isConstructor(token) {
361
355
  return typeof token === "function";
362
356
  }
@@ -418,22 +412,22 @@ class TokenRegistry {
418
412
  }
419
413
  delete(token, name) {
420
414
  const registrations = this.myMap.get(token);
421
- if (registrations) {
422
- if (name !== undefined) {
423
- const removedRegistrations = [];
424
- const newRegistrations = [];
425
- for (const registration of registrations){
426
- const array = registration.name === name ? removedRegistrations : newRegistrations;
427
- array.push(registration);
428
- }
429
- if (removedRegistrations.length > 0) {
430
- this.myMap.set(token, newRegistrations);
431
- return removedRegistrations;
432
- }
415
+ if (!registrations) {
416
+ return [];
417
+ }
418
+ if (name !== undefined) {
419
+ const removed = [];
420
+ const updated = [];
421
+ for (const registration of registrations){
422
+ (registration.name === name ? removed : updated).push(registration);
423
+ }
424
+ if (removed.length > 0) {
425
+ this.myMap.set(token, updated);
426
+ return removed;
433
427
  }
434
- this.myMap.delete(token);
435
428
  }
436
- return registrations ?? [];
429
+ this.myMap.delete(token);
430
+ return registrations;
437
431
  }
438
432
  deleteAll() {
439
433
  const tokens = Array.from(this.myMap.keys());
@@ -595,10 +589,10 @@ function isDisposable(value) {
595
589
  this.myTokenRegistry.set(token, {
596
590
  name: name,
597
591
  provider: {
598
- useExisting: {
599
- token: Class,
600
- name: name
601
- }
592
+ useExisting: [
593
+ Class,
594
+ name
595
+ ]
602
596
  }
603
597
  });
604
598
  }
@@ -614,7 +608,7 @@ function isDisposable(value) {
614
608
  const metadata = getMetadata(provider.useClass);
615
609
  const registration = {
616
610
  // An explicit provider name overrides what is specified via @Named
617
- name: metadata.name ?? provider.name,
611
+ name: metadata.name ?? name,
618
612
  provider: metadata.provider,
619
613
  options: {
620
614
  // Explicit registration options override what is specified via class decorators (e.g., @Scoped)
@@ -668,7 +662,7 @@ function isDisposable(value) {
668
662
  if (!registration && isConstructor(token)) {
669
663
  registration = this.autoRegisterClass(token, localName);
670
664
  }
671
- return this.resolveRegistration(token, registration, localOptional, localName);
665
+ return this.resolveRegistration(token, registration, localOptional, localName)?.value;
672
666
  }
673
667
  resolveAll(token, optional) {
674
668
  this.checkDisposed();
@@ -686,8 +680,7 @@ function isDisposable(value) {
686
680
  token
687
681
  ]);
688
682
  }
689
- return registrations //
690
- .map((registration)=>this.resolveRegistration(token, registration, optional)).filter((value)=>value != null);
683
+ return registrations.map((registration)=>this.resolveRegistration(token, registration, optional)).filter((result)=>result !== undefined).map((result)=>result.value);
691
684
  }
692
685
  dispose() {
693
686
  if (this.myDisposed) {
@@ -747,7 +740,9 @@ function isDisposable(value) {
747
740
  ]);
748
741
  }
749
742
  try {
750
- return this.resolveProviderValue(token, registration);
743
+ return {
744
+ value: this.resolveProviderValue(token, registration)
745
+ };
751
746
  } catch (e) {
752
747
  throwResolutionError([
753
748
  token,
@@ -757,12 +752,8 @@ function isDisposable(value) {
757
752
  }
758
753
  getTargetToken(provider) {
759
754
  const token = provider.useExisting;
760
- return isType(token) || isConstructor(token) //
761
- ? [
755
+ return Array.isArray(token) ? token : [
762
756
  token
763
- ] : [
764
- token.token,
765
- token.name
766
757
  ];
767
758
  }
768
759
  autoRegisterClass(Class, name) {
@@ -810,7 +801,7 @@ function isDisposable(value) {
810
801
  if (resolution.stack.has(provider)) {
811
802
  const dependentRef = resolution.dependents.get(provider);
812
803
  check(dependentRef, ()=>{
813
- const path = getTokenPath(resolution.tokenStack.concat(token).map((t)=>[
804
+ const path = getTokenPath(resolution.tokens.concat(token).map((t)=>[
814
805
  t
815
806
  ]));
816
807
  return `circular dependency detected while resolving ${path}`;
@@ -820,7 +811,7 @@ function isDisposable(value) {
820
811
  const scope = registration.options?.scope ?? this.myOptions.defaultScope;
821
812
  const cleanups = [
822
813
  provideInjectionContext(context),
823
- resolution.tokenStack.push(token) && (()=>resolution.tokenStack.pop()),
814
+ resolution.tokens.push(token) && (()=>resolution.tokens.pop()),
824
815
  !isBuilder(provider) && resolution.stack.push(provider, {
825
816
  provider,
826
817
  scope
@@ -947,7 +938,7 @@ function isDisposable(value) {
947
938
  }
948
939
 
949
940
  /**
950
- * Class decorator that enables auto-registration of an unregistered class,
941
+ * Class decorator that enables auto-registration of an unregistered class
951
942
  * when the class is first resolved from the container.
952
943
  *
953
944
  * @example
@@ -1006,8 +997,7 @@ function isDisposable(value) {
1006
997
  function forwardRef(token) {
1007
998
  return {
1008
999
  getRefTokens: ()=>{
1009
- // Normalize the single token here, so that we don't have
1010
- // to do it at every getRefTokens call site
1000
+ // Normalize the single token here so that we don't have to do it at every getRefTokens call site
1011
1001
  const tokenOrTokens = token();
1012
1002
  const tokensArray = Array.isArray(tokenOrTokens) ? tokenOrTokens : [
1013
1003
  tokenOrTokens
@@ -1256,14 +1246,14 @@ function OptionalAll(token) {
1256
1246
  injectAll: (token)=>runInContext(()=>injectAll(token)),
1257
1247
  optional: (token, name)=>runInContext(()=>optional(token, name)),
1258
1248
  optionalAll: (token)=>runInContext(()=>optionalAll(token)),
1259
- runInContext
1249
+ runInContext: runInContext
1260
1250
  };
1261
1251
  }, "Injector");
1262
1252
 
1263
1253
  /**
1264
1254
  * Applies middleware functions to a container.
1265
1255
  *
1266
- * Middlewares are applied in array order, but execute in reverse order.
1256
+ * Middlewares are applied in array order but execute in reverse order.
1267
1257
  *
1268
1258
  * @example
1269
1259
  * ```ts