refactor event handling to consume base class for lifecycling
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { EventHandler, ISignal } from './ISignal';
|
||||
import { IEventSubscription } from './ISubscription';
|
||||
import { EventHandler, IEventSource, IEventSubscription } from './IEventSource';
|
||||
|
||||
export class Signal<T> implements ISignal<T> {
|
||||
export class EventSource<T> implements IEventSource<T> {
|
||||
private handlers = new Map<number, EventHandler<T>>();
|
||||
|
||||
public on(handler: EventHandler<T>): IEventSubscription {
|
||||
12
src/infrastructure/Events/EventSubscriptionCollection.ts
Normal file
12
src/infrastructure/Events/EventSubscriptionCollection.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { IEventSubscription } from './IEventSource';
|
||||
|
||||
export class EventSubscriptionCollection {
|
||||
private readonly subscriptions = new Array<IEventSubscription>();
|
||||
public register(...subscriptions: IEventSubscription[]) {
|
||||
this.subscriptions.push(...subscriptions);
|
||||
}
|
||||
public unsubscribeAll() {
|
||||
this.subscriptions.forEach((listener) => listener.unsubscribe());
|
||||
this.subscriptions.splice(0, this.subscriptions.length);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import { IEventSubscription } from './ISubscription';
|
||||
export interface ISignal<T> {
|
||||
export interface IEventSource<T> {
|
||||
on(handler: EventHandler<T>): IEventSubscription;
|
||||
}
|
||||
|
||||
export interface IEventSubscription {
|
||||
unsubscribe(): void;
|
||||
}
|
||||
|
||||
export type EventHandler<T> = (data: T) => void;
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
export interface IEventSubscription {
|
||||
unsubscribe(): void;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Signal } from '../Events/Signal';
|
||||
import { EventSource } from '../Events/EventSource';
|
||||
|
||||
export class AsyncLazy<T> {
|
||||
private valueCreated = new Signal();
|
||||
private valueCreated = new EventSource();
|
||||
private isValueCreated = false;
|
||||
private isCreatingValue = false;
|
||||
private value: T | undefined;
|
||||
@@ -15,7 +15,7 @@ export class AsyncLazy<T> {
|
||||
public async getValueAsync(): Promise<T> {
|
||||
// If value is already created, return the value directly
|
||||
if (this.isValueCreated) {
|
||||
return Promise.resolve(this.value as T);
|
||||
return Promise.resolve(this.value);
|
||||
}
|
||||
// If value is being created, wait until the value is created and then return it.
|
||||
if (this.isCreatingValue) {
|
||||
|
||||
Reference in New Issue
Block a user