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