electron/lib/renderer/extensions/event.ts

21 lines
410 B
TypeScript
Raw Normal View History

export class Event {
private listeners: Function[] = []
addListener (callback: Function) {
2020-03-20 20:28:31 +00:00
this.listeners.push(callback);
2016-06-16 16:51:54 +00:00
}
removeListener (callback: Function) {
2020-03-20 20:28:31 +00:00
const index = this.listeners.indexOf(callback);
2016-06-16 16:51:54 +00:00
if (index !== -1) {
2020-03-20 20:28:31 +00:00
this.listeners.splice(index, 1);
2016-06-16 16:51:54 +00:00
}
}
emit (...args: any[]) {
2016-06-16 16:51:54 +00:00
for (const listener of this.listeners) {
2020-03-20 20:28:31 +00:00
listener(...args);
2016-06-16 16:51:54 +00:00
}
}
}