2018-04-01 20:21:11 +08:00
|
|
|
/**
|
|
|
|
* Simple global events manager
|
|
|
|
*/
|
|
|
|
class Events {
|
|
|
|
constructor() {
|
|
|
|
this.listeners = {};
|
|
|
|
this.stack = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(eventName, eventData) {
|
|
|
|
this.stack.push({name: eventName, data: eventData});
|
|
|
|
if (typeof this.listeners[eventName] === 'undefined') return this;
|
|
|
|
let eventsToStart = this.listeners[eventName];
|
|
|
|
for (let i = 0; i < eventsToStart.length; i++) {
|
|
|
|
let event = eventsToStart[i];
|
|
|
|
event(eventData);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
listen(eventName, callback) {
|
|
|
|
if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = [];
|
|
|
|
this.listeners[eventName].push(callback);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default Events;
|