UNPKG

@bemit/consent-ui

Version:

26 lines 658 B
export class EventAware { handlers = {}; state = { events: 0 }; call(scope, ...data) { const handlers = Object.values(this.handlers[scope]); handlers.forEach(handler => handler(...data)); } getEvtId() { return String(this.state.events = this.state.events + 1); } on(scope, cb) { const evtId = this.getEvtId(); if (!this.handlers[scope]) { this.handlers[scope] = {}; } this.handlers[scope][evtId] = cb; return evtId; } off(scope, evtId) { if (typeof this.handlers[scope] === 'object' && typeof this.handlers[scope]?.[evtId] === 'object') { delete this.handlers[scope][evtId]; } } }