mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-01-08 12:03:11 +00:00
29 lines
911 B
TypeScript
29 lines
911 B
TypeScript
// Need to capture *this*
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import { ISignal } from "./ISignal";
|
|
|
|
export class Signal<T> implements ISignal<T> {
|
|
private handlers: ((data: T) => void)[] = [];
|
|
private thisArgs: any[] = [];
|
|
|
|
public on(handler: (data: T) => void, thisArg: any): void {
|
|
this.handlers.push(handler);
|
|
this.thisArgs.push(thisArg);
|
|
}
|
|
public off(handler: (data: T) => void): void {
|
|
const index: number = this.handlers.indexOf(handler);
|
|
this.handlers.splice(index, 1);
|
|
this.thisArgs.splice(index, 1);
|
|
}
|
|
|
|
public trigger(data: T): void {
|
|
// protect from trigger >> off
|
|
const handlers: ((data: T) => void)[] = [...this.handlers];
|
|
const thisArgs: any[] = [...this.thisArgs];
|
|
|
|
for (let i = 0; i < handlers.length; i++) {
|
|
handlers[i].call(thisArgs[i], data);
|
|
}
|
|
}
|
|
}
|