Files
2018-04-10 14:51:39 +10:00

20 lines
497 B
JavaScript

function Event(){
this.fns = [];
}
Event.prototype.subscribe = function(name, fn){
if(!name || typeof fn !== 'function') return;
this.fns.push({key: name, fn: fn});
}
Event.prototype.unsubscribe = function(name){
this.fns = this.fns.filter((data) => {
return data.key === name ? false : true;
});
}
Event.prototype.emit = function(name, payload){
this.fns.map((data) => {
if(data.key === name) data.fn(payload);
});
}
export const event = new Event();