UNPKG

armisa-models

Version:
43 lines (42 loc) 1.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Events = void 0; class Events { constructor() { this.events = []; this.on = (pageKey, eventName, callBack) => { const handlers = this.events.find((i) => i.eventName === eventName && i.pageKey === pageKey); if (handlers) { handlers.callback.push(callBack); } else { const newHandler = { eventName: eventName, pageKey: pageKey, callback: [], }; newHandler.callback.push(callBack); this.events.push(newHandler); } }; this.remove = (pageKey, eventName) => { this.events = this.events.filter((i) => !(i.eventName === eventName && i.pageKey === pageKey)); }; this.removeAllForThisPage = (pageKey) => { this.events = this.events.filter((i) => i.pageKey !== pageKey); }; this.trigger = (eventName, ...args) => { const handlers = this.events.filter((i) => i.eventName === eventName); if (handlers && handlers.length > 0) { handlers.forEach((handler) => { if (handler && handler.callback && handler.callback.length > 0) { handler.callback.forEach((callback) => { callback.apply(null, args); }); } }); } }; } } exports.Events = Events;