Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make smaller and update REAME

  • Loading branch information...
commit a0909749bcfa7335d6d9e2a29b338aa8f1f11dfd 1 parent 8ac27ed
Benjamin Thomas authored
Showing with 72 additions and 45 deletions.
  1. +13 −8 README.md
  2. +58 −36 smokesignals.js
  3. +1 −1  smokesignals.min.js
View
21 README.md
@@ -4,13 +4,13 @@ Smoke Signals
Really, really lightweight event emitting for Node and the browser.
(though Node already has [event emitting built in][1] so I don't know why you'd
-use this there. This doesn't even have most of the functionallity that Node's
-event emitter has.)
+use this there. This is directly inspired by Node's and doesn't even have most of
+the functionality that Node's event emitter has.)
This library has three goals:
1. Make it easy to listen for and initiate events.
-2. Be small. Right now the minified version comes in at 542 bytes (181 bytes
+2. Be small. Right now the minified version comes in at 462 bytes (154 bytes
gzipped).
3. Not pollute the global namespace or the objects it modifies with a bunch of
crap.
@@ -67,10 +67,6 @@ And remove a listener:
john.off('event name', myListener);
-Or remove all listeners for an event:
-
- john.off('event name');
-
Or if you only want to listen for an event once:
john.once('another event', function() {
@@ -78,12 +74,21 @@ Or if you only want to listen for an event once:
});
john.trigger('another event');
+Or remove all listeners for an event:
+
+ john.off('event name');
+
+Or if you want to remove all listeners period:
+
+ // just reconvert the object...
+ smokesignals.convert(john);
+
That's it! One global object (`smokesignals`) and when used it adds 4 methods to
your objects (`on`, `once`, `off` and `trigger`).
By the way, all methods are chainable:
- john
+ var john = smokesignals.convert({})
.on('event name', function() { ... })
.on('event two', function() { ... })
.trigger('event name')
View
94 smokesignals.js
@@ -1,47 +1,69 @@
-(function (definition,undef) {
- undef+='';
- typeof module != undef ? module.exports = definition() :
- typeof define != undef ? define(definition) :
- this['smokesignals'] = definition()
-})(function() {
- return {
- convert: function(obj) {
- var listeners = {};
+// guess if we are in node or not, if not add our smokesignals object to global namespace
+(typeof module == 'object' ? exports : smokesignals = {})
+ // add convert method to our object
+ .convert = function(obj) {
+ // we store the list of handlers as a local variable inside the scope
+ // so that we don't have to add random properties to the object we are
+ // converting. (prefixing variables in the object with an underscore or
+ // two is an ugly solution)
+ var handlers = {};
- obj.on = function(eventName, handler) {
- (listeners[eventName] || (listeners[eventName] = [])).push(handler);
- return obj;
- }
+ // add a listener
+ obj.on = function(eventName, handler) {
+ // either use the existing array or create a new one for this event
+ (handlers[eventName] || (handlers[eventName] = []))
+ // add the handler to the array
+ .push(handler);
- obj.once = function(eventName, handler) {
- function wrappedHandler() {
- return handler.apply(obj.off(eventName, wrappedHandler), arguments);
+ return obj;
}
- wrappedHandler.h = handler;
- return obj.on(eventName, wrappedHandler);
- }
+ // add a listener that will only be called once
+ obj.once = function(eventName, handler) {
+ // create a wrapper listener, that will remove itself after it is called
+ function wrappedHandler() {
+ // remove ourself, and then call the real handler with the args
+ // passed to this wrapper
+ return handler.apply(obj.off(eventName, wrappedHandler), arguments);
+ }
+ // in order to allow that these wrapped handlers can be removed by
+ // removing the original function, we save a reference to the original
+ // function
+ wrappedHandler.h = handler;
- obj.off = function(eventName, handler) {
- for (var list = listeners[eventName], i = 0; handler && list && list[i]; i++) {
- if (list[i] == handler || list[i].h == handler) {
- list.splice(i--,1);
- }
+ // call the regular add listener function with our new wrapper
+ return obj.on(eventName, wrappedHandler);
}
- if (!i) {
- delete listeners[eventName];
+
+ // remove a listener
+ obj.off = function(eventName, handler) {
+ // loop through all handlers for this eventName, assuming a handler
+ // was passed in, to see if the handler passed in was any of them so
+ // we can remove the handler
+ for (var list = handlers[eventName], i = 0; handler && list && list[i]; i++) {
+ // either this item is the handler passed in, or this item is a
+ // wrapper for the handler passed in. See the 'once' function
+ if (list[i] == handler || list[i].h == handler) {
+ // remove it!
+ list.splice(i--,1);
+ }
+ }
+ // if i is 0 (so falsy), then there are no items in the array for this
+ // event name (or the array doesn't exist)
+ if (!i) {
+ // remove the array for this eventname
+ delete handlers[eventName];
+ }
+ return obj;
}
- return obj;
- }
- obj.trigger = function(eventName) {
- for(var list = listeners[eventName], i = 0;list && list[i];) {
- list[i++].apply(obj, list.slice.call(arguments, 1));
+ obj.trigger = function(eventName) {
+ // loop throught all handlers for this event hame and call them all
+ for(var list = handlers[eventName], i = 0;list && list[i];) {
+ list[i++].apply(obj, list.slice.call(arguments, 1));
+ }
+ return obj;
}
- return obj;
- }
- return obj;
+ return obj;
}
- }
-});
View
2  smokesignals.min.js
@@ -1 +1 @@
-(function(a,e){e+="";typeof module!=e?(module.exports=a()):typeof define!=e?define(a):(this.smokesignals=a())})(function(){return{convert:function(a){var e={};a.on=function(d,b){(e[d]||(e[d]=[])).push(b);return a};a.once=function(d,b){function c(){return b.apply(a.off(d,c),arguments)}c.h=b;return a.on(d,c)};a.off=function(d,b){for(var c=e[d],f=0;b&&c&&c[f];f++)if(c[f]==b||c[f].h==b)c.splice(f--,1);f||delete e[d];return a};a.trigger=function(d){for(var b=e[d],c=0;b&&b[c];)b[c++].apply(a,b.slice.call(arguments,1));return a};return a}}});
+(typeof module=="object"?exports:(smokesignals={})).convert=function(c){var f={};c.on=function(d,a){(f[d]||(f[d]=[])).push(a);return c};c.once=function(d,a){function b(){return a.apply(c.off(d,b),arguments)}b.h=a;return c.on(d,b)};c.off=function(d,a){for(var b=f[d],e=0;a&&b&&b[e];e++)if(b[e]==a||b[e].h==a)b.splice(e--,1);e||delete f[d];return c};c.trigger=function(d){for(var a=f[d],b=0;a&&a[b];)a[b++].apply(c,a.slice.call(arguments,1));return c};return c};
Please sign in to comment.
Something went wrong with that request. Please try again.