ts-ioc-container 37.2.0 → 37.2.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.
@@ -14,6 +14,9 @@ class AutoMockedContainer {
14
14
  throw new MethodNotImplementedError_1.MethodNotImplementedError();
15
15
  }
16
16
  dispose() { }
17
+ detachFromParent() {
18
+ throw new MethodNotImplementedError_1.MethodNotImplementedError();
19
+ }
17
20
  register() {
18
21
  return this;
19
22
  }
@@ -8,17 +8,17 @@ class Container {
8
8
  constructor(injector, options = {}) {
9
9
  this.injector = injector;
10
10
  this.isDisposed = false;
11
- this.scopes = new Set();
12
- this.instances = new Set();
11
+ this.scopes = [];
12
+ this.instances = [];
13
13
  this.providers = new Map();
14
- this.registrations = new Set();
14
+ this.registrations = [];
15
15
  this.parent = options.parent ?? new EmptyContainer_1.EmptyContainer();
16
16
  this.tags = new Set(options.tags ?? []);
17
17
  this.onConstruct = options.onConstruct ?? (() => { });
18
18
  this.onDispose = options.onDispose ?? (() => { });
19
19
  }
20
20
  add(registration) {
21
- this.registrations.add(registration);
21
+ this.registrations.push(registration);
22
22
  registration.applyTo(this);
23
23
  return this;
24
24
  }
@@ -31,8 +31,8 @@ class Container {
31
31
  this.validateContainer();
32
32
  if ((0, IContainer_1.isConstructor)(token)) {
33
33
  const instance = this.injector.resolve(this, token, { args });
34
- this.instances.add(instance);
35
- this.onConstruct(instance);
34
+ this.instances.push(instance);
35
+ this.onConstruct(instance, this);
36
36
  return instance;
37
37
  }
38
38
  const provider = this.providers.get(token);
@@ -44,7 +44,7 @@ class Container {
44
44
  this.validateContainer();
45
45
  const scope = new Container(this.injector, { parent: this, tags, onDispose: this.onDispose });
46
46
  scope.applyRegistrationsFrom(this);
47
- this.scopes.add(scope);
47
+ this.scopes.push(scope);
48
48
  return scope;
49
49
  }
50
50
  dispose({ cascade = true } = {}) {
@@ -55,16 +55,24 @@ class Container {
55
55
  this.parent = new EmptyContainer_1.EmptyContainer();
56
56
  // Reset the state
57
57
  this.providers.clear();
58
- this.instances.clear();
59
- this.registrations.clear();
58
+ this.instances.splice(0, this.instances.length);
59
+ this.registrations.splice(0, this.registrations.length);
60
60
  this.onDispose(this);
61
- if (cascade) {
62
- // Dispose all scopes
63
- for (const scope of this.scopes) {
64
- scope.dispose({ cascade });
61
+ // Dispose all scopes
62
+ while (this.scopes.length > 0) {
63
+ const scope = this.scopes[0];
64
+ if (cascade) {
65
+ scope.dispose({ cascade: true });
66
+ }
67
+ else {
68
+ scope.detachFromParent();
65
69
  }
66
70
  }
67
71
  }
72
+ detachFromParent() {
73
+ this.parent.removeScope(this);
74
+ this.parent = new EmptyContainer_1.EmptyContainer();
75
+ }
68
76
  use(module) {
69
77
  module.applyTo(this);
70
78
  return this;
@@ -118,7 +126,8 @@ class Container {
118
126
  * @private
119
127
  */
120
128
  removeScope(child) {
121
- this.scopes.delete(child);
129
+ const index = this.scopes.indexOf(child);
130
+ this.scopes.splice(index, 1);
122
131
  }
123
132
  validateContainer() {
124
133
  ContainerDisposedError_1.ContainerDisposedError.assert(!this.isDisposed, 'Container is already disposed');
@@ -7,6 +7,9 @@ class EmptyContainer {
7
7
  get isDisposed() {
8
8
  throw new MethodNotImplementedError_1.MethodNotImplementedError();
9
9
  }
10
+ detachFromParent() {
11
+ throw new MethodNotImplementedError_1.MethodNotImplementedError();
12
+ }
10
13
  hasProvider(key) {
11
14
  return false;
12
15
  }
@@ -19,10 +19,6 @@ class HookContext {
19
19
  // @ts-ignore
20
20
  this.instance[this.methodName] = fn(this.scope);
21
21
  }
22
- getProperty() {
23
- // @ts-ignore
24
- return this.instance[this.methodName];
25
- }
26
22
  }
27
23
  exports.HookContext = HookContext;
28
24
  const createHookContext = (Target, scope, methodName = 'constructor') => new HookContext(Target, scope, methodName);
@@ -11,6 +11,9 @@ export class AutoMockedContainer {
11
11
  throw new MethodNotImplementedError();
12
12
  }
13
13
  dispose() { }
14
+ detachFromParent() {
15
+ throw new MethodNotImplementedError();
16
+ }
14
17
  register() {
15
18
  return this;
16
19
  }
@@ -5,17 +5,17 @@ export class Container {
5
5
  constructor(injector, options = {}) {
6
6
  this.injector = injector;
7
7
  this.isDisposed = false;
8
- this.scopes = new Set();
9
- this.instances = new Set();
8
+ this.scopes = [];
9
+ this.instances = [];
10
10
  this.providers = new Map();
11
- this.registrations = new Set();
11
+ this.registrations = [];
12
12
  this.parent = options.parent ?? new EmptyContainer();
13
13
  this.tags = new Set(options.tags ?? []);
14
14
  this.onConstruct = options.onConstruct ?? (() => { });
15
15
  this.onDispose = options.onDispose ?? (() => { });
16
16
  }
17
17
  add(registration) {
18
- this.registrations.add(registration);
18
+ this.registrations.push(registration);
19
19
  registration.applyTo(this);
20
20
  return this;
21
21
  }
@@ -28,8 +28,8 @@ export class Container {
28
28
  this.validateContainer();
29
29
  if (isConstructor(token)) {
30
30
  const instance = this.injector.resolve(this, token, { args });
31
- this.instances.add(instance);
32
- this.onConstruct(instance);
31
+ this.instances.push(instance);
32
+ this.onConstruct(instance, this);
33
33
  return instance;
34
34
  }
35
35
  const provider = this.providers.get(token);
@@ -41,7 +41,7 @@ export class Container {
41
41
  this.validateContainer();
42
42
  const scope = new Container(this.injector, { parent: this, tags, onDispose: this.onDispose });
43
43
  scope.applyRegistrationsFrom(this);
44
- this.scopes.add(scope);
44
+ this.scopes.push(scope);
45
45
  return scope;
46
46
  }
47
47
  dispose({ cascade = true } = {}) {
@@ -52,16 +52,24 @@ export class Container {
52
52
  this.parent = new EmptyContainer();
53
53
  // Reset the state
54
54
  this.providers.clear();
55
- this.instances.clear();
56
- this.registrations.clear();
55
+ this.instances.splice(0, this.instances.length);
56
+ this.registrations.splice(0, this.registrations.length);
57
57
  this.onDispose(this);
58
- if (cascade) {
59
- // Dispose all scopes
60
- for (const scope of this.scopes) {
61
- scope.dispose({ cascade });
58
+ // Dispose all scopes
59
+ while (this.scopes.length > 0) {
60
+ const scope = this.scopes[0];
61
+ if (cascade) {
62
+ scope.dispose({ cascade: true });
63
+ }
64
+ else {
65
+ scope.detachFromParent();
62
66
  }
63
67
  }
64
68
  }
69
+ detachFromParent() {
70
+ this.parent.removeScope(this);
71
+ this.parent = new EmptyContainer();
72
+ }
65
73
  use(module) {
66
74
  module.applyTo(this);
67
75
  return this;
@@ -115,7 +123,8 @@ export class Container {
115
123
  * @private
116
124
  */
117
125
  removeScope(child) {
118
- this.scopes.delete(child);
126
+ const index = this.scopes.indexOf(child);
127
+ this.scopes.splice(index, 1);
119
128
  }
120
129
  validateContainer() {
121
130
  ContainerDisposedError.assert(!this.isDisposed, 'Container is already disposed');
@@ -4,6 +4,9 @@ export class EmptyContainer {
4
4
  get isDisposed() {
5
5
  throw new MethodNotImplementedError();
6
6
  }
7
+ detachFromParent() {
8
+ throw new MethodNotImplementedError();
9
+ }
7
10
  hasProvider(key) {
8
11
  return false;
9
12
  }
@@ -16,10 +16,6 @@ export class HookContext {
16
16
  // @ts-ignore
17
17
  this.instance[this.methodName] = fn(this.scope);
18
18
  }
19
- getProperty() {
20
- // @ts-ignore
21
- return this.instance[this.methodName];
22
- }
23
19
  }
24
20
  export const createHookContext = (Target, scope, methodName = 'constructor') => new HookContext(Target, scope, methodName);
25
21
  export const hookMetaKey = (methodName = 'constructor') => `inject:${methodName}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "37.2.0",
3
+ "version": "37.2.3",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -51,7 +51,8 @@
51
51
  "format": "prettier --write \"**/*.ts\"",
52
52
  "lint": "eslint lib/**/*.ts __tests__/**/*.ts scripts/**/*.ts",
53
53
  "lint:fix": "npm run lint --fix",
54
- "prepare": "husky"
54
+ "prepare": "husky",
55
+ "release": "npm run build && npm test && npm publish"
55
56
  },
56
57
  "devDependencies": {
57
58
  "@types/jest": "29.5.14",
@@ -6,6 +6,7 @@ export declare abstract class AutoMockedContainer implements IContainer {
6
6
  createScope(): IContainer;
7
7
  abstract resolve<T>(key: InjectionToken<T>, options?: ResolveOptions): T;
8
8
  dispose(): void;
9
+ detachFromParent(): void;
9
10
  register(): this;
10
11
  getParent(): undefined;
11
12
  getScopes(): never[];
@@ -16,7 +16,7 @@ export declare class Container implements IContainer {
16
16
  constructor(injector: IInjector, options?: {
17
17
  parent?: IContainer;
18
18
  tags?: Tag[];
19
- onConstruct?: (instance: Instance) => void;
19
+ onConstruct?: (instance: Instance, scope: IContainer) => void;
20
20
  onDispose?: (scope: IContainer) => void;
21
21
  });
22
22
  add(registration: IRegistration): this;
@@ -26,6 +26,7 @@ export declare class Container implements IContainer {
26
26
  dispose({ cascade }?: {
27
27
  cascade?: boolean;
28
28
  }): void;
29
+ detachFromParent(): void;
29
30
  use(module: IContainerModule): this;
30
31
  hasProvider(key: DependencyKey): boolean;
31
32
  resolveManyByAlias(predicate: AliasPredicate, { args, child, lazy }?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
@@ -3,6 +3,7 @@ import { IProvider } from '../provider/IProvider';
3
3
  import { IRegistration } from '../registration/IRegistration';
4
4
  export declare class EmptyContainer implements IContainer {
5
5
  get isDisposed(): boolean;
6
+ detachFromParent(): void;
6
7
  hasProvider(key: string): boolean;
7
8
  getParent(): undefined;
8
9
  getScopes(): never[];
@@ -46,4 +46,5 @@ export interface IContainer extends Resolvable, Tagged {
46
46
  getInstances(): Instance[];
47
47
  resolveManyByAlias(predicate: AliasPredicate, options?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
48
48
  resolveOneByAlias<T>(predicate: AliasPredicate, options?: ResolveOptions): [DependencyKey, T];
49
+ detachFromParent(): void;
49
50
  }
@@ -9,7 +9,6 @@ export interface IHookContext {
9
9
  args?: unknown[];
10
10
  }): unknown;
11
11
  setProperty(fn: InjectFn): void;
12
- getProperty(): unknown;
13
12
  }
14
13
  export declare class HookContext implements IHookContext {
15
14
  instance: object;
@@ -21,7 +20,6 @@ export declare class HookContext implements IHookContext {
21
20
  args?: unknown[];
22
21
  }): unknown;
23
22
  setProperty(fn: InjectFn): void;
24
- getProperty(): unknown;
25
23
  }
26
24
  export type CreateHookContext = (Target: object, scope: IContainer, methodName?: string) => IHookContext;
27
25
  export declare const createHookContext: CreateHookContext;