/** * @file EventBus.js * @description Simple event bus for component communication * @author Implementation Team */ /** * @class EventBus * @description Facilitates publish-subscribe pattern for loose coupling between components * * @example * const eventBus = new EventBus(); * eventBus.subscribe('piece:moved', (data) => console.log(data)); * eventBus.publish('piece:moved', {from: 'e2', to: 'e4'}); */ class EventBus { constructor() { /** * @property {Map} _subscribers - Map of event names to arrays of callbacks */ this._subscribers = new Map(); } /** * Subscribe to an event * * @param {string} event - Event name * @param {Function} callback - Function to call when event is published * @returns {Function} Unsubscribe function * * @example * const unsubscribe = eventBus.subscribe('game:check', handleCheck); * // Later: unsubscribe() */ subscribe(event, callback) { // TODO: Implement subscription // 1. Check if event exists in _subscribers // 2. If not, create new array for this event // 3. Add callback to array // 4. Return unsubscribe function that removes this callback // Return unsubscribe function return () => { // TODO: Implement unsubscribe logic }; } /** * Publish an event with data * * @param {string} event - Event name * @param {*} data - Data to pass to subscribers * * @example * eventBus.publish('piece:moved', { * piece: pawn, * from: {row: 6, col: 4}, * to: {row: 4, col: 4} * }); */ publish(event, data) { // TODO: Implement publishing // 1. Get all subscribers for this event // 2. Call each callback with the data // 3. Handle any errors in callbacks (try-catch) } /** * Unsubscribe a specific callback from an event * * @param {string} event - Event name * @param {Function} callback - Callback to remove * * @example * eventBus.unsubscribe('game:check', handleCheck); */ unsubscribe(event, callback) { // TODO: Implement unsubscribe // 1. Get subscribers for event // 2. Find and remove the callback } /** * Remove all subscribers for an event * * @param {string} event - Event name * * @example * eventBus.clear('game:over'); */ clear(event) { // TODO: Implement clear // Remove all subscribers for the event } /** * Remove all subscribers for all events * * @example * eventBus.clearAll(); */ clearAll() { // TODO: Implement clear all this._subscribers.clear(); } /** * Get count of subscribers for an event * * @param {string} event - Event name * @returns {number} Number of subscribers * * @example * const count = eventBus.getSubscriberCount('piece:moved'); */ getSubscriberCount(event) { // TODO: Implement subscriber count return 0; // Replace with actual logic } /** * Check if an event has any subscribers * * @param {string} event - Event name * @returns {boolean} True if event has subscribers * * @example * if (eventBus.hasSubscribers('game:over')) { * // Do something * } */ hasSubscribers(event) { // TODO: Implement has subscribers check return false; // Replace with actual logic } } // Export singleton instance export default new EventBus();