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