@tsed/di 8.38.0-beta.1 → 8.38.0-beta.3

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.
@@ -183,7 +183,7 @@ export class InjectorService {
183
183
  * @returns `true` if the instance is cached, `false` otherwise
184
184
  */
185
185
  has(token) {
186
- return this.#cache.get(token) !== undefined;
186
+ return this.#cache.has(token);
187
187
  }
188
188
  /**
189
189
  * Create an alias for a provider token.
@@ -222,9 +222,8 @@ export class InjectorService {
222
222
  * @returns The resolved provider instance
223
223
  */
224
224
  resolve(token, options = {}) {
225
- let instance = options.locals ? options.locals.get(token) : undefined;
226
- if (instance !== undefined) {
227
- return instance;
225
+ if (options.locals?.has(token)) {
226
+ return options.locals.get(token);
228
227
  }
229
228
  if (token === DI_USE_PARAM_OPTIONS) {
230
229
  return options.useOpts;
@@ -233,23 +232,19 @@ export class InjectorService {
233
232
  // keep this configuration for legacy code that use as custom Configuration service
234
233
  return this.settings;
235
234
  }
236
- instance = !options.rebuild ? this.#cache.get(token) : undefined;
237
- if (instance != undefined) {
238
- return instance;
235
+ if (!options.rebuild && this.#cache.has(token)) {
236
+ return this.#cache.get(token);
239
237
  }
240
238
  const provider = this.ensureProvider(token);
241
- if (provider && provider.scope === ProviderScope.SINGLETON && !options.rebuild && !this.#registeredSingletonHooks.has(provider.token)) {
242
- this.registerHooks(provider, options);
243
- }
244
239
  // maybe not necessary
245
240
  if (!provider || options.rebuild) {
246
- instance = this.invokeToken(token, options);
241
+ const instance = this.invokeToken(token, options);
247
242
  if (provider) {
248
243
  return this.setToCache(provider, instance);
249
244
  }
250
245
  return instance;
251
246
  }
252
- instance = this.invokeToken(token, options);
247
+ const instance = this.invokeToken(token, options);
253
248
  switch (provider.scope) {
254
249
  case ProviderScope.SINGLETON:
255
250
  return this.setToCache(provider, instance);
@@ -472,30 +467,31 @@ export class InjectorService {
472
467
  $emit("$beforeInvoke", token, [resolvedOpts]);
473
468
  $emit(`$beforeInvoke:${String(provider.type)}`, [resolvedOpts]);
474
469
  let instance;
475
- let currentDependency = false;
470
+ let currentDependency;
471
+ let currentDependencyIndex = -1;
472
+ let hasCurrentDependency = false;
476
473
  try {
477
- const invokeDependency = (parent) => (token, index) => {
478
- currentDependency = { token, index, deps };
479
- if (isArray(token)) {
480
- return this.getMany(token[0], options);
481
- }
482
- return isInheritedFrom(token, Provider, 1)
483
- ? provider
484
- : this.resolve(token, {
485
- parent,
486
- locals: options.locals,
487
- useOpts: provider?.getArgOpts(index) || options.useOpts
488
- });
489
- };
490
474
  // Invoke manually imported providers
491
- imports.forEach(invokeDependency());
475
+ for (let index = 0; index < imports.length; index++) {
476
+ currentDependency = imports[index];
477
+ currentDependencyIndex = index;
478
+ hasCurrentDependency = true;
479
+ this.invokeDependency(currentDependency, index, undefined, provider, options);
480
+ }
492
481
  // Inject dependencies
493
- const services = deps.map(invokeDependency(token));
494
- currentDependency = false;
482
+ const services = new Array(deps.length);
483
+ for (let index = 0; index < deps.length; index++) {
484
+ currentDependency = deps[index];
485
+ currentDependencyIndex = index;
486
+ hasCurrentDependency = true;
487
+ services[index] = this.invokeDependency(currentDependency, index, token, provider, options);
488
+ }
489
+ hasCurrentDependency = false;
495
490
  instance = construct(services);
496
491
  }
497
492
  catch (error) {
498
- InjectionError.throwInjectorError(token, currentDependency, error);
493
+ const dependency = hasCurrentDependency ? { token: currentDependency, index: currentDependencyIndex, deps } : false;
494
+ InjectionError.throwInjectorError(token, dependency, error);
499
495
  }
500
496
  if (instance === undefined) {
501
497
  throw new InjectionError(token, `Unable to create new instance from undefined value. Check your provider declaration for ${nameOf(token)}`);
@@ -519,6 +515,18 @@ export class InjectorService {
519
515
  }
520
516
  });
521
517
  }
518
+ invokeDependency(token, index, parent, provider, options) {
519
+ if (isArray(token)) {
520
+ return this.getMany(token[0], options);
521
+ }
522
+ return isInheritedFrom(token, Provider, 1)
523
+ ? provider
524
+ : this.resolve(token, {
525
+ parent,
526
+ locals: options.locals,
527
+ useOpts: provider.getArgOpts(index) || options.useOpts
528
+ });
529
+ }
522
530
  resolveImportsProviders() {
523
531
  const imports = this.settings.imports;
524
532
  if (!imports) {
@@ -572,13 +580,7 @@ export class InjectorService {
572
580
  if (!token || token === Object) {
573
581
  throw new Error("Given token is undefined. Could mean a circular dependency problem. Try to use @Inject(() => Token) to solve it.");
574
582
  }
575
- let provider;
576
- if (!this.providers.get(token)) {
577
- provider = new Provider(token);
578
- }
579
- else {
580
- provider = this.providers.get(token);
581
- }
583
+ const provider = this.#providers.get(token) || new Provider(token);
582
584
  deps = deps || provider.deps;
583
585
  imports = imports || provider.imports;
584
586
  if (provider.useValue !== undefined) {
@@ -261,6 +261,7 @@ export declare class InjectorService {
261
261
  */
262
262
  protected invokeToken<T>(target: TokenProvider, options?: Partial<InvokeOptions>): T | Promise<T>;
263
263
  private mergeProviders;
264
+ private invokeDependency;
264
265
  private resolveImportsProviders;
265
266
  /**
266
267
  * Create options to invoke a provider or class.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/di",
3
- "version": "8.38.0-beta.1",
3
+ "version": "8.38.0-beta.3",
4
4
  "description": "DI module for Ts.ED Framework",
5
5
  "source": "./src/index.ts",
6
6
  "type": "module",
@@ -29,11 +29,11 @@
29
29
  "uuid": "^14.0.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@tsed/barrels": "8.38.0-beta.1",
33
- "@tsed/core": "8.38.0-beta.1",
34
- "@tsed/hooks": "8.38.0-beta.1",
32
+ "@tsed/barrels": "8.38.0-beta.3",
33
+ "@tsed/core": "8.38.0-beta.3",
34
+ "@tsed/hooks": "8.38.0-beta.3",
35
35
  "@tsed/logger": "^8.0.0",
36
- "@tsed/typescript": "8.38.0-beta.1",
36
+ "@tsed/typescript": "8.38.0-beta.3",
37
37
  "cross-env": "7.0.3",
38
38
  "typescript": "6.0.2",
39
39
  "vite": "7.3.2",