Skip to content

Instantly share code, notes, and snippets.

@ToxicFrog
Created June 22, 2012 17:41
Show Gist options
  • Save ToxicFrog/2974157 to your computer and use it in GitHub Desktop.
Save ToxicFrog/2974157 to your computer and use it in GitHub Desktop.
local handlers = setmetatable({}, { __index = function(self, k) self[k] = {}; return self[k]; end; })
event = {}
function event.add(name, fn)
assert(handlers[name][fn] == nil, "attempt to register the same callback multiple times for event " .. name)
handlers[name][fn] = true
return fn
end
function event.remove(name, fn)
assert(handlers[name][fn], "attempt to unregister a non-registered callback for event " .. name)
handlers[name][fn] = nil
end
function event.call(name, ...)
for handler in pairs(events[name]) do
handler(...)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment