31 lines
700 B
JavaScript
31 lines
700 B
JavaScript
import fastCall from './fast-call.js';
|
|
|
|
export default class EventEmitter {
|
|
$$listeners = {};
|
|
addListener(type, listener) {
|
|
if (!this.$$listeners[type]) {
|
|
this.$$listeners[type] = new Set();
|
|
}
|
|
this.$$listeners[type].add(listener);
|
|
}
|
|
invoke(type, ...args) {
|
|
const listeners = this.$$listeners[type];
|
|
if (!listeners) {
|
|
return;
|
|
}
|
|
for (const listener of listeners) {
|
|
fastCall(listener, undefined, args);
|
|
}
|
|
}
|
|
removeListener(type, listener) {
|
|
const listeners = this.$$listeners[type];
|
|
if (!listeners) {
|
|
return;
|
|
}
|
|
listeners.delete(listener);
|
|
if (0 === listeners.size) {
|
|
delete this.$$listeners[type];
|
|
}
|
|
}
|
|
}
|