annotate Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 23:52973d00a183

- framework update.
author Tercio
date Tue, 08 Sep 2015 13:23:31 -0300
parents 1c3534391efb
children dbf04157d63e
rev   line source
Tercio@4 1 --[[ $Id: CallbackHandler-1.0.lua 965 2010-08-09 00:47:52Z mikk $ ]]
Tercio@4 2 local MAJOR, MINOR = "CallbackHandler-1.0", 6
Tercio@4 3 local CallbackHandler = LibStub:NewLibrary(MAJOR, MINOR)
Tercio@4 4
Tercio@4 5 if not CallbackHandler then return end -- No upgrade needed
Tercio@4 6
Tercio@4 7 local meta = {__index = function(tbl, key) tbl[key] = {} return tbl[key] end}
Tercio@4 8
Tercio@4 9 -- Lua APIs
Tercio@4 10 local tconcat = table.concat
Tercio@4 11 local assert, error, loadstring = assert, error, loadstring
Tercio@4 12 local setmetatable, rawset, rawget = setmetatable, rawset, rawget
Tercio@4 13 local next, select, pairs, type, tostring = next, select, pairs, type, tostring
Tercio@4 14
Tercio@4 15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
Tercio@4 16 -- List them here for Mikk's FindGlobals script
Tercio@4 17 -- GLOBALS: geterrorhandler
Tercio@4 18
Tercio@4 19 local xpcall = xpcall
Tercio@4 20
Tercio@4 21 local function errorhandler(err)
Tercio@4 22 return geterrorhandler()(err)
Tercio@4 23 end
Tercio@4 24
Tercio@4 25 local function CreateDispatcher(argCount)
Tercio@4 26 local code = [[
Tercio@4 27 local next, xpcall, eh = ...
Tercio@4 28
Tercio@4 29 local method, ARGS
Tercio@4 30 local function call() method(ARGS) end
Tercio@4 31
Tercio@4 32 local function dispatch(handlers, ...)
Tercio@4 33 local index
Tercio@4 34 index, method = next(handlers)
Tercio@4 35 if not method then return end
Tercio@4 36 local OLD_ARGS = ARGS
Tercio@4 37 ARGS = ...
Tercio@4 38 repeat
Tercio@4 39 xpcall(call, eh)
Tercio@4 40 index, method = next(handlers, index)
Tercio@4 41 until not method
Tercio@4 42 ARGS = OLD_ARGS
Tercio@4 43 end
Tercio@4 44
Tercio@4 45 return dispatch
Tercio@4 46 ]]
Tercio@4 47
Tercio@4 48 local ARGS, OLD_ARGS = {}, {}
Tercio@4 49 for i = 1, argCount do ARGS[i], OLD_ARGS[i] = "arg"..i, "old_arg"..i end
Tercio@4 50 code = code:gsub("OLD_ARGS", tconcat(OLD_ARGS, ", ")):gsub("ARGS", tconcat(ARGS, ", "))
Tercio@4 51 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(next, xpcall, errorhandler)
Tercio@4 52 end
Tercio@4 53
Tercio@4 54 local Dispatchers = setmetatable({}, {__index=function(self, argCount)
Tercio@4 55 local dispatcher = CreateDispatcher(argCount)
Tercio@4 56 rawset(self, argCount, dispatcher)
Tercio@4 57 return dispatcher
Tercio@4 58 end})
Tercio@4 59
Tercio@4 60 --------------------------------------------------------------------------
Tercio@4 61 -- CallbackHandler:New
Tercio@4 62 --
Tercio@4 63 -- target - target object to embed public APIs in
Tercio@4 64 -- RegisterName - name of the callback registration API, default "RegisterCallback"
Tercio@4 65 -- UnregisterName - name of the callback unregistration API, default "UnregisterCallback"
Tercio@4 66 -- UnregisterAllName - name of the API to unregister all callbacks, default "UnregisterAllCallbacks". false == don't publish this API.
Tercio@4 67
Tercio@4 68 function CallbackHandler:New(target, RegisterName, UnregisterName, UnregisterAllName, OnUsed, OnUnused)
Tercio@4 69 -- TODO: Remove this after beta has gone out
Tercio@4 70 assert(not OnUsed and not OnUnused, "ACE-80: OnUsed/OnUnused are deprecated. Callbacks are now done to registry.OnUsed and registry.OnUnused")
Tercio@4 71
Tercio@4 72 RegisterName = RegisterName or "RegisterCallback"
Tercio@4 73 UnregisterName = UnregisterName or "UnregisterCallback"
Tercio@4 74 if UnregisterAllName==nil then -- false is used to indicate "don't want this method"
Tercio@4 75 UnregisterAllName = "UnregisterAllCallbacks"
Tercio@4 76 end
Tercio@4 77
Tercio@4 78 -- we declare all objects and exported APIs inside this closure to quickly gain access
Tercio@4 79 -- to e.g. function names, the "target" parameter, etc
Tercio@4 80
Tercio@4 81
Tercio@4 82 -- Create the registry object
Tercio@4 83 local events = setmetatable({}, meta)
Tercio@4 84 local registry = { recurse=0, events=events }
Tercio@4 85
Tercio@4 86 -- registry:Fire() - fires the given event/message into the registry
Tercio@4 87 function registry:Fire(eventname, ...)
Tercio@4 88 if not rawget(events, eventname) or not next(events[eventname]) then return end
Tercio@4 89 local oldrecurse = registry.recurse
Tercio@4 90 registry.recurse = oldrecurse + 1
Tercio@4 91
Tercio@4 92 Dispatchers[select('#', ...) + 1](events[eventname], eventname, ...)
Tercio@4 93
Tercio@4 94 registry.recurse = oldrecurse
Tercio@4 95
Tercio@4 96 if registry.insertQueue and oldrecurse==0 then
Tercio@4 97 -- Something in one of our callbacks wanted to register more callbacks; they got queued
Tercio@4 98 for eventname,callbacks in pairs(registry.insertQueue) do
Tercio@4 99 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@4 100 for self,func in pairs(callbacks) do
Tercio@4 101 events[eventname][self] = func
Tercio@4 102 -- fire OnUsed callback?
Tercio@4 103 if first and registry.OnUsed then
Tercio@4 104 registry.OnUsed(registry, target, eventname)
Tercio@4 105 first = nil
Tercio@4 106 end
Tercio@4 107 end
Tercio@4 108 end
Tercio@4 109 registry.insertQueue = nil
Tercio@4 110 end
Tercio@4 111 end
Tercio@4 112
Tercio@4 113 -- Registration of a callback, handles:
Tercio@4 114 -- self["method"], leads to self["method"](self, ...)
Tercio@4 115 -- self with function ref, leads to functionref(...)
Tercio@4 116 -- "addonId" (instead of self) with function ref, leads to functionref(...)
Tercio@4 117 -- all with an optional arg, which, if present, gets passed as first argument (after self if present)
Tercio@4 118 target[RegisterName] = function(self, eventname, method, ... --[[actually just a single arg]])
Tercio@4 119 if type(eventname) ~= "string" then
Tercio@4 120 error("Usage: "..RegisterName.."(eventname, method[, arg]): 'eventname' - string expected.", 2)
Tercio@4 121 end
Tercio@4 122
Tercio@4 123 method = method or eventname
Tercio@4 124
Tercio@4 125 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@4 126
Tercio@4 127 if type(method) ~= "string" and type(method) ~= "function" then
Tercio@4 128 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - string or function expected.", 2)
Tercio@4 129 end
Tercio@4 130
Tercio@4 131 local regfunc
Tercio@4 132
Tercio@4 133 if type(method) == "string" then
Tercio@4 134 -- self["method"] calling style
Tercio@4 135 if type(self) ~= "table" then
Tercio@4 136 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): self was not a table?", 2)
Tercio@4 137 elseif self==target then
Tercio@4 138 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): do not use Library:"..RegisterName.."(), use your own 'self'", 2)
Tercio@4 139 elseif type(self[method]) ~= "function" then
Tercio@4 140 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - method '"..tostring(method).."' not found on self.", 2)
Tercio@4 141 end
Tercio@4 142
Tercio@4 143 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
Tercio@4 144 local arg=select(1,...)
Tercio@4 145 regfunc = function(...) self[method](self,arg,...) end
Tercio@4 146 else
Tercio@4 147 regfunc = function(...) self[method](self,...) end
Tercio@4 148 end
Tercio@4 149 else
Tercio@4 150 -- function ref with self=object or self="addonId" or self=thread
Tercio@4 151 if type(self)~="table" and type(self)~="string" and type(self)~="thread" then
Tercio@4 152 error("Usage: "..RegisterName.."(self or \"addonId\", eventname, method): 'self or addonId': table or string or thread expected.", 2)
Tercio@4 153 end
Tercio@4 154
Tercio@4 155 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
Tercio@4 156 local arg=select(1,...)
Tercio@4 157 regfunc = function(...) method(arg,...) end
Tercio@4 158 else
Tercio@4 159 regfunc = method
Tercio@4 160 end
Tercio@4 161 end
Tercio@4 162
Tercio@4 163
Tercio@4 164 if events[eventname][self] or registry.recurse<1 then
Tercio@4 165 -- if registry.recurse<1 then
Tercio@4 166 -- we're overwriting an existing entry, or not currently recursing. just set it.
Tercio@4 167 events[eventname][self] = regfunc
Tercio@4 168 -- fire OnUsed callback?
Tercio@4 169 if registry.OnUsed and first then
Tercio@4 170 registry.OnUsed(registry, target, eventname)
Tercio@4 171 end
Tercio@4 172 else
Tercio@4 173 -- we're currently processing a callback in this registry, so delay the registration of this new entry!
Tercio@4 174 -- 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@4 175 registry.insertQueue = registry.insertQueue or setmetatable({},meta)
Tercio@4 176 registry.insertQueue[eventname][self] = regfunc
Tercio@4 177 end
Tercio@4 178 end
Tercio@4 179
Tercio@4 180 -- Unregister a callback
Tercio@4 181 target[UnregisterName] = function(self, eventname)
Tercio@4 182 if not self or self==target then
Tercio@4 183 error("Usage: "..UnregisterName.."(eventname): bad 'self'", 2)
Tercio@4 184 end
Tercio@4 185 if type(eventname) ~= "string" then
Tercio@4 186 error("Usage: "..UnregisterName.."(eventname): 'eventname' - string expected.", 2)
Tercio@4 187 end
Tercio@4 188 if rawget(events, eventname) and events[eventname][self] then
Tercio@4 189 events[eventname][self] = nil
Tercio@4 190 -- Fire OnUnused callback?
Tercio@4 191 if registry.OnUnused and not next(events[eventname]) then
Tercio@4 192 registry.OnUnused(registry, target, eventname)
Tercio@4 193 end
Tercio@4 194 end
Tercio@4 195 if registry.insertQueue and rawget(registry.insertQueue, eventname) and registry.insertQueue[eventname][self] then
Tercio@4 196 registry.insertQueue[eventname][self] = nil
Tercio@4 197 end
Tercio@4 198 end
Tercio@4 199
Tercio@4 200 -- OPTIONAL: Unregister all callbacks for given selfs/addonIds
Tercio@4 201 if UnregisterAllName then
Tercio@4 202 target[UnregisterAllName] = function(...)
Tercio@4 203 if select("#",...)<1 then
Tercio@4 204 error("Usage: "..UnregisterAllName.."([whatFor]): missing 'self' or \"addonId\" to unregister events for.", 2)
Tercio@4 205 end
Tercio@4 206 if select("#",...)==1 and ...==target then
Tercio@4 207 error("Usage: "..UnregisterAllName.."([whatFor]): supply a meaningful 'self' or \"addonId\"", 2)
Tercio@4 208 end
Tercio@4 209
Tercio@4 210
Tercio@4 211 for i=1,select("#",...) do
Tercio@4 212 local self = select(i,...)
Tercio@4 213 if registry.insertQueue then
Tercio@4 214 for eventname, callbacks in pairs(registry.insertQueue) do
Tercio@4 215 if callbacks[self] then
Tercio@4 216 callbacks[self] = nil
Tercio@4 217 end
Tercio@4 218 end
Tercio@4 219 end
Tercio@4 220 for eventname, callbacks in pairs(events) do
Tercio@4 221 if callbacks[self] then
Tercio@4 222 callbacks[self] = nil
Tercio@4 223 -- Fire OnUnused callback?
Tercio@4 224 if registry.OnUnused and not next(callbacks) then
Tercio@4 225 registry.OnUnused(registry, target, eventname)
Tercio@4 226 end
Tercio@4 227 end
Tercio@4 228 end
Tercio@4 229 end
Tercio@4 230 end
Tercio@4 231 end
Tercio@4 232
Tercio@4 233 return registry
Tercio@4 234 end
Tercio@4 235
Tercio@4 236
Tercio@4 237 -- CallbackHandler purposefully does NOT do explicit embedding. Nor does it
Tercio@4 238 -- try to upgrade old implicit embeds since the system is selfcontained and
Tercio@4 239 -- relies on closures to work.
Tercio@4 240