Skip to content

Instantly share code, notes, and snippets.

@Zolmeister
Created August 28, 2013 23:58
Show Gist options
  • Save Zolmeister/6372840 to your computer and use it in GitHub Desktop.
Save Zolmeister/6372840 to your computer and use it in GitHub Desktop.
/* The MIT License (MIT)
Copyright (c) 2013 Zolmeister
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
(function(){
var events = {}, i=0, list, args;
this.Events = {
on: function(type, func, context) {
events[type] = events[type] || []
events[type].push({f: func, c: context})
},
off: function(type, func) {
list = events[type] || []
if(!func) return list.length = 0
i = list.length
while(--i>=0){
func == list[i].f && list.splice(i,1)
}
},
emit: function() {
args = Array.apply([], arguments)
list = events[args.shift()] || []
args = args[0] instanceof Array && args[0] || args
i = list.length
while(--i>=0){
list[i].f.apply(list[i].c, args)
}
}
}
})()
345 bytes
(function(){var d={},c=0,a,b;this.Events={on:function(a,c,b){d[a]=d[a]||[];d[a].push({f:c,c:b})},off:function(b,e){a=d[b]||[];if(!e)return a.length=0;for(c=a.length;0<=--c;)e==a[c].f&&a.splice(c,1)},emit:function(){b=Array.apply([],arguments);a=d[b.shift()]||[];b=b[0]instanceof Array&&b[0]||b;for(c=a.length;0<=--c;)a[c].f.apply(a[c].c,b)}}})()
Copy link

ghost commented Aug 9, 2014

I've improved this a bit more:

(function(){
    var bound = {}, events, index;
    this.Events = {
        on: function(type, fn){
           return (bound[type] = bound[type]||[]).push(fn);
        },
        off: function(type, fn) {
            for (events = bound[type]||[], index = events.length; index--;) 
                fn == events[index] && events.splice(index, 1);
            if (!events.length) delete bound[type];
            return events.length;
        },
        emit: function(type) {
            for (events = bound[type]||[], index = events.length; index--;) 
                events[index].apply(this, [].slice.call(arguments, 1));
        }
    }; 
}());

Where it differs:

  • On & Off methods always return the number of bound events of the selected kind.
  • Added handler hygiene (delete).
  • No context management, context switch can be achieved using:
Events.emit.call(context, type, arg1); 
Events.emit.apply(context, type.concat(varargs));
  • 294B compressed:
!function(){var n,t,e={};this.Events={on:function(n,t){return(e[n]=e[n]||[]).push(t)},off:function(l,i){for(n=e[l]||[],t=n.length;t--;)i==n[t]&&n.splice(t,1);return n.length||delete e[l],n.length},emit:function(l){for(n=e[l]||[],t=n.length;t--;)n[t].apply(this,[].slice.call(arguments,1))}}}();

Now part of https://github.com/aynik/be#emitter

@Zolmeister
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment