1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/common/event/Event2Args.ts

22 lines
492 B
TypeScript
Raw Normal View History

export class Event2Args<T, M> {
private handlers: ((data?: T, data2?: M) => void)[] = [];
2016-03-13 18:28:29 +08:00
public on(handler: (data?: T, data2?: M) => void) {
this.handlers.push(handler);
}
2016-03-13 18:28:29 +08:00
public off(handler: (data?: T, data2?: M) => void) {
this.handlers = this.handlers.filter(h => h !== handler);
}
2016-03-13 18:28:29 +08:00
public allOff() {
this.handlers = [];
}
2016-03-13 18:28:29 +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
}
}
2016-03-13 18:28:29 +08:00
}