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