annotate Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 11:371e14cd2feb

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