Created
September 6, 2012 04:59
-
-
Save tbranyen/3651431 to your computer and use it in GitHub Desktop.
Mixin-able events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Portable & reusable Events object, not too different from what is found | |
// in Backbone. | |
// | |
// (Credits @visionmedia) & Modified from: | |
// https://raw.github.com/component/emitter/master/index.js | |
var Events = { | |
// Cache all callbacks. | |
callbacks: {}, | |
// Listen on the given `event` with `fn`. | |
on: function(event, fn){ | |
var callback = this.callbacks[event] = this.callbacks[event] || []; | |
callback.push(fn); | |
return this; | |
}, | |
// Adds an `event` listener that will be invoked a single time then | |
// automatically removed. | |
once: function(event, fn){ | |
var self = this; | |
function on() { | |
self.off(event, on); | |
fn.apply(this, arguments); | |
} | |
fn._off = on; | |
this.on(event, on); | |
return this; | |
}, | |
// Remove the given callback for `event` or all registered callbacks. | |
off: function(event, fn){ | |
var i; | |
var callbacks = this.callbacks[event]; | |
if (!callbacks) { | |
return this; | |
} | |
// remove all handlers | |
if (arguments.length === 1) { | |
delete this.callbacks[event]; | |
return this; | |
} | |
// remove specific handler | |
i = callbacks.indexOf(fn._off || fn); | |
if (~i) { | |
callbacks.splice(i, 1); | |
} | |
return this; | |
}, | |
// Emit `event` with the given args. | |
emit: function(event){ | |
var i, len; | |
var args = [].slice.call(arguments, 1); | |
var callbacks = this.callbacks[event]; | |
if (callbacks) { | |
callbacks = callbacks.slice(0); | |
for (i = 0, len = callbacks.length; i < len; ++i) { | |
callbacks[i].apply(this, args); | |
} | |
} | |
return this; | |
}, | |
// Return array of callbacks for `event`. | |
listeners: function(event){ | |
return this.callbacks[event] || []; | |
}, | |
// Check if this emitter has `event` handlers. | |
hasListeners: function(event){ | |
return Boolean(this.listeners(event).length); | |
} | |
}; |
Keep in mind older browsers (IE < 9) lack Array#indexOf
.
Instead of [].slice
in emit
it would be rad to store a reference to slice
instead. That literal is just more garbage to be collected.
The use of ~i
in off()
isn't as clear as i < 0
One optimization would be to avoid this
binding by default.
See jsperf.com/avoiding-call
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be nice if
listeners()
didreturn this.callbacks[event] || (this.callbacks[event] = []);
so that devs holding the collection wouldn't have to grab it again once events are added.