Dispatcher.js 733 Bytes
  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
var dispCbs = [];
var dispIns = [];

function Dispatcher(){
dispIns.push(this);
dispCbs.push({});
}
Dispatcher.prototype = {
on(type, cb){
let cbtypes = dispCbs[dispIns.indexOf(this)];
let cbs = cbtypes[type] = (cbtypes[type] || []);
if(!~cbs.indexOf(cb)){
cbs.push(cb);
}
},
off(type, cb){
let cbtypes = dispCbs[dispIns.indexOf(this)];
let cbs = cbtypes[type] = (cbtypes[type] || []);
let curTypeCbIdx = cbs.indexOf(cb);
if(~curTypeCbIdx){
cbs.splice(curTypeCbIdx, 1);
}
},
fire(type, ...args){
let cbtypes = dispCbs[dispIns.indexOf(this)];
let cbs = cbtypes[type] = (cbtypes[type] || []);
for(let i = 0; i < cbs.length; i++){
cbs[i].apply(null, args);
}
}
};
module.exports = Dispatcher;