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