Created
February 17, 2013 04:34
-
-
Save bjcull/4970183 to your computer and use it in GitHub Desktop.
A tiny javascript event engine
This file contains hidden or 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
var QuickEvent = function () { | |
var nextSubscriberId = 0; | |
var subscriberList = []; | |
var subscribe = function (callback) { | |
var id = nextSubscriberId; | |
subscriberList[id] = callback; | |
nextSubscriberId++; | |
return id; | |
}; | |
var unsubscribe = function (id) { | |
delete subscriberList[id]; | |
}; | |
var trigger = function (sender) { | |
for (var i in subscriberList) { | |
subscriberList[i].apply(sender, Array.prototype.slice.call(arguments, 1)); | |
} | |
}; | |
return { | |
subscribe: subscribe, | |
unsubscribe: unsubscribe, | |
trigger: trigger | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just ported to CoffeeScript: