File size: 913 Bytes
09a6f7f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/**
* @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));
}
} |