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