ts-ioc-container 35.5.0 → 35.6.1

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/cjm/TypedEvent.js CHANGED
@@ -1,9 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypedEvent = void 0;
3
+ exports.TypedEvent = exports.EventDisposedError = void 0;
4
+ class EventDisposedError extends Error {
5
+ static assert(condition, message) {
6
+ if (!condition) {
7
+ throw new EventDisposedError(message);
8
+ }
9
+ }
10
+ }
11
+ exports.EventDisposedError = EventDisposedError;
4
12
  class TypedEvent {
5
13
  constructor() {
6
14
  this.observerStorage = [];
15
+ this.isDisposed = false;
7
16
  }
8
17
  static fromPromise(promise) {
9
18
  const event = new TypedEvent();
@@ -14,10 +23,12 @@ class TypedEvent {
14
23
  return event;
15
24
  }
16
25
  on(observer) {
26
+ this.validate();
17
27
  this.observerStorage.push(observer);
18
28
  return () => this.off(observer);
19
29
  }
20
30
  once(observer) {
31
+ this.validate();
21
32
  const onceObserver = (data) => {
22
33
  observer(data);
23
34
  this.off(onceObserver);
@@ -26,16 +37,25 @@ class TypedEvent {
26
37
  return () => this.off(onceObserver);
27
38
  }
28
39
  off(observer) {
40
+ this.validate();
29
41
  this.observerStorage = this.observerStorage.filter((o) => o !== observer);
30
42
  }
31
43
  emit(data) {
44
+ this.validate();
32
45
  this.observerStorage.forEach((o) => o(data));
33
46
  }
34
47
  toPromise() {
35
48
  return new Promise((resolve) => this.once(resolve));
36
49
  }
37
50
  dispose() {
51
+ this.validate();
52
+ this.isDisposed = true;
38
53
  this.observerStorage = [];
39
54
  }
55
+ validate() {
56
+ if (this.isDisposed) {
57
+ throw new EventDisposedError('Event is disposed');
58
+ }
59
+ }
40
60
  }
41
61
  exports.TypedEvent = TypedEvent;
@@ -12,6 +12,8 @@ class AutoMockedContainer {
12
12
  this.isDisposed = false;
13
13
  this.onConstruct = new TypedEvent_1.TypedEvent();
14
14
  this.onDispose = new TypedEvent_1.TypedEvent();
15
+ this.onScopeCreated = new TypedEvent_1.TypedEvent();
16
+ this.onScopeRemoved = new TypedEvent_1.TypedEvent();
15
17
  }
16
18
  findChild(matchFn) {
17
19
  return undefined;
@@ -25,6 +25,12 @@ class Container {
25
25
  get onDispose() {
26
26
  return this.parent.onDispose;
27
27
  }
28
+ get onScopeCreated() {
29
+ return this.parent.onScopeCreated;
30
+ }
31
+ get onScopeRemoved() {
32
+ return this.parent.onScopeRemoved;
33
+ }
28
34
  add(registration) {
29
35
  this.registrations.push(registration);
30
36
  registration.applyTo(this);
@@ -63,13 +69,12 @@ class Container {
63
69
  const scope = new Container(this.injector, { parent: this, tags, counter: this.counter });
64
70
  scope.applyRegistrationsFrom(this);
65
71
  this.scopes.add(scope);
72
+ this.onScopeCreated.emit(scope);
66
73
  return scope;
67
74
  }
68
75
  dispose() {
69
76
  this.validateContainer();
70
- this.onConstruct.dispose();
71
77
  this.onDispose.emit(this);
72
- this.onDispose.dispose();
73
78
  this.isDisposed = true;
74
79
  this.parent.removeScope(this);
75
80
  this.parent = new EmptyContainer_1.EmptyContainer();
@@ -162,6 +167,7 @@ class Container {
162
167
  */
163
168
  removeScope(child) {
164
169
  this.scopes.delete(child);
170
+ this.onScopeRemoved.emit(child);
165
171
  }
166
172
  validateContainer() {
167
173
  ContainerDisposedError_1.ContainerDisposedError.assert(!this.isDisposed, 'Container is already disposed');
@@ -12,6 +12,8 @@ class EmptyContainer {
12
12
  this.isDisposed = false;
13
13
  this.onConstruct = new TypedEvent_1.TypedEvent();
14
14
  this.onDispose = new TypedEvent_1.TypedEvent();
15
+ this.onScopeCreated = new TypedEvent_1.TypedEvent();
16
+ this.onScopeRemoved = new TypedEvent_1.TypedEvent();
15
17
  }
16
18
  hasInstance(value) {
17
19
  throw new MethodNotImplementedError_1.MethodNotImplementedError();
@@ -52,7 +54,12 @@ class EmptyContainer {
52
54
  getOwnInstances() {
53
55
  return [];
54
56
  }
55
- removeScope() { }
57
+ removeScope() {
58
+ this.onConstruct.dispose();
59
+ this.onDispose.dispose();
60
+ this.onScopeCreated.dispose();
61
+ this.onScopeRemoved.dispose();
62
+ }
56
63
  use(module) {
57
64
  throw new MethodNotImplementedError_1.MethodNotImplementedError();
58
65
  }
package/esm/TypedEvent.js CHANGED
@@ -1,6 +1,14 @@
1
+ export class EventDisposedError extends Error {
2
+ static assert(condition, message) {
3
+ if (!condition) {
4
+ throw new EventDisposedError(message);
5
+ }
6
+ }
7
+ }
1
8
  export class TypedEvent {
2
9
  constructor() {
3
10
  this.observerStorage = [];
11
+ this.isDisposed = false;
4
12
  }
5
13
  static fromPromise(promise) {
6
14
  const event = new TypedEvent();
@@ -11,10 +19,12 @@ export class TypedEvent {
11
19
  return event;
12
20
  }
13
21
  on(observer) {
22
+ this.validate();
14
23
  this.observerStorage.push(observer);
15
24
  return () => this.off(observer);
16
25
  }
17
26
  once(observer) {
27
+ this.validate();
18
28
  const onceObserver = (data) => {
19
29
  observer(data);
20
30
  this.off(onceObserver);
@@ -23,15 +33,24 @@ export class TypedEvent {
23
33
  return () => this.off(onceObserver);
24
34
  }
25
35
  off(observer) {
36
+ this.validate();
26
37
  this.observerStorage = this.observerStorage.filter((o) => o !== observer);
27
38
  }
28
39
  emit(data) {
40
+ this.validate();
29
41
  this.observerStorage.forEach((o) => o(data));
30
42
  }
31
43
  toPromise() {
32
44
  return new Promise((resolve) => this.once(resolve));
33
45
  }
34
46
  dispose() {
47
+ this.validate();
48
+ this.isDisposed = true;
35
49
  this.observerStorage = [];
36
50
  }
51
+ validate() {
52
+ if (this.isDisposed) {
53
+ throw new EventDisposedError('Event is disposed');
54
+ }
55
+ }
37
56
  }
@@ -9,6 +9,8 @@ export class AutoMockedContainer {
9
9
  this.isDisposed = false;
10
10
  this.onConstruct = new TypedEvent();
11
11
  this.onDispose = new TypedEvent();
12
+ this.onScopeCreated = new TypedEvent();
13
+ this.onScopeRemoved = new TypedEvent();
12
14
  }
13
15
  findChild(matchFn) {
14
16
  return undefined;
@@ -22,6 +22,12 @@ export class Container {
22
22
  get onDispose() {
23
23
  return this.parent.onDispose;
24
24
  }
25
+ get onScopeCreated() {
26
+ return this.parent.onScopeCreated;
27
+ }
28
+ get onScopeRemoved() {
29
+ return this.parent.onScopeRemoved;
30
+ }
25
31
  add(registration) {
26
32
  this.registrations.push(registration);
27
33
  registration.applyTo(this);
@@ -60,13 +66,12 @@ export class Container {
60
66
  const scope = new Container(this.injector, { parent: this, tags, counter: this.counter });
61
67
  scope.applyRegistrationsFrom(this);
62
68
  this.scopes.add(scope);
69
+ this.onScopeCreated.emit(scope);
63
70
  return scope;
64
71
  }
65
72
  dispose() {
66
73
  this.validateContainer();
67
- this.onConstruct.dispose();
68
74
  this.onDispose.emit(this);
69
- this.onDispose.dispose();
70
75
  this.isDisposed = true;
71
76
  this.parent.removeScope(this);
72
77
  this.parent = new EmptyContainer();
@@ -159,6 +164,7 @@ export class Container {
159
164
  */
160
165
  removeScope(child) {
161
166
  this.scopes.delete(child);
167
+ this.onScopeRemoved.emit(child);
162
168
  }
163
169
  validateContainer() {
164
170
  ContainerDisposedError.assert(!this.isDisposed, 'Container is already disposed');
@@ -9,6 +9,8 @@ export class EmptyContainer {
9
9
  this.isDisposed = false;
10
10
  this.onConstruct = new TypedEvent();
11
11
  this.onDispose = new TypedEvent();
12
+ this.onScopeCreated = new TypedEvent();
13
+ this.onScopeRemoved = new TypedEvent();
12
14
  }
13
15
  hasInstance(value) {
14
16
  throw new MethodNotImplementedError();
@@ -49,7 +51,12 @@ export class EmptyContainer {
49
51
  getOwnInstances() {
50
52
  return [];
51
53
  }
52
- removeScope() { }
54
+ removeScope() {
55
+ this.onConstruct.dispose();
56
+ this.onDispose.dispose();
57
+ this.onScopeCreated.dispose();
58
+ this.onScopeRemoved.dispose();
59
+ }
53
60
  use(module) {
54
61
  throw new MethodNotImplementedError();
55
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "35.5.0",
3
+ "version": "35.6.1",
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": "cad5efe78c49477ed0b69e9f381649c6b339f58a"
62
+ "gitHead": "7dae31e88ea99e92905ad1bd5ef2f2587a6d86ba"
63
63
  }
@@ -1,12 +1,17 @@
1
1
  export type IObserver<T> = (data: T) => void;
2
2
  export type IUnsubscribe = () => void;
3
+ export declare class EventDisposedError extends Error {
4
+ static assert(condition: boolean, message: string): void;
5
+ }
3
6
  export declare class TypedEvent<T> {
4
7
  static fromPromise<T>(promise: Promise<T>): TypedEvent<unknown>;
5
8
  private observerStorage;
9
+ private isDisposed;
6
10
  on(observer: IObserver<T>): IUnsubscribe;
7
11
  once(observer: IObserver<T>): IUnsubscribe;
8
12
  off(observer: IObserver<T>): void;
9
13
  emit(data: T): void;
10
14
  toPromise(): Promise<T>;
11
15
  dispose(): void;
16
+ private validate;
12
17
  }
@@ -8,6 +8,8 @@ export declare abstract class AutoMockedContainer implements IContainer {
8
8
  isDisposed: boolean;
9
9
  onConstruct: TypedEvent<Instance>;
10
10
  onDispose: TypedEvent<IContainer>;
11
+ onScopeCreated: TypedEvent<IContainer>;
12
+ onScopeRemoved: TypedEvent<IContainer>;
11
13
  findChild(matchFn: (s: IContainer) => boolean): IContainer | undefined;
12
14
  findParent(matchFn: (s: IContainer) => boolean): IContainer | undefined;
13
15
  hasDependency(key: string): boolean;
@@ -23,6 +23,8 @@ export declare class Container implements IContainer {
23
23
  });
24
24
  get onConstruct(): TypedEvent<Instance>;
25
25
  get onDispose(): TypedEvent<IContainer>;
26
+ get onScopeCreated(): TypedEvent<IContainer>;
27
+ get onScopeRemoved(): TypedEvent<IContainer>;
26
28
  add(registration: IRegistration): this;
27
29
  register(key: DependencyKey, provider: IProvider): this;
28
30
  resolve<T>(token: InjectionToken<T>, { args, child, lazy }?: ResolveOptions): T;
@@ -9,6 +9,8 @@ export declare class EmptyContainer implements IContainer {
9
9
  isDisposed: boolean;
10
10
  onConstruct: TypedEvent<Instance>;
11
11
  onDispose: TypedEvent<IContainer>;
12
+ onScopeCreated: TypedEvent<IContainer>;
13
+ onScopeRemoved: TypedEvent<IContainer>;
12
14
  hasInstance(value: object): boolean;
13
15
  reduceToRoot<TResult>(fn: ReduceScope<TResult>, initial: TResult): TResult;
14
16
  findChild(matchFn: (s: IContainer) => boolean): IContainer | undefined;
@@ -39,6 +39,8 @@ export interface IContainer extends Resolvable, Tagged {
39
39
  readonly isDisposed: boolean;
40
40
  onDispose: TypedEvent<IContainer>;
41
41
  onConstruct: TypedEvent<Instance>;
42
+ onScopeCreated: TypedEvent<IContainer>;
43
+ onScopeRemoved: TypedEvent<IContainer>;
42
44
  createScope(options?: CreateScopeOptions): IContainer;
43
45
  register(key: DependencyKey, value: IProvider): this;
44
46
  add(registration: IRegistration): this;