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