UNPKG

@bernotieno/mini-framework

Version:

A lightweight JavaScript framework built from scratch with zero dependencies

40 lines (34 loc) 774 B
/** * Event Utilities * Simple event bus for component communication */ export class EventBus { constructor() { this.events = {}; } on(event, callback) { if (!this.events[event]) { this.events[event] = []; } this.events[event].push(callback); return () => { const index = this.events[event].indexOf(callback); if (index > -1) { this.events[event].splice(index, 1); } }; } emit(event, data) { if (this.events[event]) { this.events[event].forEach(callback => callback(data)); } } off(event, callback) { if (this.events[event]) { const index = this.events[event].indexOf(callback); if (index > -1) { this.events[event].splice(index, 1); } } } }