annotate Libs/CallbackHandler-1.0/CallbackHandler-1.0.lua @ 25:ac501d71c890 tip

Added tag v8.2.0.024 for changeset 389dcaeebc47
author Tercioo
date Fri, 28 Jun 2019 20:07:27 -0300
parents 3596dadf9a90
children
rev   line source
Tercioo@22 1 --[[ $Id: CallbackHandler-1.0.lua 22 2018-07-21 14:17:22Z nevcairiel $ ]]
Tercioo@22 2 local MAJOR, MINOR = "CallbackHandler-1.0", 7
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
Tercioo@22 25 local function Dispatch(handlers, ...)
Tercioo@22 26 local index, method = next(handlers)
Tercioo@22 27 if not method then return end
Tercioo@22 28 repeat
Tercioo@22 29 xpcall(method, errorhandler, ...)
Tercioo@22 30 index, method = next(handlers, index)
Tercioo@22 31 until not method
tercio@0 32 end
tercio@0 33
tercio@0 34 --------------------------------------------------------------------------
tercio@0 35 -- CallbackHandler:New
tercio@0 36 --
tercio@0 37 -- target - target object to embed public APIs in
tercio@0 38 -- RegisterName - name of the callback registration API, default "RegisterCallback"
tercio@0 39 -- UnregisterName - name of the callback unregistration API, default "UnregisterCallback"
tercio@0 40 -- UnregisterAllName - name of the API to unregister all callbacks, default "UnregisterAllCallbacks". false == don't publish this API.
tercio@0 41
Tercio@11 42 function CallbackHandler:New(target, RegisterName, UnregisterName, UnregisterAllName)
tercio@0 43
tercio@0 44 RegisterName = RegisterName or "RegisterCallback"
tercio@0 45 UnregisterName = UnregisterName or "UnregisterCallback"
tercio@0 46 if UnregisterAllName==nil then -- false is used to indicate "don't want this method"
tercio@0 47 UnregisterAllName = "UnregisterAllCallbacks"
tercio@0 48 end
tercio@0 49
tercio@0 50 -- we declare all objects and exported APIs inside this closure to quickly gain access
tercio@0 51 -- to e.g. function names, the "target" parameter, etc
tercio@0 52
tercio@0 53
tercio@0 54 -- Create the registry object
tercio@0 55 local events = setmetatable({}, meta)
tercio@0 56 local registry = { recurse=0, events=events }
tercio@0 57
tercio@0 58 -- registry:Fire() - fires the given event/message into the registry
tercio@0 59 function registry:Fire(eventname, ...)
tercio@0 60 if not rawget(events, eventname) or not next(events[eventname]) then return end
tercio@0 61 local oldrecurse = registry.recurse
tercio@0 62 registry.recurse = oldrecurse + 1
tercio@0 63
Tercioo@22 64 Dispatch(events[eventname], eventname, ...)
tercio@0 65
tercio@0 66 registry.recurse = oldrecurse
tercio@0 67
tercio@0 68 if registry.insertQueue and oldrecurse==0 then
tercio@0 69 -- Something in one of our callbacks wanted to register more callbacks; they got queued
tercio@0 70 for eventname,callbacks in pairs(registry.insertQueue) do
tercio@0 71 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 72 for self,func in pairs(callbacks) do
tercio@0 73 events[eventname][self] = func
tercio@0 74 -- fire OnUsed callback?
tercio@0 75 if first and registry.OnUsed then
tercio@0 76 registry.OnUsed(registry, target, eventname)
tercio@0 77 first = nil
tercio@0 78 end
tercio@0 79 end
tercio@0 80 end
tercio@0 81 registry.insertQueue = nil
tercio@0 82 end
tercio@0 83 end
tercio@0 84
tercio@0 85 -- Registration of a callback, handles:
tercio@0 86 -- self["method"], leads to self["method"](self, ...)
tercio@0 87 -- self with function ref, leads to functionref(...)
tercio@0 88 -- "addonId" (instead of self) with function ref, leads to functionref(...)
tercio@0 89 -- all with an optional arg, which, if present, gets passed as first argument (after self if present)
tercio@0 90 target[RegisterName] = function(self, eventname, method, ... --[[actually just a single arg]])
tercio@0 91 if type(eventname) ~= "string" then
tercio@0 92 error("Usage: "..RegisterName.."(eventname, method[, arg]): 'eventname' - string expected.", 2)
tercio@0 93 end
tercio@0 94
tercio@0 95 method = method or eventname
tercio@0 96
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
tercio@0 99 if type(method) ~= "string" and type(method) ~= "function" then
tercio@0 100 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - string or function expected.", 2)
tercio@0 101 end
tercio@0 102
tercio@0 103 local regfunc
tercio@0 104
tercio@0 105 if type(method) == "string" then
tercio@0 106 -- self["method"] calling style
tercio@0 107 if type(self) ~= "table" then
tercio@0 108 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): self was not a table?", 2)
tercio@0 109 elseif self==target then
tercio@0 110 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): do not use Library:"..RegisterName.."(), use your own 'self'", 2)
tercio@0 111 elseif type(self[method]) ~= "function" then
tercio@0 112 error("Usage: "..RegisterName.."(\"eventname\", \"methodname\"): 'methodname' - method '"..tostring(method).."' not found on self.", 2)
tercio@0 113 end
tercio@0 114
tercio@0 115 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
tercio@0 116 local arg=select(1,...)
tercio@0 117 regfunc = function(...) self[method](self,arg,...) end
tercio@0 118 else
tercio@0 119 regfunc = function(...) self[method](self,...) end
tercio@0 120 end
tercio@0 121 else
tercio@0 122 -- function ref with self=object or self="addonId" or self=thread
tercio@0 123 if type(self)~="table" and type(self)~="string" and type(self)~="thread" then
tercio@0 124 error("Usage: "..RegisterName.."(self or \"addonId\", eventname, method): 'self or addonId': table or string or thread expected.", 2)
tercio@0 125 end
tercio@0 126
tercio@0 127 if select("#",...)>=1 then -- this is not the same as testing for arg==nil!
tercio@0 128 local arg=select(1,...)
tercio@0 129 regfunc = function(...) method(arg,...) end
tercio@0 130 else
tercio@0 131 regfunc = method
tercio@0 132 end
tercio@0 133 end
tercio@0 134
tercio@0 135
tercio@0 136 if events[eventname][self] or registry.recurse<1 then
tercio@0 137 -- if registry.recurse<1 then
tercio@0 138 -- we're overwriting an existing entry, or not currently recursing. just set it.
tercio@0 139 events[eventname][self] = regfunc
tercio@0 140 -- fire OnUsed callback?
tercio@0 141 if registry.OnUsed and first then
tercio@0 142 registry.OnUsed(registry, target, eventname)
tercio@0 143 end
tercio@0 144 else
tercio@0 145 -- we're currently processing a callback in this registry, so delay the registration of this new entry!
tercio@0 146 -- 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 147 registry.insertQueue = registry.insertQueue or setmetatable({},meta)
tercio@0 148 registry.insertQueue[eventname][self] = regfunc
tercio@0 149 end
tercio@0 150 end
tercio@0 151
tercio@0 152 -- Unregister a callback
tercio@0 153 target[UnregisterName] = function(self, eventname)
tercio@0 154 if not self or self==target then
tercio@0 155 error("Usage: "..UnregisterName.."(eventname): bad 'self'", 2)
tercio@0 156 end
tercio@0 157 if type(eventname) ~= "string" then
tercio@0 158 error("Usage: "..UnregisterName.."(eventname): 'eventname' - string expected.", 2)
tercio@0 159 end
tercio@0 160 if rawget(events, eventname) and events[eventname][self] then
tercio@0 161 events[eventname][self] = nil
tercio@0 162 -- Fire OnUnused callback?
tercio@0 163 if registry.OnUnused and not next(events[eventname]) then
tercio@0 164 registry.OnUnused(registry, target, eventname)
tercio@0 165 end
tercio@0 166 end
tercio@0 167 if registry.insertQueue and rawget(registry.insertQueue, eventname) and registry.insertQueue[eventname][self] then
tercio@0 168 registry.insertQueue[eventname][self] = nil
tercio@0 169 end
tercio@0 170 end
tercio@0 171
tercio@0 172 -- OPTIONAL: Unregister all callbacks for given selfs/addonIds
tercio@0 173 if UnregisterAllName then
tercio@0 174 target[UnregisterAllName] = function(...)
tercio@0 175 if select("#",...)<1 then
tercio@0 176 error("Usage: "..UnregisterAllName.."([whatFor]): missing 'self' or \"addonId\" to unregister events for.", 2)
tercio@0 177 end
tercio@0 178 if select("#",...)==1 and ...==target then
tercio@0 179 error("Usage: "..UnregisterAllName.."([whatFor]): supply a meaningful 'self' or \"addonId\"", 2)
tercio@0 180 end
tercio@0 181
tercio@0 182
tercio@0 183 for i=1,select("#",...) do
tercio@0 184 local self = select(i,...)
tercio@0 185 if registry.insertQueue then
tercio@0 186 for eventname, callbacks in pairs(registry.insertQueue) do
tercio@0 187 if callbacks[self] then
tercio@0 188 callbacks[self] = nil
tercio@0 189 end
tercio@0 190 end
tercio@0 191 end
tercio@0 192 for eventname, callbacks in pairs(events) do
tercio@0 193 if callbacks[self] then
tercio@0 194 callbacks[self] = nil
tercio@0 195 -- Fire OnUnused callback?
tercio@0 196 if registry.OnUnused and not next(callbacks) then
tercio@0 197 registry.OnUnused(registry, target, eventname)
tercio@0 198 end
tercio@0 199 end
tercio@0 200 end
tercio@0 201 end
tercio@0 202 end
tercio@0 203 end
tercio@0 204
tercio@0 205 return registry
tercio@0 206 end
tercio@0 207
tercio@0 208
tercio@0 209 -- CallbackHandler purposefully does NOT do explicit embedding. Nor does it
tercio@0 210 -- try to upgrade old implicit embeds since the system is selfcontained and
tercio@0 211 -- relies on closures to work.
tercio@0 212