UNPKG

@mr-samani/event-bus

Version:

A lightweight JavaScript event bus for any framework

28 lines (25 loc) 646 B
const bus = { events: {}, on(name, callback) { if (!this.events[name]) this.events[name] = []; this.events[name].push(callback); }, off(name) { delete this.events[name]; }, trigger(name, ...args) { if (this.events[name]) { this.events[name].forEach((cb) => cb(...args)); } }, }; // Attach to global object if (typeof window !== "undefined") { window.app = window.app || {}; window.app.event = bus; } else if (typeof global !== "undefined") { global.app = global.app || {}; global.app.event = bus; } // Export for environments that still want import export default bus;