ts-ioc-container 55.4.0 → 55.5.0

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.
package/README.md CHANGED
@@ -1542,7 +1542,7 @@ Constructor parameters that should pick up positional args from `ProviderOptions
1542
1542
  - `@inject(args(0))` — resolves the first element of the `args` array passed at resolution time
1543
1543
  - Works together with `token.args(...)` to pass typed dependencies through the args context
1544
1544
 
1545
- `args(index)` and `argsFn(fn)` are thin shortcuts for an `InjectFn`. Every `InjectFn` receives `(scope, options)`, where `options.args` is the runtime args array so `args(0)` is just `(scope, { args = [] }) => args[0]`, and `argsFn(fn)` is `(scope, { args = [] }) => fn(...args)` (the runtime args are spread into your callback as positional parameters).
1545
+ `argsFn(predicate)` is the general form: it iterates the runtime `args` array and returns the **first argument matching** `predicate(value, index)` think `args.find(predicate)`. `args(index)` is just a shortcut for matching by position: `args(0)` is `argsFn((value, index) => index === 0)`. Every `InjectFn` receives `(scope, options)`, where `options.args` is the runtime args array.
1546
1546
 
1547
1547
  ### Immutable token chaining
1548
1548
 
@@ -6,7 +6,6 @@ const ContainerDisposedError_1 = require("../errors/ContainerDisposedError");
6
6
  const MetadataInjector_1 = require("../injector/MetadataInjector");
7
7
  const AliasMap_1 = require("./AliasMap");
8
8
  const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
9
- const ContainerNotFoundError_1 = require("../errors/ContainerNotFoundError");
10
9
  const basic_1 = require("../utils/basic");
11
10
  const array_1 = require("../utils/array");
12
11
  class Container {
@@ -137,15 +136,10 @@ class Container {
137
136
  }
138
137
  getScopeByInstanceOrFail(instance) {
139
138
  this.validateContainer();
140
- const queue = [this];
141
- while (queue.length > 0) {
142
- const scope = queue.shift();
143
- if (scope.hasInstance(instance)) {
144
- return scope;
145
- }
146
- queue.push(...scope.getScopes());
139
+ if (this.hasInstance(instance)) {
140
+ return this;
147
141
  }
148
- throw new ContainerNotFoundError_1.ContainerNotFoundError('Cannot find scope for the given instance');
142
+ return this.parent.getScopeByInstanceOrFail(instance);
149
143
  }
150
144
  removeScope(child) {
151
145
  this.scopes = this.scopes.filter((s) => s !== child);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveArgs = exports.argsFn = exports.args = exports.inject = exports.MetadataInjector = void 0;
3
+ exports.resolveArgs = exports.args = exports.argsFn = exports.inject = exports.MetadataInjector = void 0;
4
4
  const IInjector_1 = require("./IInjector");
5
5
  const basic_1 = require("../utils/basic");
6
6
  const parameter_1 = require("../metadata/parameter");
@@ -17,12 +17,10 @@ const inject = (fn) => (target, propertyKey, parameterIndex) => {
17
17
  (0, parameter_1.addParamMeta)(hookMetaKey(propertyKey), () => (0, toToken_1.toToken)(fn))(basic_1.Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
18
18
  };
19
19
  exports.inject = inject;
20
- const args = (index) => (c, { args = [] }) => {
21
- return args[index];
22
- };
23
- exports.args = args;
24
- const argsFn = (fn) => (c, options) => fn(...(options.args ?? []));
20
+ const argsFn = (predicate) => (c, { args = [] }) => args.find((value, index) => predicate(value, index));
25
21
  exports.argsFn = argsFn;
22
+ const args = (index) => (0, exports.argsFn)((value, i) => i === index);
23
+ exports.args = args;
26
24
  const resolveArgs = (Target, methodName) => {
27
25
  const tokens = (0, parameter_1.getParamMeta)(hookMetaKey(methodName), Target);
28
26
  return (scope, { args = [], lazy }) => tokens.map((fn) => fn.resolve(scope, { args: args.map(toToken_1.argToToken).map((t) => t.resolve(scope)), lazy }));
@@ -3,7 +3,6 @@ import { ContainerDisposedError } from '../errors/ContainerDisposedError';
3
3
  import { MetadataInjector } from '../injector/MetadataInjector';
4
4
  import { AliasMap } from './AliasMap';
5
5
  import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
6
- import { ContainerNotFoundError } from '../errors/ContainerNotFoundError';
7
6
  import { Is } from '../utils/basic';
8
7
  import { Filter as F } from '../utils/array';
9
8
  export class Container {
@@ -134,15 +133,10 @@ export class Container {
134
133
  }
135
134
  getScopeByInstanceOrFail(instance) {
136
135
  this.validateContainer();
137
- const queue = [this];
138
- while (queue.length > 0) {
139
- const scope = queue.shift();
140
- if (scope.hasInstance(instance)) {
141
- return scope;
142
- }
143
- queue.push(...scope.getScopes());
136
+ if (this.hasInstance(instance)) {
137
+ return this;
144
138
  }
145
- throw new ContainerNotFoundError('Cannot find scope for the given instance');
139
+ return this.parent.getScopeByInstanceOrFail(instance);
146
140
  }
147
141
  removeScope(child) {
148
142
  this.scopes = this.scopes.filter((s) => s !== child);
@@ -12,10 +12,8 @@ const hookMetaKey = (methodName = 'constructor') => `inject:${methodName}`;
12
12
  export const inject = (fn) => (target, propertyKey, parameterIndex) => {
13
13
  addParamMeta(hookMetaKey(propertyKey), () => toToken(fn))(Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
14
14
  };
15
- export const args = (index) => (c, { args = [] }) => {
16
- return args[index];
17
- };
18
- export const argsFn = (fn) => (c, options) => fn(...(options.args ?? []));
15
+ export const argsFn = (predicate) => (c, { args = [] }) => args.find((value, index) => predicate(value, index));
16
+ export const args = (index) => argsFn((value, i) => i === index);
19
17
  export const resolveArgs = (Target, methodName) => {
20
18
  const tokens = getParamMeta(hookMetaKey(methodName), Target);
21
19
  return (scope, { args = [], lazy }) => tokens.map((fn) => fn.resolve(scope, { args: args.map(argToToken).map((t) => t.resolve(scope)), lazy }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "55.4.0",
3
+ "version": "55.5.0",
4
4
  "description": "Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.",
5
5
  "workspaces": [
6
6
  "docs"
@@ -8,6 +8,6 @@ export declare class MetadataInjector extends Injector implements IInjector {
8
8
  protected createInstance<T>(scope: IContainer, Target: constructor<T>, { args: deps }?: InjectOptions): T;
9
9
  }
10
10
  export declare const inject: <T>(fn: InjectionToken<T> | InjectFn<T> | symbol | string | constructor<T>) => ParameterDecorator;
11
+ export declare const argsFn: <T = unknown>(predicate: (value: unknown, index: number) => boolean) => InjectFn<T>;
11
12
  export declare const args: (index: number) => InjectFn;
12
- export declare const argsFn: <T>(fn: (...args: unknown[]) => T) => InjectFn<T>;
13
13
  export declare const resolveArgs: (Target: constructor<unknown>, methodName?: string) => (scope: IContainer, { args, lazy }: ProviderOptions) => unknown[];