adam@0: --[[ $Id: CallbackHandler-1.0.lua 14 2010-08-09 00:43:38Z mikk $ ]] adam@0: local MAJOR, MINOR = "CallbackHandler-1.0", 6 adam@0: local CallbackHandler = LibStub:NewLibrary(MAJOR, MINOR) adam@0: adam@0: if not CallbackHandler then return end -- No upgrade needed adam@0: adam@0: local meta = {__index = function(tbl, key) tbl[key] = {} return tbl[key] end} adam@0: adam@0: -- Lua APIs adam@0: local tconcat = table.concat adam@0: local assert, error, loadstring = assert, error, loadstring adam@0: local setmetatable, rawset, rawget = setmetatable, rawset, rawget adam@0: local next, select, pairs, type, tostring = next, select, pairs, type, tostring adam@0: adam@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded adam@0: -- List them here for Mikk's FindGlobals script adam@0: -- GLOBALS: geterrorhandler adam@0: adam@0: local xpcall = xpcall adam@0: adam@0: local function errorhandler(err) adam@0: return geterrorhandler()(err) adam@0: end adam@0: adam@0: local function CreateDispatcher(argCount) adam@0: local code = [[ adam@0: local next, xpcall, eh = ... adam@0: adam@0: local method, ARGS adam@0: local function call() method(ARGS) end adam@0: adam@0: local function dispatch(handlers, ...) adam@0: local index adam@0: index, method = next(handlers) adam@0: if not method then return end adam@0: local OLD_ARGS = ARGS adam@0: ARGS = ... adam@0: repeat adam@0: xpcall(call, eh) adam@0: index, method = next(handlers, index) adam@0: until not method adam@0: ARGS = OLD_ARGS adam@0: end adam@0: adam@0: return dispatch adam@0: ]] adam@0: adam@0: local ARGS, OLD_ARGS = {}, {} adam@0: for i = 1, argCount do ARGS[i], OLD_ARGS[i] = "arg"..i, "old_arg"..i end adam@0: code = code:gsub("OLD_ARGS", tconcat(OLD_ARGS, ", ")):gsub("ARGS", tconcat(ARGS, ", ")) adam@0: return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(next, xpcall, errorhandler) adam@0: end adam@0: adam@0: local Dispatchers = setmetatable({}, {__index=function(self, argCount) adam@0: local dispatcher = CreateDispatcher(argCount) adam@0: rawset(self, argCount, dispatcher) adam@0: return dispatcher adam@0: end}) adam@0: adam@0: -------------------------------------------------------------------------- adam@0: -- CallbackHandler:New adam@0: -- adam@0: -- target - target object to embed public APIs in adam@0: -- RegisterName - name of the callback registration API, default "RegisterCallback" adam@0: -- UnregisterName - name of the callback unregistration API, default "UnregisterCallback" adam@0: -- UnregisterAllName - name of the API to unregister all callbacks, default "UnregisterAllCallbacks". false == don't publish this API. adam@0: adam@0: function CallbackHandler:New(target, RegisterName, UnregisterName, UnregisterAllName, OnUsed, OnUnused) adam@0: -- TODO: Remove this after beta has gone out adam@0: assert(not OnUsed and not OnUnused, "ACE-80: OnUsed/OnUnused are deprecated. Callbacks are now done to registry.OnUsed and registry.OnUnused") adam@0: adam@0: RegisterName = RegisterName or "RegisterCallback" adam@0: UnregisterName = UnregisterName or "UnregisterCallback" adam@0: if UnregisterAllName==nil then -- false is used to indicate "don't want this method" adam@0: UnregisterAllName = "UnregisterAllCallbacks" adam@0: end adam@0: adam@0: -- we declare all objects and exported APIs inside this closure to quickly gain access adam@0: -- to e.g. function names, the "target" parameter, etc adam@0: adam@0: adam@0: -- Create the registry object adam@0: local events = setmetatable({}, meta) adam@0: local registry = { recurse=0, events=events } adam@0: adam@0: -- registry:Fire() - fires the given event/message into the registry adam@0: function registry:Fire(eventname, ...) adam@0: if not rawget(events, eventname) or not next(events[eventname]) then return end adam@0: local oldrecurse = registry.recurse adam@0: registry.recurse = oldrecurse + 1 adam@0: adam@0: Dispatchers[select('#', ...) + 1](events[eventname], eventname, ...) adam@0: adam@0: registry.recurse = oldrecurse adam@0: adam@0: if registry.insertQueue and oldrecurse==0 then adam@0: -- Something in one of our callbacks wanted to register more callbacks; they got queued adam@0: for eventname,callbacks in pairs(registry.insertQueue) do adam@0: local first = not rawget(events, eventname) or not next(events[eventname]) -- test for empty before. not test for one member after. that one member may have been overwritten. adam@0: for self,func in pairs(callbacks) do adam@0: events[eventname][self] = func adam@0: -- fire OnUsed callback? adam@0: if first and registry.OnUsed then adam@0: registry.OnUsed(registry, target, eventname) adam@0: first = nil adam@0: end adam@0: end adam@0: end adam@0: registry.insertQueue = nil adam@0: end adam@0: end adam@0: adam@0: -- Registration of a callback, handles: adam@0: -- self["method"], leads to self["method"](self, ...) adam@0: -- self with function ref, leads to functionref(...) adam@0: -- "addonId" (instead of self) with function ref, leads to functionref(...) adam@0: -- all with an optional arg, which, if present, gets passed as first argument (after self if present) adam@0: target[RegisterName] = function(self, eventname, method, ... --[[actually just a single arg]]) adam@0: if type(eventname) ~= "string" then adam@0: error("Usage: "..RegisterName.."(eventname, method[, arg]): 'eventname' - string expected.", 2) adam@0: end adam@0: adam@0: method = method or eventname adam@0: adam@0: local first = not rawget(events, eventname) or not next(events[eventname]) -- test for empty before. not test for one member after. that one member may have been overwritten. adam@0: adam@0: if type(method) ~= "string" and type(method) ~= "function" then adam@0: error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - string or function expected.", 2) adam@0: end adam@0: adam@0: local regfunc adam@0: adam@0: if type(method) == "string" then adam@0: -- self["method"] calling style adam@0: if type(self) ~= "table" then adam@0: error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): self was not a table?", 2) adam@0: elseif self==target then adam@0: error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): do not use Library:"..RegisterName.."(), use your own 'self'", 2) adam@0: elseif type(self[method]) ~= "function" then adam@0: error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - method '"..tostring(method).."' not found on self.", 2) adam@0: end adam@0: adam@0: if select("#",...)>=1 then -- this is not the same as testing for arg==nil! adam@0: local arg=select(1,...) adam@0: regfunc = function(...) self[method](self,arg,...) end adam@0: else adam@0: regfunc = function(...) self[method](self,...) end adam@0: end adam@0: else adam@0: -- function ref with self=object or self="addonId" or self=thread adam@0: if type(self)~="table" and type(self)~="string" and type(self)~="thread" then adam@0: error("Usage: "..RegisterName.."(self or \"addonId\", eventname, method): 'self or addonId': table or string or thread expected.", 2) adam@0: end adam@0: adam@0: if select("#",...)>=1 then -- this is not the same as testing for arg==nil! adam@0: local arg=select(1,...) adam@0: regfunc = function(...) method(arg,...) end adam@0: else adam@0: regfunc = method adam@0: end adam@0: end adam@0: adam@0: adam@0: if events[eventname][self] or registry.recurse<1 then adam@0: -- if registry.recurse<1 then adam@0: -- we're overwriting an existing entry, or not currently recursing. just set it. adam@0: events[eventname][self] = regfunc adam@0: -- fire OnUsed callback? adam@0: if registry.OnUsed and first then adam@0: registry.OnUsed(registry, target, eventname) adam@0: end adam@0: else adam@0: -- we're currently processing a callback in this registry, so delay the registration of this new entry! adam@0: -- yes, we're a bit wasteful on garbage, but this is a fringe case, so we're picking low implementation overhead over garbage efficiency adam@0: registry.insertQueue = registry.insertQueue or setmetatable({},meta) adam@0: registry.insertQueue[eventname][self] = regfunc adam@0: end adam@0: end adam@0: adam@0: -- Unregister a callback adam@0: target[UnregisterName] = function(self, eventname) adam@0: if not self or self==target then adam@0: error("Usage: "..UnregisterName.."(eventname): bad 'self'", 2) adam@0: end adam@0: if type(eventname) ~= "string" then adam@0: error("Usage: "..UnregisterName.."(eventname): 'eventname' - string expected.", 2) adam@0: end adam@0: if rawget(events, eventname) and events[eventname][self] then adam@0: events[eventname][self] = nil adam@0: -- Fire OnUnused callback? adam@0: if registry.OnUnused and not next(events[eventname]) then adam@0: registry.OnUnused(registry, target, eventname) adam@0: end adam@0: end adam@0: if registry.insertQueue and rawget(registry.insertQueue, eventname) and registry.insertQueue[eventname][self] then adam@0: registry.insertQueue[eventname][self] = nil adam@0: end adam@0: end adam@0: adam@0: -- OPTIONAL: Unregister all callbacks for given selfs/addonIds adam@0: if UnregisterAllName then adam@0: target[UnregisterAllName] = function(...) adam@0: if select("#",...)<1 then adam@0: error("Usage: "..UnregisterAllName.."([whatFor]): missing 'self' or \"addonId\" to unregister events for.", 2) adam@0: end adam@0: if select("#",...)==1 and ...==target then adam@0: error("Usage: "..UnregisterAllName.."([whatFor]): supply a meaningful 'self' or \"addonId\"", 2) adam@0: end adam@0: adam@0: adam@0: for i=1,select("#",...) do adam@0: local self = select(i,...) adam@0: if registry.insertQueue then adam@0: for eventname, callbacks in pairs(registry.insertQueue) do adam@0: if callbacks[self] then adam@0: callbacks[self] = nil adam@0: end adam@0: end adam@0: end adam@0: for eventname, callbacks in pairs(events) do adam@0: if callbacks[self] then adam@0: callbacks[self] = nil adam@0: -- Fire OnUnused callback? adam@0: if registry.OnUnused and not next(callbacks) then adam@0: registry.OnUnused(registry, target, eventname) adam@0: end adam@0: end adam@0: end adam@0: end adam@0: end adam@0: end adam@0: adam@0: return registry adam@0: end adam@0: adam@0: adam@0: -- CallbackHandler purposefully does NOT do explicit embedding. Nor does it adam@0: -- try to upgrade old implicit embeds since the system is selfcontained and adam@0: -- relies on closures to work. adam@0: