Split up monolith index.js into multiple files

This commit is contained in:
2026-01-02 13:18:20 +11:00
parent 5764e41db3
commit 1d978e3244
10 changed files with 1460 additions and 1041 deletions

44
js/EventBus.js Normal file
View File

@@ -0,0 +1,44 @@
/**
* A simple publish-subscribe event bus for decoupled communication between components.
* @class EventBus
*/
export class EventBus {
constructor() {
/** @type {Object.<string, Function[]>} */
this.events = {};
}
/**
* Subscribe to an event.
* @param {string} event - The event name to subscribe to.
* @param {Function} callback - The callback function to execute when the event is emitted.
* @returns {Function} An unsubscribe function that removes this listener.
*/
on(event, callback) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(callback);
return () => this.off(event, callback);
}
/**
* Unsubscribe from an event.
* @param {string} event - The event name to unsubscribe from.
* @param {Function} callback - The callback function to remove.
*/
off(event, callback) {
if (!this.events[event]) return;
this.events[event] = this.events[event].filter(cb => cb !== callback);
}
/**
* Emit an event to all subscribers.
* @param {string} event - The event name to emit.
* @param {*} data - The data to pass to all subscribers.
*/
emit(event, data) {
if (!this.events[event]) return;
this.events[event].forEach(callback => callback(data));
}
}