|
|
function EventBaton(options) { |
|
|
this.eventMap = {}; |
|
|
this.options = options || {}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EventBaton.prototype.stealBaton = function(name, func, context) { |
|
|
if (!name) { throw new Error('need name'); } |
|
|
if (!func) { throw new Error('need func!'); } |
|
|
|
|
|
var listeners = this.eventMap[name] || []; |
|
|
listeners.push({ |
|
|
func: func, |
|
|
context: context |
|
|
}); |
|
|
this.eventMap[name] = listeners; |
|
|
}; |
|
|
|
|
|
EventBaton.prototype.sliceOffArgs = function(num, args) { |
|
|
var newArgs = []; |
|
|
for (var i = num; i < args.length; i++) { |
|
|
newArgs.push(args[i]); |
|
|
} |
|
|
return newArgs; |
|
|
}; |
|
|
|
|
|
EventBaton.prototype.trigger = function(name) { |
|
|
|
|
|
var argsToApply = this.sliceOffArgs(1, arguments); |
|
|
|
|
|
var listeners = this.eventMap[name]; |
|
|
if (!listeners || !listeners.length) { |
|
|
console.warn('no listeners for', name); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
var toCall = listeners.slice(-1)[0]; |
|
|
toCall.func.apply(toCall.context, argsToApply); |
|
|
}; |
|
|
|
|
|
EventBaton.prototype.getNumListeners = function(name) { |
|
|
var listeners = this.eventMap[name] || []; |
|
|
return listeners.length; |
|
|
}; |
|
|
|
|
|
EventBaton.prototype.getListenersThrow = function(name) { |
|
|
var listeners = this.eventMap[name]; |
|
|
if (!listeners || !listeners.length) { |
|
|
throw new Error('no one has that baton!' + name); |
|
|
} |
|
|
return listeners; |
|
|
}; |
|
|
|
|
|
EventBaton.prototype.passBatonBackSoft = function(name, func, context, args) { |
|
|
try { |
|
|
return this.passBatonBack(name, func, context, args); |
|
|
} catch (e) { |
|
|
} |
|
|
}; |
|
|
|
|
|
EventBaton.prototype.passBatonBack = function(name, func, context, args) { |
|
|
|
|
|
|
|
|
|
|
|
var listeners = this.getListenersThrow(name); |
|
|
|
|
|
var indexBefore; |
|
|
listeners.forEach(function(listenerObj, index) { |
|
|
|
|
|
if (index === 0) { return; } |
|
|
if (listenerObj.func === func && listenerObj.context === context) { |
|
|
indexBefore = index - 1; |
|
|
} |
|
|
}); |
|
|
if (indexBefore === undefined) { |
|
|
throw new Error('you are the last baton holder! or i didn\'t find you'); |
|
|
} |
|
|
var toCallObj = listeners[indexBefore]; |
|
|
|
|
|
toCallObj.func.apply(toCallObj.context, args); |
|
|
}; |
|
|
|
|
|
EventBaton.prototype.releaseBaton = function(name, func, context) { |
|
|
|
|
|
|
|
|
var listeners = this.getListenersThrow(name); |
|
|
|
|
|
var newListeners = []; |
|
|
var found = false; |
|
|
listeners.forEach(function(listenerObj) { |
|
|
if (listenerObj.func === func && listenerObj.context === context) { |
|
|
if (found) { |
|
|
console.warn('woah duplicates!!!'); |
|
|
console.log(listeners); |
|
|
} |
|
|
found = true; |
|
|
} else { |
|
|
newListeners.push(listenerObj); |
|
|
} |
|
|
}); |
|
|
|
|
|
if (!found) { |
|
|
console.log('did not find that function', func, context, name, arguments); |
|
|
console.log(this.eventMap); |
|
|
throw new Error('can\'t releasebaton if you don\'t have it'); |
|
|
} |
|
|
this.eventMap[name] = newListeners; |
|
|
}; |
|
|
|
|
|
exports.EventBaton = EventBaton; |
|
|
|