annotate Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 51:dbf04157d63e v7.3.0.051

- ToC Update.
author Tercio
date Sat, 02 Sep 2017 12:42:52 -0300
parents 1c3534391efb
children
rev   line source
Tercio@51 1 --[[ $Id: CallbackHandler-1.0.lua 1131 2015-06-04 07:29:24Z nevcairiel $ ]]
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@51 68 function CallbackHandler:New(target, RegisterName, UnregisterName, UnregisterAllName)
Tercio@4 69
Tercio@4 70 RegisterName = RegisterName or "RegisterCallback"
Tercio@4 71 UnregisterName = UnregisterName or "UnregisterCallback"
Tercio@4 72 if UnregisterAllName==nil then -- false is used to indicate "don't want this method"
Tercio@4 73 UnregisterAllName = "UnregisterAllCallbacks"
Tercio@4 74 end
Tercio@4 75
Tercio@4 76 -- we declare all objects and exported APIs inside this closure to quickly gain access
Tercio@4 77 -- to e.g. function names, the "target" parameter, etc
Tercio@4 78
Tercio@4 79
Tercio@4 80 -- Create the registry object
Tercio@4 81 local events = setmetatable({}, meta)
Tercio@4 82 local registry = { recurse=0, events=events }
Tercio@4 83
Tercio@4 84 -- registry:Fire() - fires the given event/message into the registry
Tercio@4 85 function registry:Fire(eventname, ...)
Tercio@4 86 if not rawget(events, eventname) or not next(events[eventname]) then return end
Tercio@4 87 local oldrecurse = registry.recurse
Tercio@4 88 registry.recurse = oldrecurse + 1
Tercio@4 89
Tercio@4 90 Dispatchers[select('#', ...) + 1](events[eventname], eventname, ...)
Tercio@4 91
Tercio@4 92 registry.recurse = oldrecurse
Tercio@4 93
Tercio@4 94 if registry.insertQueue and oldrecurse==0 then
Tercio@4 95 -- Something in one of our callbacks wanted to register more callbacks; they got queued
Tercio@4 96 for eventname,callbacks in pairs(registry.insertQueue) do
Tercio@4 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@4 98 for self,func in pairs(callbacks) do
Tercio@4 99 events[eventname][self] = func
Tercio@4 100 -- fire OnUsed callback?
Tercio@4 101 if first and registry.OnUsed then
Tercio@4 102 registry.OnUsed(registry, target, eventname)
Tercio@4 103 first = nil
Tercio@4 104 end
Tercio@4 105 end
Tercio@4 106 end
Tercio@4 107 registry.insertQueue = nil
Tercio@4 108 end
Tercio@4 109 end
Tercio@4 110
Tercio@4 111 -- Registration of a callback, handles:
Tercio@4 112 -- self["method"], leads to self["method"](self, ...)
Tercio@4 113 -- self with function ref, leads to functionref(...)
Tercio@4 114 -- "addonId" (instead of self) with function ref, leads to functionref(...)
Tercio@4 115 -- all with an optional arg, which, if present, gets passed as first argument (after self if present)
Tercio@4 116 target[RegisterName] = function(self, eventname, method, ... --[[actually just a single arg]])
Tercio@4 117 if type(eventname) ~= "string" then
Tercio@4 118 error("Usage: "..RegisterName.."(eventname, method[, arg]): 'eventname' - string expected.", 2)
Tercio@4 119 end
Tercio@4 120
Tercio@4 121 method = method or eventname
Tercio@4 122
Tercio@4 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@4 124
Tercio@4 125 if type(method) ~= "string" and type(method) ~= "function" then
Tercio@4 126 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - string or function expected.", 2)
Tercio@4 127 end
Tercio@4 128
Tercio@4 129 local regfunc
Tercio@4 130
Tercio@4 131 if type(method) == "string" then
Tercio@4 132 -- self["method"] calling style
Tercio@4 133 if type(self) ~= "table" then
Tercio@4 134 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): self was not a table?", 2)
Tercio@4 135 elseif self==target then
Tercio@4 136 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): do not use Library:"..RegisterName.."(), use your own 'self'", 2)
Tercio@4 137 elseif type(self[method]) ~= "function" then
Tercio@4 138 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - method '"..tostring(method).."' not found on self.", 2)
Tercio@4 139 end
Tercio@4 140
Tercio@4 141 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
Tercio@4 142 local arg=select(1,...)
Tercio@4 143 regfunc = function(...) self[method](self,arg,...) end
Tercio@4 144 else
Tercio@4 145 regfunc = function(...) self[method](self,...) end
Tercio@4 146 end
Tercio@4 147 else
Tercio@4 148 -- function ref with self=object or self="addonId" or self=thread
Tercio@4 149 if type(self)~="table" and type(self)~="string" and type(self)~="thread" then
Tercio@4 150 error("Usage: "..RegisterName.."(self or \"addonId\", eventname, method): 'self or addonId': table or string or thread expected.", 2)
Tercio@4 151 end
Tercio@4 152
Tercio@4 153 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
Tercio@4 154 local arg=select(1,...)
Tercio@4 155 regfunc = function(...) method(arg,...) end
Tercio@4 156 else
Tercio@4 157 regfunc = method
Tercio@4 158 end
Tercio@4 159 end
Tercio@4 160
Tercio@4 161
Tercio@4 162 if events[eventname][self] or registry.recurse<1 then
Tercio@4 163 -- if registry.recurse<1 then
Tercio@4 164 -- we're overwriting an existing entry, or not currently recursing. just set it.
Tercio@4 165 events[eventname][self] = regfunc
Tercio@4 166 -- fire OnUsed callback?
Tercio@4 167 if registry.OnUsed and first then
Tercio@4 168 registry.OnUsed(registry, target, eventname)
Tercio@4 169 end
Tercio@4 170 else
Tercio@4 171 -- we're currently processing a callback in this registry, so delay the registration of this new entry!
Tercio@4 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@4 173 registry.insertQueue = registry.insertQueue or setmetatable({},meta)
Tercio@4 174 registry.insertQueue[eventname][self] = regfunc
Tercio@4 175 end
Tercio@4 176 end
Tercio@4 177
Tercio@4 178 -- Unregister a callback
Tercio@4 179 target[UnregisterName] = function(self, eventname)
Tercio@4 180 if not self or self==target then
Tercio@4 181 error("Usage: "..UnregisterName.."(eventname): bad 'self'", 2)
Tercio@4 182 end
Tercio@4 183 if type(eventname) ~= "string" then
Tercio@4 184 error("Usage: "..UnregisterName.."(eventname): 'eventname' - string expected.", 2)
Tercio@4 185 end
Tercio@4 186 if rawget(events, eventname) and events[eventname][self] then
Tercio@4 187 events[eventname][self] = nil
Tercio@4 188 -- Fire OnUnused callback?
Tercio@4 189 if registry.OnUnused and not next(events[eventname]) then
Tercio@4 190 registry.OnUnused(registry, target, eventname)
Tercio@4 191 end
Tercio@4 192 end
Tercio@4 193 if registry.insertQueue and rawget(registry.insertQueue, eventname) and registry.insertQueue[eventname][self] then
Tercio@4 194 registry.insertQueue[eventname][self] = nil
Tercio@4 195 end
Tercio@4 196 end
Tercio@4 197
Tercio@4 198 -- OPTIONAL: Unregister all callbacks for given selfs/addonIds
Tercio@4 199 if UnregisterAllName then
Tercio@4 200 target[UnregisterAllName] = function(...)
Tercio@4 201 if select("#",...)<1 then
Tercio@4 202 error("Usage: "..UnregisterAllName.."([whatFor]): missing 'self' or \"addonId\" to unregister events for.", 2)
Tercio@4 203 end
Tercio@4 204 if select("#",...)==1 and ...==target then
Tercio@4 205 error("Usage: "..UnregisterAllName.."([whatFor]): supply a meaningful 'self' or \"addonId\"", 2)
Tercio@4 206 end
Tercio@4 207
Tercio@4 208
Tercio@4 209 for i=1,select("#",...) do
Tercio@4 210 local self = select(i,...)
Tercio@4 211 if registry.insertQueue then
Tercio@4 212 for eventname, callbacks in pairs(registry.insertQueue) do
Tercio@4 213 if callbacks[self] then
Tercio@4 214 callbacks[self] = nil
Tercio@4 215 end
Tercio@4 216 end
Tercio@4 217 end
Tercio@4 218 for eventname, callbacks in pairs(events) do
Tercio@4 219 if callbacks[self] then
Tercio@4 220 callbacks[self] = nil
Tercio@4 221 -- Fire OnUnused callback?
Tercio@4 222 if registry.OnUnused and not next(callbacks) then
Tercio@4 223 registry.OnUnused(registry, target, eventname)
Tercio@4 224 end
Tercio@4 225 end
Tercio@4 226 end
Tercio@4 227 end
Tercio@4 228 end
Tercio@4 229 end
Tercio@4 230
Tercio@4 231 return registry
Tercio@4 232 end
Tercio@4 233
Tercio@4 234
Tercio@4 235 -- CallbackHandler purposefully does NOT do explicit embedding. Nor does it
Tercio@4 236 -- try to upgrade old implicit embeds since the system is selfcontained and
Tercio@4 237 -- relies on closures to work.
Tercio@4 238