@fanoutio/eventstream
Version:
Connect-compatible middleware that enables the easy creation of EventStream endpoints
20 lines (19 loc) • 682 B
JavaScript
import Debug from 'debug';
const debug = Debug('eventstream');
export default class AddressedEvents {
listeners = [];
addListener(fn) {
this.listeners.push(fn);
debug('AddressedEvents listener added, ' + this.listeners.length + ' listeners');
return () => {
this.listeners = this.listeners.filter((x) => x !== fn);
debug('AddressedEvents listener removed, ' + this.listeners.length + ' listeners');
};
}
async addressedEvent(event) {
debug('Calling addressedEvent with ' + this.listeners.length + ' listeners');
for (const fn of this.listeners) {
await fn(event);
}
}
}