ts-ioc-container 33.0.0 → 33.0.2

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.
@@ -5,9 +5,9 @@ const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError
5
5
  const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
6
6
  class AutoMockedContainer {
7
7
  constructor() {
8
- this.tags = new Set();
9
8
  this.id = '0';
10
9
  this.level = 0;
10
+ this.tags = new Set();
11
11
  this.isDisposed = false;
12
12
  }
13
13
  findChild(matchFn) {
@@ -52,10 +52,7 @@ class AutoMockedContainer {
52
52
  throw new DependencyNotFoundError_1.DependencyNotFoundError(`Cannot find by alias`);
53
53
  }
54
54
  hasInstance(value) {
55
- throw new Error('Method not implemented.');
56
- }
57
- hasOwnInstance(value) {
58
- throw new Error('Method not implemented.');
55
+ throw new MethodNotImplementedError_1.MethodNotImplementedError();
59
56
  }
60
57
  }
61
58
  exports.AutoMockedContainer = AutoMockedContainer;
@@ -62,9 +62,9 @@ class Container {
62
62
  }
63
63
  getInstances(direction = 'child') {
64
64
  if (direction === 'parent') {
65
- return this.parent.getInstances('parent').concat([...this.instances.values()]);
65
+ return [...this.parent.getInstances('parent'), ...this.instances];
66
66
  }
67
- const instances = [...this.instances.values()];
67
+ const instances = Array.from(this.instances);
68
68
  for (const scope of this.scopes) {
69
69
  instances.push(...scope.getInstances('child'));
70
70
  }
@@ -114,6 +114,12 @@ class Container {
114
114
  findParent(matchFn) {
115
115
  return matchFn(this) ? this : this.parent.findParent(matchFn);
116
116
  }
117
+ hasInstance(value, direction) {
118
+ if (direction === 'parent') {
119
+ return this.instances.has(value) || this.parent.hasInstance(value, 'parent');
120
+ }
121
+ return this.instances.has(value) || Array.from(this.scopes).some((scope) => scope.hasInstance(value, 'child'));
122
+ }
117
123
  /**
118
124
  * @private
119
125
  */
@@ -126,14 +132,7 @@ class Container {
126
132
  * @private
127
133
  */
128
134
  getRegistrations() {
129
- const registrations = this.parent.getRegistrations();
130
- return this.registrations.length > 0 ? registrations.concat(this.registrations) : registrations;
131
- }
132
- hasInstance(value) {
133
- return this.instances.has(value) || this.parent.hasInstance(value);
134
- }
135
- hasOwnInstance(value) {
136
- return this.instances.has(value);
135
+ return [...this.parent.getRegistrations(), ...this.registrations];
137
136
  }
138
137
  /**
139
138
  * @private
@@ -5,16 +5,13 @@ const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError
5
5
  const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
6
6
  class EmptyContainer {
7
7
  constructor() {
8
- this.isDisposed = false;
9
8
  this.level = -1;
10
9
  this.id = 'empty';
11
10
  this.tags = new Set();
11
+ this.isDisposed = false;
12
12
  }
13
13
  hasInstance(value) {
14
- throw new Error('Method not implemented.');
15
- }
16
- hasOwnInstance(value) {
17
- throw new Error('Method not implemented.');
14
+ throw new MethodNotImplementedError_1.MethodNotImplementedError();
18
15
  }
19
16
  reduceToRoot(fn, initial) {
20
17
  return initial;
@@ -2,9 +2,9 @@ import { MethodNotImplementedError } from '../errors/MethodNotImplementedError';
2
2
  import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
3
3
  export class AutoMockedContainer {
4
4
  constructor() {
5
- this.tags = new Set();
6
5
  this.id = '0';
7
6
  this.level = 0;
7
+ this.tags = new Set();
8
8
  this.isDisposed = false;
9
9
  }
10
10
  findChild(matchFn) {
@@ -49,9 +49,6 @@ export class AutoMockedContainer {
49
49
  throw new DependencyNotFoundError(`Cannot find by alias`);
50
50
  }
51
51
  hasInstance(value) {
52
- throw new Error('Method not implemented.');
53
- }
54
- hasOwnInstance(value) {
55
- throw new Error('Method not implemented.');
52
+ throw new MethodNotImplementedError();
56
53
  }
57
54
  }
@@ -59,9 +59,9 @@ export class Container {
59
59
  }
60
60
  getInstances(direction = 'child') {
61
61
  if (direction === 'parent') {
62
- return this.parent.getInstances('parent').concat([...this.instances.values()]);
62
+ return [...this.parent.getInstances('parent'), ...this.instances];
63
63
  }
64
- const instances = [...this.instances.values()];
64
+ const instances = Array.from(this.instances);
65
65
  for (const scope of this.scopes) {
66
66
  instances.push(...scope.getInstances('child'));
67
67
  }
@@ -111,6 +111,12 @@ export class Container {
111
111
  findParent(matchFn) {
112
112
  return matchFn(this) ? this : this.parent.findParent(matchFn);
113
113
  }
114
+ hasInstance(value, direction) {
115
+ if (direction === 'parent') {
116
+ return this.instances.has(value) || this.parent.hasInstance(value, 'parent');
117
+ }
118
+ return this.instances.has(value) || Array.from(this.scopes).some((scope) => scope.hasInstance(value, 'child'));
119
+ }
114
120
  /**
115
121
  * @private
116
122
  */
@@ -123,14 +129,7 @@ export class Container {
123
129
  * @private
124
130
  */
125
131
  getRegistrations() {
126
- const registrations = this.parent.getRegistrations();
127
- return this.registrations.length > 0 ? registrations.concat(this.registrations) : registrations;
128
- }
129
- hasInstance(value) {
130
- return this.instances.has(value) || this.parent.hasInstance(value);
131
- }
132
- hasOwnInstance(value) {
133
- return this.instances.has(value);
132
+ return [...this.parent.getRegistrations(), ...this.registrations];
134
133
  }
135
134
  /**
136
135
  * @private
@@ -2,16 +2,13 @@ import { MethodNotImplementedError } from '../errors/MethodNotImplementedError';
2
2
  import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
3
3
  export class EmptyContainer {
4
4
  constructor() {
5
- this.isDisposed = false;
6
5
  this.level = -1;
7
6
  this.id = 'empty';
8
7
  this.tags = new Set();
8
+ this.isDisposed = false;
9
9
  }
10
10
  hasInstance(value) {
11
- throw new Error('Method not implemented.');
12
- }
13
- hasOwnInstance(value) {
14
- throw new Error('Method not implemented.');
11
+ throw new MethodNotImplementedError();
15
12
  }
16
13
  reduceToRoot(fn, initial) {
17
14
  return initial;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "33.0.0",
3
+ "version": "33.0.2",
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": "9fa774ef445611ecf1b7c08c450c3d251e2e2171"
62
+ "gitHead": "2fc46f490e00dfdf60d77861dee309bdcf039d39"
63
63
  }
@@ -1,9 +1,9 @@
1
- import { AliasPredicate, DependencyKey, IContainer, InjectionToken, ReduceFn, ResolveOptions } from './IContainer';
1
+ import { AliasPredicate, DependencyKey, IContainer, InjectionToken, ReduceScope, ResolveOptions } from './IContainer';
2
2
  import { IRegistration } from '../registration/IRegistration';
3
3
  export declare abstract class AutoMockedContainer implements IContainer {
4
- tags: Set<string>;
5
4
  id: string;
6
5
  level: number;
6
+ tags: Set<string>;
7
7
  isDisposed: boolean;
8
8
  findChild(matchFn: (s: IContainer) => boolean): IContainer | undefined;
9
9
  findParent(matchFn: (s: IContainer) => boolean): IContainer | undefined;
@@ -13,7 +13,7 @@ export declare abstract class AutoMockedContainer implements IContainer {
13
13
  dispose(): void;
14
14
  register(): this;
15
15
  getInstances(): object[];
16
- reduceToRoot<TResult>(fn: ReduceFn<TResult>, initial: TResult): TResult;
16
+ reduceToRoot<TResult>(fn: ReduceScope<TResult>, initial: TResult): TResult;
17
17
  removeScope(): void;
18
18
  use(): this;
19
19
  getRegistrations(): never[];
@@ -22,5 +22,4 @@ export declare abstract class AutoMockedContainer implements IContainer {
22
22
  resolveManyByAlias(predicate: AliasPredicate, options?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
23
23
  resolveOneByAlias<T>(predicate: AliasPredicate, options?: ResolveOptions): [DependencyKey, T];
24
24
  hasInstance(value: object): boolean;
25
- hasOwnInstance(value: object): boolean;
26
25
  }
@@ -1,4 +1,4 @@
1
- import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken, ReduceFn, ResolveOptions, Tag } from './IContainer';
1
+ import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken, ReduceScope, ResolveOptions, Tag } from './IContainer';
2
2
  import { IInjector } from '../injector/IInjector';
3
3
  import { IProvider } from '../provider/IProvider';
4
4
  import { IRegistration } from '../registration/IRegistration';
@@ -6,14 +6,14 @@ import { Counter } from './Counter';
6
6
  export declare class Container implements IContainer {
7
7
  private readonly injector;
8
8
  isDisposed: boolean;
9
- private readonly providers;
9
+ readonly id: string;
10
10
  readonly tags: Set<Tag>;
11
+ readonly level: number;
11
12
  private parent;
12
- private scopes;
13
- private instances;
13
+ private readonly providers;
14
+ private readonly scopes;
15
+ private readonly instances;
14
16
  private readonly registrations;
15
- readonly id: string;
16
- readonly level: number;
17
17
  private readonly counter;
18
18
  constructor(injector: IInjector, { parent, tags, counter, }?: {
19
19
  parent?: IContainer;
@@ -31,9 +31,10 @@ export declare class Container implements IContainer {
31
31
  hasDependency(key: DependencyKey): boolean;
32
32
  resolveManyByAlias(predicate: AliasPredicate, { args, child, lazy }?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
33
33
  resolveOneByAlias<T>(predicate: AliasPredicate, { args, child, lazy }?: ResolveOptions): [DependencyKey, T];
34
- reduceToRoot<TResult>(fn: ReduceFn<TResult>, initial: TResult): TResult;
34
+ reduceToRoot<TResult>(fn: ReduceScope<TResult>, initial: TResult): TResult;
35
35
  findChild(matchFn: (s: IContainer) => boolean): IContainer | undefined;
36
36
  findParent(matchFn: (s: IContainer) => boolean): IContainer | undefined;
37
+ hasInstance(value: object, direction: 'parent' | 'child'): boolean;
37
38
  /**
38
39
  * @private
39
40
  */
@@ -42,8 +43,6 @@ export declare class Container implements IContainer {
42
43
  * @private
43
44
  */
44
45
  getRegistrations(): IRegistration[];
45
- hasInstance(value: object): boolean;
46
- hasOwnInstance(value: object): boolean;
47
46
  /**
48
47
  * @private
49
48
  */
@@ -1,14 +1,13 @@
1
- import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken, ReduceFn, ResolveOptions } from './IContainer';
1
+ import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken, ReduceScope, ResolveOptions } from './IContainer';
2
2
  import { IProvider } from '../provider/IProvider';
3
3
  import { IRegistration } from '../registration/IRegistration';
4
4
  export declare class EmptyContainer implements IContainer {
5
- hasInstance(value: object): boolean;
6
- hasOwnInstance(value: object): boolean;
7
- isDisposed: boolean;
8
5
  level: number;
9
6
  id: string;
10
7
  tags: Set<string>;
11
- reduceToRoot<TResult>(fn: ReduceFn<TResult>, initial: TResult): TResult;
8
+ isDisposed: boolean;
9
+ hasInstance(value: object): boolean;
10
+ reduceToRoot<TResult>(fn: ReduceScope<TResult>, initial: TResult): TResult;
12
11
  findChild(matchFn: (s: IContainer) => boolean): IContainer | undefined;
13
12
  findParent(matchFn: (s: IContainer) => boolean): IContainer | undefined;
14
13
  hasDependency(key: string): boolean;
@@ -24,10 +24,10 @@ export interface Tagged {
24
24
  }
25
25
  export type Alias = string;
26
26
  export type AliasPredicate = (aliases: Set<Alias>) => boolean;
27
- export type ReduceFn<TResult> = (acc: TResult, container: IContainer) => TResult;
27
+ export type ReduceScope<TResult> = (acc: TResult, container: IContainer) => TResult;
28
28
  export interface IContainer extends Resolvable, Tagged {
29
+ readonly tags: Set<Tag>;
29
30
  readonly isDisposed: boolean;
30
- tags: Set<Tag>;
31
31
  createScope(...tags: Tag[]): IContainer;
32
32
  register(key: DependencyKey, value: IProvider): this;
33
33
  add(registration: IRegistration): this;
@@ -37,11 +37,10 @@ export interface IContainer extends Resolvable, Tagged {
37
37
  use(module: IContainerModule): this;
38
38
  getRegistrations(): IRegistration[];
39
39
  hasDependency(key: DependencyKey): boolean;
40
- reduceToRoot<TResult>(fn: ReduceFn<TResult>, initial: TResult): TResult;
40
+ reduceToRoot<TResult>(fn: ReduceScope<TResult>, initial: TResult): TResult;
41
41
  findChild(matchFn: (s: IContainer) => boolean): IContainer | undefined;
42
42
  findParent(matchFn: (s: IContainer) => boolean): IContainer | undefined;
43
43
  resolveManyByAlias(predicate: AliasPredicate, options?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
44
44
  resolveOneByAlias<T>(predicate: AliasPredicate, options?: ResolveOptions): [DependencyKey, T];
45
- hasInstance(value: object): boolean;
46
- hasOwnInstance(value: object): boolean;
45
+ hasInstance(value: object, direction: 'parent' | 'child'): boolean;
47
46
  }