ClementRomac's picture
ClementRomac HF Staff
Added interactive demo with some policies
09a6f7f
raw
history blame contribute delete
913 Bytes
/**
* @classdesc UI state manager.
*/
export default class PubSub {
/**
* @constructor
*/
constructor() {
this.events = {};
}
/**
* Adds the given callback function to the given event and adds the event to the handled events if it is not in it yet.
* @param event {string}
* @param callback {function}
* @return {number}
*/
subscribe(event, callback) {
if (!this.events.hasOwnProperty(event)) {
this.events[event] = [];
}
return this.events[event].push(callback);
}
/**
* Triggers all the callback functions for the given event.
* @param event {string}
* @param data
* @return {*[]|*}
*/
publish(event, data = {}) {
if (!this.events.hasOwnProperty(event)) {
return [];
}
return this.events[event].map(callback => callback(data));
}
}