ts-ioc-container 35.6.2 → 35.7.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.
@@ -74,13 +74,13 @@ class Container {
74
74
  }
75
75
  dispose() {
76
76
  this.validateContainer();
77
+ for (const scope of this.scopes) {
78
+ scope.dispose();
79
+ }
77
80
  this.onDispose.emit(this);
78
81
  this.isDisposed = true;
79
82
  this.parent.removeScope(this);
80
83
  this.parent = new EmptyContainer_1.EmptyContainer();
81
- for (const scope of this.scopes) {
82
- scope.dispose();
83
- }
84
84
  this.providers.clear();
85
85
  this.instances.clear();
86
86
  this.registrations.splice(0, this.registrations.length);
@@ -11,7 +11,7 @@ const key = (...keys) => (r) => {
11
11
  exports.key = key;
12
12
  const redirectFrom = (...keys) => (r) => r.redirectFrom(...keys);
13
13
  exports.redirectFrom = redirectFrom;
14
- const scope = (predicate) => (r) => r.when(predicate);
14
+ const scope = (...predicates) => (r) => r.when(...predicates);
15
15
  exports.scope = scope;
16
16
  const METADATA_KEY = 'registration';
17
17
  const getTransformers = (Target) => (0, metadata_1.getMetadata)(Target, METADATA_KEY) ?? [];
@@ -23,10 +23,10 @@ class Registration {
23
23
  static toKey(key) {
24
24
  return new Registration(() => Provider_1.Provider.fromKey(key));
25
25
  }
26
- constructor(createProvider, key, matchScope = () => true) {
26
+ constructor(createProvider, key, scopePredicates = []) {
27
27
  this.createProvider = createProvider;
28
28
  this.key = key;
29
- this.matchScope = matchScope;
29
+ this.scopePredicates = scopePredicates;
30
30
  this.redirectKeys = new Set();
31
31
  this.mappers = [];
32
32
  }
@@ -44,10 +44,17 @@ class Registration {
44
44
  this.mappers.push(...mappers);
45
45
  return this;
46
46
  }
47
- when(isValidWhen) {
48
- this.matchScope = isValidWhen;
47
+ when(...predicates) {
48
+ this.scopePredicates.push(...predicates);
49
49
  return this;
50
50
  }
51
+ matchScope(container) {
52
+ if (this.scopePredicates.length === 0) {
53
+ return true;
54
+ }
55
+ const [first, ...rest] = this.scopePredicates;
56
+ return rest.reduce((prev, curr) => curr(container, prev), first(container));
57
+ }
51
58
  applyTo(container) {
52
59
  if (!this.matchScope(container)) {
53
60
  return;
@@ -71,13 +71,13 @@ export class Container {
71
71
  }
72
72
  dispose() {
73
73
  this.validateContainer();
74
+ for (const scope of this.scopes) {
75
+ scope.dispose();
76
+ }
74
77
  this.onDispose.emit(this);
75
78
  this.isDisposed = true;
76
79
  this.parent.removeScope(this);
77
80
  this.parent = new EmptyContainer();
78
- for (const scope of this.scopes) {
79
- scope.dispose();
80
- }
81
81
  this.providers.clear();
82
82
  this.instances.clear();
83
83
  this.registrations.splice(0, this.registrations.length);
@@ -6,7 +6,7 @@ export const key = (...keys) => (r) => {
6
6
  return r.fromKey(originalKey).redirectFrom(...redirectKeys);
7
7
  };
8
8
  export const redirectFrom = (...keys) => (r) => r.redirectFrom(...keys);
9
- export const scope = (predicate) => (r) => r.when(predicate);
9
+ export const scope = (...predicates) => (r) => r.when(...predicates);
10
10
  const METADATA_KEY = 'registration';
11
11
  export const getTransformers = (Target) => getMetadata(Target, METADATA_KEY) ?? [];
12
12
  export const register = (...mappers) => setMetadata(METADATA_KEY, mappers.map((m, index) => {
@@ -20,10 +20,10 @@ export class Registration {
20
20
  static toKey(key) {
21
21
  return new Registration(() => Provider.fromKey(key));
22
22
  }
23
- constructor(createProvider, key, matchScope = () => true) {
23
+ constructor(createProvider, key, scopePredicates = []) {
24
24
  this.createProvider = createProvider;
25
25
  this.key = key;
26
- this.matchScope = matchScope;
26
+ this.scopePredicates = scopePredicates;
27
27
  this.redirectKeys = new Set();
28
28
  this.mappers = [];
29
29
  }
@@ -41,10 +41,17 @@ export class Registration {
41
41
  this.mappers.push(...mappers);
42
42
  return this;
43
43
  }
44
- when(isValidWhen) {
45
- this.matchScope = isValidWhen;
44
+ when(...predicates) {
45
+ this.scopePredicates.push(...predicates);
46
46
  return this;
47
47
  }
48
+ matchScope(container) {
49
+ if (this.scopePredicates.length === 0) {
50
+ return true;
51
+ }
52
+ const [first, ...rest] = this.scopePredicates;
53
+ return rest.reduce((prev, curr) => curr(container, prev), first(container));
54
+ }
48
55
  applyTo(container) {
49
56
  if (!this.matchScope(container)) {
50
57
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "35.6.2",
3
+ "version": "35.7.0",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -59,5 +59,5 @@
59
59
  "ts-node": "^10.9.1",
60
60
  "typescript": "5.4.3"
61
61
  },
62
- "gitHead": "ad0b8fd07c4deeb317db9876ab77b0672b62a802"
62
+ "gitHead": "b86fcf49ce85d9ce8a99b323b1745f14c7f0c9da"
63
63
  }
@@ -2,9 +2,9 @@ import { DependencyKey, IContainer, IContainerModule } from '../container/IConta
2
2
  import { constructor, MapFn } from '../utils';
3
3
  import { IProvider } from '../provider/IProvider';
4
4
  import { DepKey } from '../by';
5
- export type ScopePredicate = (s: IContainer) => boolean;
5
+ export type ScopePredicate = (s: IContainer, prev?: boolean) => boolean;
6
6
  export interface IRegistration<T = any> extends IContainerModule {
7
- when(isValidWhen: ScopePredicate): this;
7
+ when(...predicates: ScopePredicate[]): this;
8
8
  fromKey(key: DependencyKey): this;
9
9
  pipe(...mappers: MapFn<IProvider<T>>[]): this;
10
10
  redirectFrom(...keys: DependencyKey[]): this;
@@ -12,6 +12,6 @@ export interface IRegistration<T = any> extends IContainerModule {
12
12
  export type ReturnTypeOfRegistration<T> = T extends IRegistration<infer R> ? R : never;
13
13
  export declare const key: (...keys: DependencyKey[]) => MapFn<IRegistration>;
14
14
  export declare const redirectFrom: (...keys: DependencyKey[]) => MapFn<IRegistration>;
15
- export declare const scope: (predicate: ScopePredicate) => MapFn<IRegistration>;
15
+ export declare const scope: (...predicates: ScopePredicate[]) => MapFn<IRegistration>;
16
16
  export declare const getTransformers: (Target: constructor<unknown>) => MapFn<IRegistration<any>>[];
17
17
  export declare const register: (...mappers: (MapFn<IRegistration> | DepKey<any> | DependencyKey)[]) => ClassDecorator;
@@ -5,17 +5,18 @@ import { IRegistration, ScopePredicate } from './IRegistration';
5
5
  export declare class Registration<T = any> implements IRegistration<T> {
6
6
  private createProvider;
7
7
  private key?;
8
- private matchScope;
8
+ private scopePredicates;
9
9
  private redirectKeys;
10
10
  static toClass<T>(Target: constructor<T>): IRegistration<any>;
11
11
  static toValue<T>(value: T): IRegistration<any>;
12
12
  static toFn<T>(fn: ResolveDependency<T>): Registration<T>;
13
13
  static toKey<T>(key: DependencyKey): Registration<T>;
14
14
  private mappers;
15
- constructor(createProvider: (key: DependencyKey) => IProvider<T>, key?: DependencyKey | undefined, matchScope?: ScopePredicate);
15
+ constructor(createProvider: (key: DependencyKey) => IProvider<T>, key?: DependencyKey | undefined, scopePredicates?: ScopePredicate[]);
16
16
  fromKey(key: DependencyKey): this;
17
17
  redirectFrom(...keys: DependencyKey[]): this;
18
18
  pipe(...mappers: MapFn<IProvider<T>>[]): this;
19
- when(isValidWhen: ScopePredicate): this;
19
+ when(...predicates: ScopePredicate[]): this;
20
+ private matchScope;
20
21
  applyTo(container: IContainer): void;
21
22
  }