1 ;(function(global ,undefined){ 2 var evts = {} 3 ,onceTag = ‘__event_once‘ 4 function emit(event ){ 5 var args = util.toArray(arguments , 1) 6 if (!(event in evts)) return 7 var _dels = [] 8 for (var i = 0 , j = evts[event].length ; i < j ;i ++){ 9 var cbk = evts[event][i]10 if (!cbk) return11 cbk.apply(null , args)12 if (cbk[onceTag]) { evts[event][i] = null ; _dels.push[i]}13 }14 for (var i = _dels.length -1 ; i>=0 ; i--) evts[event].splice(_dels[i] , 1)15 }16 17 function addMultiCon(event , listener){18 var once = true19 event.sort()20 addListener(event.join(‘|‘) , listener , once)21 var eventBubbles = []22 function tinOpener(evt){23 eventBubbles.push(evt)24 if (eventBubbles.length >= event.length) {25 eventBubbles.sort()26 emit(eventBubbles.join(‘|‘))27 }28 }29 30 for (var i = 0 ; i < event.length;i ++){31 addListener(event[i] , tinOpener.bind(null, event[i]) , once)32 }33 }34 35 function addListener(event , listener , once){36 if (util.isArray(event)) return addMultiCon(event , listener)37 38 39 if (!(event in evts)) evts[event] = []40 if (once) listener[onceTag] = true41 evts[event].push(listener)42 }43 44 function removeListener(event, listener){45 if (!listener) {46 delete evts[event]47 return48 }49 for (var i = 0 , j = evts[event].length ; i < j ;i ++){50 if (evts[event][i] === listener) { evts[event].splice(i, 1) ; break}51 }52 }53 54 function listeners(event){55 return evts[event]56 }57 global.emitter = {58 on : addListener59 ,once : function(event , listener){60 addListener(event , listener , true)61 }62 ,emit : emit63 ,listeners : listeners64 }65 })(this)
function toArray(colletions ,offset){ return Array.prototype.slice.call(colletions , offset || 0)}
Emitter. On (['test', 'test2'], function (){
Console. Log ('test is fired ')
})
Emitter. emit ('test ')
Emitter. emit ('test2 ')
Emitter enhanced multi-condition triggering