2017-06-10 22:32:56 +02:00
|
|
|
export class Event2Args<T, M> {
|
2018-05-12 12:19:51 -04:00
|
|
|
private handlers: ((data?: T, data2?: M) => void)[] = [];
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public on(handler: (data?: T, data2?: M) => void): void {
|
2017-06-10 22:32:56 +02:00
|
|
|
this.handlers.push(handler);
|
|
|
|
}
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public off(handler: (data?: T, data2?: M) => void): void {
|
|
|
|
this.handlers = this.handlers.filter((h): boolean => h !== handler);
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public allOff(): void {
|
2017-06-10 22:32:56 +02:00
|
|
|
this.handlers = [];
|
|
|
|
}
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public trigger(data?: T, data2?: M): void {
|
2017-06-10 22:32:56 +02:00
|
|
|
if (this.handlers) {
|
2021-04-18 15:48:35 +02:00
|
|
|
this.handlers.slice(0).forEach((h): void => h(data, data2));
|
2016-03-13 11:28:29 +01:00
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-03-13 11:28:29 +01:00
|
|
|
}
|