annotate Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 201:3447634f0388 tip

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