@tstdl/base 0.93.23 → 0.93.25

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.
@@ -36,6 +36,7 @@ export type ResolveManyReturnType<T extends ResolveManyItem<any, any>[]> = {
36
36
  export type AddDisposeHandler = (handler: Disposable | AsyncDisposable | (() => any)) => void;
37
37
  export declare class Injector implements AsyncDisposable {
38
38
  #private;
39
+ readonly id: number;
39
40
  readonly name: string;
40
41
  get disposed(): boolean;
41
42
  constructor(name: string, parent?: Injector | null);
@@ -18,6 +18,7 @@ import { ResolveChain } from './resolve-chain.js';
18
18
  import { ResolveError } from './resolve.error.js';
19
19
  import { injectMetadataSymbol, injectableMetadataSymbol } from './symbols.js';
20
20
  import { getTokenName } from './token.js';
21
+ let _id = 1;
21
22
  export class Injector {
22
23
  static #globalRegistrations = new Map();
23
24
  #parent;
@@ -28,6 +29,7 @@ export class Injector {
28
29
  #registrations = new Map();
29
30
  #injectorScopedResolutions = new MultiKeyMap();
30
31
  #addDisposeHandler;
32
+ id = _id++;
31
33
  name;
32
34
  get disposed() {
33
35
  return this.#disposableStack.disposed;
@@ -318,22 +320,18 @@ export class Injector {
318
320
  if (isUndefined(token)) {
319
321
  throw new ResolveError('Token is undefined. This might be due to a circular dependency. Consider using an alias or forwardRef.', chain);
320
322
  }
321
- const ownValues = [];
322
- const parentValues = [];
323
- const registration = this.tryGetRegistration(token, options);
323
+ const registration = this.tryGetRegistration(token);
324
324
  if (isDefined(registration)) {
325
325
  const registrations = isArray(registration) ? registration : [registration];
326
- const resolved = registrations.map((reg) => this._resolveRegistration(reg, argument, options, context, chain));
327
- ownValues.push(...resolved);
326
+ return registrations.map((reg) => this._resolveRegistration(reg, argument, options, context, chain));
328
327
  }
329
328
  if ((options.onlySelf != true) && isNotNull(this.#parent)) {
330
- parentValues.push(...this.#parent._resolveAll(token, argument, options, context, chain));
329
+ return this.#parent._resolveAll(token, argument, { ...options, skipSelf: false }, context, chain);
331
330
  }
332
- const allValues = [...ownValues, ...parentValues];
333
- if ((allValues.length == 0) && (options.optional != true)) {
334
- throw new ResolveError(`No provider for ${getTokenName(token)} registered.`, chain);
331
+ if (options.optional == true) {
332
+ return [];
335
333
  }
336
- return allValues;
334
+ throw new ResolveError(`No provider for ${getTokenName(token)} registered.`, chain);
337
335
  }
338
336
  _resolveRegistration(registration, argument, options, context, chain) {
339
337
  checkOverflow(chain, context);
@@ -358,7 +356,7 @@ export class Injector {
358
356
  }
359
357
  // A new scope is only needed if we are instantiating a class, running a factory, or if the registration explicitly defines scoped providers.
360
358
  const needsNewScope = isClassProvider(provider) || isFactoryProvider(provider) || (providers.length > 0);
361
- const injector = needsNewScope ? this.fork(`[${getTokenName(token)}]ResolutionInjector`) : this;
359
+ const injector = needsNewScope ? this.fork(`[${getTokenName(token)}]Injector`) : this;
362
360
  for (const nestedProvider of providers) {
363
361
  injector.registerSingleton(nestedProvider.provide, nestedProvider, { multi: nestedProvider.multi });
364
362
  }
@@ -6,7 +6,9 @@ export type ResolveChainNodeBase<Type extends string> = {
6
6
  type: Type;
7
7
  forwardRef?: true;
8
8
  };
9
- export type ResolveChainNode = ResolveChainNodeBase<'token'> & {
9
+ export type ResolveChainNode = {
10
+ type: 'ellipsis';
11
+ } | ResolveChainNodeBase<'token'> & {
10
12
  token: InjectionToken;
11
13
  } | ResolveChainNodeBase<'inject'> & {
12
14
  token: InjectionToken;
@@ -41,9 +41,13 @@ export class ResolveChain {
41
41
  if (chain.length < this.length) {
42
42
  chainString += '\n [...]';
43
43
  }
44
- const longestInjectorName = Math.max(...chain.nodes.map((node) => node.injector.name.length));
44
+ const longestInjectorName = Math.max(...chain.nodes.map((node) => (node.type == 'ellipsis') ? 0 : `${node.injector.name}#${node.injector.id}`.length));
45
45
  for (const node of chain.nodes) {
46
- const paddedInjectorName = node.injector.name.padStart(longestInjectorName);
46
+ if (node.type == 'ellipsis') {
47
+ chainString += `\n ${'[...]'.padStart(longestInjectorName)}`;
48
+ continue;
49
+ }
50
+ const paddedInjectorName = `${node.injector.name}#${node.injector.id}`.padStart(longestInjectorName);
47
51
  const tokenName = getTokenName(node.token);
48
52
  const forwardRefPrefix = node.forwardRef ? 'ForwardRef::' : '';
49
53
  switch (node.type) {
@@ -74,16 +78,29 @@ export class ResolveChain {
74
78
  return chainString;
75
79
  }
76
80
  truncate(tokenCount) {
77
- const truncatedChain = [];
78
- let counter = 0;
79
- for (let i = this.nodes.length - 1; (i >= 0) && (counter < tokenCount); i--) {
81
+ const startOfChain = [];
82
+ const endOfChain = [];
83
+ let startCounter = 0;
84
+ let index = 0;
85
+ for (index = 0; (index < this.nodes.length) && (startCounter < tokenCount); index++) {
86
+ const node = this.nodes[index];
87
+ startOfChain.push(node);
88
+ if (node.type == 'token') {
89
+ startCounter++;
90
+ }
91
+ }
92
+ let endCounter = 0;
93
+ for (let i = this.nodes.length - 1; (i > index) && (endCounter < tokenCount); i--) {
80
94
  const node = this.nodes[i];
81
- truncatedChain.unshift(node);
95
+ endOfChain.unshift(node);
82
96
  if (node.type == 'token') {
83
- counter++;
97
+ endCounter++;
84
98
  }
85
99
  }
86
- return new ResolveChain(truncatedChain);
100
+ if (endOfChain.length > 0) {
101
+ startOfChain.push({ type: 'ellipsis' });
102
+ }
103
+ return new ResolveChain([...startOfChain, ...endOfChain]);
87
104
  }
88
105
  }
89
106
  function getPropertyKeyString(key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.93.23",
3
+ "version": "0.93.25",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"