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