@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/es/index.mjs CHANGED
@@ -53,8 +53,7 @@ function getTokenPath(tokens) {
53
53
  function getTokenName(token) {
54
54
  return token.name || "<unnamed>";
55
55
  }
56
- function getFullTokenName(tokenInfo) {
57
- const [token, name] = tokenInfo;
56
+ function getFullTokenName([token, name]) {
58
57
  const tokenName = token.name || "<unnamed>";
59
58
  return name ? `${tokenName}["${name}"]` : tokenName;
60
59
  }
@@ -81,8 +80,7 @@ class KeyedStack {
81
80
  return this.myKeys.has(key);
82
81
  }
83
82
  peek() {
84
- const entry = this.myEntries.at(-1);
85
- return entry?.value;
83
+ return this.myEntries.at(-1)?.value;
86
84
  }
87
85
  push(key, value) {
88
86
  check(!this.has(key), "invariant violation");
@@ -130,7 +128,7 @@ class WeakRefMap {
130
128
  // @internal
131
129
  function createResolution() {
132
130
  return {
133
- tokenStack: [],
131
+ tokens: [],
134
132
  stack: new KeyedStack(),
135
133
  values: new WeakRefMap(),
136
134
  dependents: new WeakRefMap()
@@ -328,8 +326,9 @@ const Scope = {
328
326
  Container: "Container"
329
327
  };
330
328
 
331
- const typeSymbol = Symbol("di-wise-neo.typeToken");
332
329
  /**
330
+ * Type API.
331
+ */ /**
333
332
  * Creates a type token.
334
333
  *
335
334
  * @example
@@ -343,7 +342,6 @@ const typeSymbol = Symbol("di-wise-neo.typeToken");
343
342
  name: `Type<${typeName}>`,
344
343
  inter: createType,
345
344
  union: createType,
346
- __type: typeSymbol,
347
345
  toString () {
348
346
  return type.name;
349
347
  }
@@ -351,10 +349,6 @@ const typeSymbol = Symbol("di-wise-neo.typeToken");
351
349
  return type;
352
350
  }
353
351
  // @internal
354
- function isType(token) {
355
- return token.__type === typeSymbol;
356
- }
357
- // @internal
358
352
  function isConstructor(token) {
359
353
  return typeof token === "function";
360
354
  }
@@ -416,22 +410,22 @@ class TokenRegistry {
416
410
  }
417
411
  delete(token, name) {
418
412
  const registrations = this.myMap.get(token);
419
- if (registrations) {
420
- if (name !== undefined) {
421
- const removedRegistrations = [];
422
- const newRegistrations = [];
423
- for (const registration of registrations){
424
- const array = registration.name === name ? removedRegistrations : newRegistrations;
425
- array.push(registration);
426
- }
427
- if (removedRegistrations.length > 0) {
428
- this.myMap.set(token, newRegistrations);
429
- return removedRegistrations;
430
- }
413
+ if (!registrations) {
414
+ return [];
415
+ }
416
+ if (name !== undefined) {
417
+ const removed = [];
418
+ const updated = [];
419
+ for (const registration of registrations){
420
+ (registration.name === name ? removed : updated).push(registration);
421
+ }
422
+ if (removed.length > 0) {
423
+ this.myMap.set(token, updated);
424
+ return removed;
431
425
  }
432
- this.myMap.delete(token);
433
426
  }
434
- return registrations ?? [];
427
+ this.myMap.delete(token);
428
+ return registrations;
435
429
  }
436
430
  deleteAll() {
437
431
  const tokens = Array.from(this.myMap.keys());
@@ -593,10 +587,10 @@ function isDisposable(value) {
593
587
  this.myTokenRegistry.set(token, {
594
588
  name: name,
595
589
  provider: {
596
- useExisting: {
597
- token: Class,
598
- name: name
599
- }
590
+ useExisting: [
591
+ Class,
592
+ name
593
+ ]
600
594
  }
601
595
  });
602
596
  }
@@ -612,7 +606,7 @@ function isDisposable(value) {
612
606
  const metadata = getMetadata(provider.useClass);
613
607
  const registration = {
614
608
  // An explicit provider name overrides what is specified via @Named
615
- name: metadata.name ?? provider.name,
609
+ name: metadata.name ?? name,
616
610
  provider: metadata.provider,
617
611
  options: {
618
612
  // Explicit registration options override what is specified via class decorators (e.g., @Scoped)
@@ -666,7 +660,7 @@ function isDisposable(value) {
666
660
  if (!registration && isConstructor(token)) {
667
661
  registration = this.autoRegisterClass(token, localName);
668
662
  }
669
- return this.resolveRegistration(token, registration, localOptional, localName);
663
+ return this.resolveRegistration(token, registration, localOptional, localName)?.value;
670
664
  }
671
665
  resolveAll(token, optional) {
672
666
  this.checkDisposed();
@@ -684,8 +678,7 @@ function isDisposable(value) {
684
678
  token
685
679
  ]);
686
680
  }
687
- return registrations //
688
- .map((registration)=>this.resolveRegistration(token, registration, optional)).filter((value)=>value != null);
681
+ return registrations.map((registration)=>this.resolveRegistration(token, registration, optional)).filter((result)=>result !== undefined).map((result)=>result.value);
689
682
  }
690
683
  dispose() {
691
684
  if (this.myDisposed) {
@@ -745,7 +738,9 @@ function isDisposable(value) {
745
738
  ]);
746
739
  }
747
740
  try {
748
- return this.resolveProviderValue(token, registration);
741
+ return {
742
+ value: this.resolveProviderValue(token, registration)
743
+ };
749
744
  } catch (e) {
750
745
  throwResolutionError([
751
746
  token,
@@ -755,12 +750,8 @@ function isDisposable(value) {
755
750
  }
756
751
  getTargetToken(provider) {
757
752
  const token = provider.useExisting;
758
- return isType(token) || isConstructor(token) //
759
- ? [
753
+ return Array.isArray(token) ? token : [
760
754
  token
761
- ] : [
762
- token.token,
763
- token.name
764
755
  ];
765
756
  }
766
757
  autoRegisterClass(Class, name) {
@@ -808,7 +799,7 @@ function isDisposable(value) {
808
799
  if (resolution.stack.has(provider)) {
809
800
  const dependentRef = resolution.dependents.get(provider);
810
801
  check(dependentRef, ()=>{
811
- const path = getTokenPath(resolution.tokenStack.concat(token).map((t)=>[
802
+ const path = getTokenPath(resolution.tokens.concat(token).map((t)=>[
812
803
  t
813
804
  ]));
814
805
  return `circular dependency detected while resolving ${path}`;
@@ -818,7 +809,7 @@ function isDisposable(value) {
818
809
  const scope = registration.options?.scope ?? this.myOptions.defaultScope;
819
810
  const cleanups = [
820
811
  provideInjectionContext(context),
821
- resolution.tokenStack.push(token) && (()=>resolution.tokenStack.pop()),
812
+ resolution.tokens.push(token) && (()=>resolution.tokens.pop()),
822
813
  !isBuilder(provider) && resolution.stack.push(provider, {
823
814
  provider,
824
815
  scope
@@ -945,7 +936,7 @@ function isDisposable(value) {
945
936
  }
946
937
 
947
938
  /**
948
- * Class decorator that enables auto-registration of an unregistered class,
939
+ * Class decorator that enables auto-registration of an unregistered class
949
940
  * when the class is first resolved from the container.
950
941
  *
951
942
  * @example
@@ -1004,8 +995,7 @@ function isDisposable(value) {
1004
995
  function forwardRef(token) {
1005
996
  return {
1006
997
  getRefTokens: ()=>{
1007
- // Normalize the single token here, so that we don't have
1008
- // to do it at every getRefTokens call site
998
+ // Normalize the single token here so that we don't have to do it at every getRefTokens call site
1009
999
  const tokenOrTokens = token();
1010
1000
  const tokensArray = Array.isArray(tokenOrTokens) ? tokenOrTokens : [
1011
1001
  tokenOrTokens
@@ -1254,14 +1244,14 @@ function OptionalAll(token) {
1254
1244
  injectAll: (token)=>runInContext(()=>injectAll(token)),
1255
1245
  optional: (token, name)=>runInContext(()=>optional(token, name)),
1256
1246
  optionalAll: (token)=>runInContext(()=>optionalAll(token)),
1257
- runInContext
1247
+ runInContext: runInContext
1258
1248
  };
1259
1249
  }, "Injector");
1260
1250
 
1261
1251
  /**
1262
1252
  * Applies middleware functions to a container.
1263
1253
  *
1264
- * Middlewares are applied in array order, but execute in reverse order.
1254
+ * Middlewares are applied in array order but execute in reverse order.
1265
1255
  *
1266
1256
  * @example
1267
1257
  * ```ts