Start using sinatra & ruby for backend
Signed-off-by: Syed Daanish <me@syedm.dev>
This commit is contained in:
75
public/src/js/event_bus.js
Normal file
75
public/src/js/event_bus.js
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* @author Syed Daanish <me@syedm.dev>
|
||||
* @file `event_bus.js`
|
||||
*/
|
||||
|
||||
/**
|
||||
* @name EventBus
|
||||
* @access public
|
||||
* @classdesc Handles event emission & listening
|
||||
* to connect with other modules.
|
||||
* @exports EventBus
|
||||
* @default
|
||||
*/
|
||||
export default class EventBus {
|
||||
/**
|
||||
* @constructs EventBus
|
||||
* @function constructor
|
||||
* @description Initializes the event bus
|
||||
*/
|
||||
constructor() {
|
||||
this.retreivable_events = {};
|
||||
this.events = {};
|
||||
}
|
||||
/**
|
||||
* @function on
|
||||
* @description Sets up an event listener
|
||||
* @param {string} event - Event name
|
||||
* @param {Function} callback - Callback function to call
|
||||
* (multiple functions can be added)
|
||||
*/
|
||||
on(event, callback) {
|
||||
if (!this.events[event]) {
|
||||
this.events[event] = [];
|
||||
}
|
||||
this.events[event].push(callback);
|
||||
}
|
||||
/**
|
||||
* @function emit
|
||||
* @description Emits an event
|
||||
* @param {string} event - Event name
|
||||
* @param {...*} args - Arguments to pass to the callback
|
||||
* functions (0 or more)
|
||||
*/
|
||||
emit(event, ...args) {
|
||||
if (this.events[event]) {
|
||||
this.events[event].forEach((fn) => {
|
||||
fn(...args);
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @function onRetrievable
|
||||
* @description Sets up a retrievable event listener
|
||||
* @param {string} event - Event name
|
||||
* @param {Function} callback - Callback function to call
|
||||
* (can only be used once)
|
||||
*/
|
||||
onRetrievable(event, callback) {
|
||||
this.retreivable_events[event] = callback;
|
||||
}
|
||||
/**
|
||||
* @function get
|
||||
* @description Retrieves data from an event
|
||||
* @param {string} event - Event name
|
||||
* @param {...*} args - Arguments to pass to the callback
|
||||
* function (only one)
|
||||
* @returns {any|undefined} - Data from the event or undefined
|
||||
*/
|
||||
get(event, ...args) {
|
||||
if (this.retreivable_events[event]) {
|
||||
return this.retreivable_events[event](...args);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user