let events = {};

const on = (name,self,callback)=>{
    let tuple = [self,callback];
    let cb = events[name];
    if(Array.isArray(cb)){
        cb.push(tuple);
    }else {
        events[name] = [tuple];
    }
};

const emit = (name,data)=>{
    let callbacks = events[name];
    if(Array.isArray(callbacks)){
        callbacks.map((tuple)=>{
            let self = tuple[0];
            let callback = tuple[1];
            callback.call(self,data);
        })
    }
};

const remove = (name,self)=>{
    let callbacks = events[name];
    if(Array.isArray(callbacks)){
        events[name] = callbacks.filter(turple=>turple[0] !== self)
    }
};

module.exports = {on,emit,remove};