@lppedd/di-wise-neo 0.11.0 → 0.11.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.
@@ -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.
@@ -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
  *
@@ -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
@@ -669,8 +666,8 @@ declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
669
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
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)
@@ -757,12 +751,8 @@ function isDisposable(value) {
757
751
  }
758
752
  getTargetToken(provider) {
759
753
  const token = provider.useExisting;
760
- return isType(token) || isConstructor(token) //
761
- ? [
754
+ return Array.isArray(token) ? token : [
762
755
  token
763
- ] : [
764
- token.token,
765
- token.name
766
756
  ];
767
757
  }
768
758
  autoRegisterClass(Class, name) {
@@ -810,7 +800,7 @@ function isDisposable(value) {
810
800
  if (resolution.stack.has(provider)) {
811
801
  const dependentRef = resolution.dependents.get(provider);
812
802
  check(dependentRef, ()=>{
813
- const path = getTokenPath(resolution.tokenStack.concat(token).map((t)=>[
803
+ const path = getTokenPath(resolution.tokens.concat(token).map((t)=>[
814
804
  t
815
805
  ]));
816
806
  return `circular dependency detected while resolving ${path}`;
@@ -820,7 +810,7 @@ function isDisposable(value) {
820
810
  const scope = registration.options?.scope ?? this.myOptions.defaultScope;
821
811
  const cleanups = [
822
812
  provideInjectionContext(context),
823
- resolution.tokenStack.push(token) && (()=>resolution.tokenStack.pop()),
813
+ resolution.tokens.push(token) && (()=>resolution.tokens.pop()),
824
814
  !isBuilder(provider) && resolution.stack.push(provider, {
825
815
  provider,
826
816
  scope
@@ -1006,8 +996,7 @@ function isDisposable(value) {
1006
996
  function forwardRef(token) {
1007
997
  return {
1008
998
  getRefTokens: ()=>{
1009
- // Normalize the single token here, so that we don't have
1010
- // to do it at every getRefTokens call site
999
+ // Normalize the single token here so that we don't have to do it at every getRefTokens call site
1011
1000
  const tokenOrTokens = token();
1012
1001
  const tokensArray = Array.isArray(tokenOrTokens) ? tokenOrTokens : [
1013
1002
  tokenOrTokens