UNPKG

@logcomex/aylawc-core

Version:

An experimental library of web components

25 lines 769 B
import { v4 } from 'uuid'; export class EventDispatcher { constructor() { this._handlers = {}; } next(event) { if (this._handlers[event.name]) this._handlers[event.name].forEach((evt) => evt.handle(event.payload)); } subscribe(name, fn) { const id = v4(); if (!this._handlers[name]) this._handlers[name] = []; this._handlers[name].push({ id, handle: fn }); return () => { this.unsubscribe(name, id); }; } unsubscribe(name, id) { if (this._handlers[name]) this._handlers[name] = this._handlers[name].filter((handler) => handler.id !== id); } unsubscribeAll() { this._handlers = {}; } } //# sourceMappingURL=event-dispatcher.js.map