@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/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)
@@ -755,12 +749,8 @@ function isDisposable(value) {
755
749
  }
756
750
  getTargetToken(provider) {
757
751
  const token = provider.useExisting;
758
- return isType(token) || isConstructor(token) //
759
- ? [
752
+ return Array.isArray(token) ? token : [
760
753
  token
761
- ] : [
762
- token.token,
763
- token.name
764
754
  ];
765
755
  }
766
756
  autoRegisterClass(Class, name) {
@@ -808,7 +798,7 @@ function isDisposable(value) {
808
798
  if (resolution.stack.has(provider)) {
809
799
  const dependentRef = resolution.dependents.get(provider);
810
800
  check(dependentRef, ()=>{
811
- const path = getTokenPath(resolution.tokenStack.concat(token).map((t)=>[
801
+ const path = getTokenPath(resolution.tokens.concat(token).map((t)=>[
812
802
  t
813
803
  ]));
814
804
  return `circular dependency detected while resolving ${path}`;
@@ -818,7 +808,7 @@ function isDisposable(value) {
818
808
  const scope = registration.options?.scope ?? this.myOptions.defaultScope;
819
809
  const cleanups = [
820
810
  provideInjectionContext(context),
821
- resolution.tokenStack.push(token) && (()=>resolution.tokenStack.pop()),
811
+ resolution.tokens.push(token) && (()=>resolution.tokens.pop()),
822
812
  !isBuilder(provider) && resolution.stack.push(provider, {
823
813
  provider,
824
814
  scope
@@ -1004,8 +994,7 @@ function isDisposable(value) {
1004
994
  function forwardRef(token) {
1005
995
  return {
1006
996
  getRefTokens: ()=>{
1007
- // Normalize the single token here, so that we don't have
1008
- // to do it at every getRefTokens call site
997
+ // Normalize the single token here so that we don't have to do it at every getRefTokens call site
1009
998
  const tokenOrTokens = token();
1010
999
  const tokensArray = Array.isArray(tokenOrTokens) ? tokenOrTokens : [
1011
1000
  tokenOrTokens