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