/* 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)}}})() |
This comment has been minimized.
This comment has been minimized.
@aynik - you got me (xkcd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
I've improved this a bit more:
Where it differs:
Now part of https://github.com/aynik/be#emitter