2022-11-08 10:42:14 +00:00
|
|
|
import { ISignal } from "./ISignal";
|
|
|
|
|
|
|
|
export class Signal<T> implements ISignal<T> {
|
|
|
|
private handlers: ((data: T) => void)[] = [];
|
2022-11-08 18:45:57 +00:00
|
|
|
private thisArgs: any[] = [];
|
2022-11-08 10:42:14 +00:00
|
|
|
|
2022-11-08 18:45:57 +00:00
|
|
|
public on(handler: (data: T) => void, thisArg: any): void {
|
2022-11-08 10:42:14 +00:00
|
|
|
this.handlers.push(handler);
|
2022-11-08 18:45:57 +00:00
|
|
|
this.thisArgs.push(thisArg);
|
2022-11-08 10:42:14 +00:00
|
|
|
}
|
|
|
|
public off(handler: (data: T) => void): void {
|
2022-11-08 18:45:57 +00:00
|
|
|
console.log("[OFF] " + this.handlers.length);
|
2022-11-08 10:42:14 +00:00
|
|
|
this.handlers = this.handlers.filter((h) => h !== handler);
|
2022-11-08 18:45:57 +00:00
|
|
|
console.log("[OFF] >> " + this.handlers.length);
|
2022-11-08 10:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public trigger(data: T): void {
|
2022-11-08 18:45:57 +00:00
|
|
|
//[...this.handlers].forEach((handler) => handler(data));
|
|
|
|
|
|
|
|
for (let i = 0; i < this.handlers.length; i++) {
|
|
|
|
this.handlers[i].call(this.thisArgs[i], data);
|
|
|
|
}
|
2022-11-08 10:42:14 +00:00
|
|
|
}
|
|
|
|
}
|