Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 33x 28x | /* eslint-disable @typescript-eslint/no-explicit-any */
interface TimelineEvent {
topic: string;
callback: (args: any) => void;
}
/**
* Timeline events emitter.
*/
export class TimelineEventsEmitter {
/**
* Active events subscriptions.
*/
_subscriptions: TimelineEvent[] = [];
/**
* Subscribe event.
* @param topic event name.
* @param callback callback to be added.
*/
on<T>(topic: string, callback: (args: T) => void): boolean {
if (!callback) {
return false;
}
this._subscriptions.push({
topic: topic,
callback: callback,
} as TimelineEvent);
return true;
}
/**
* Remove an event from the subscriptions list.
*/
off<T>(topic: string, callback: (args: T) => void): boolean {
const before = this._subscriptions.length;
this._subscriptions = this._subscriptions.filter((event) => {
return event && event.callback != callback && event.topic != topic;
});
return before !== this._subscriptions.length;
}
/**
* Unsubscribe all
*/
offAll(): void {
// Remove all callbacks from array.
this._subscriptions.length = 0;
}
/**
* Emit event.
* @param topic Event name.
* @param args Event arguments.
*/
emit<T>(topic: string, args: T): void {
this._subscriptions.forEach((event) => {
if (event?.topic === topic && event?.callback) {
event.callback(args);
}
});
}
}
|