Mercurial > wow > itemauditor
comparison Libs/AceBucket-3.0/AceBucket-3.0.lua @ 0:169f5211fc7f
First public revision.
At this point ItemAuditor watches mail for auctions sold or purchased, watches for buy/sell (money and 1 item type change) and conversions/tradeskills. Milling isn't working yet because there is too much time between the first event and the last event.
author | Asa Ayers <Asa.Ayers@Gmail.com> |
---|---|
date | Thu, 20 May 2010 19:22:19 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:169f5211fc7f |
---|---|
1 --- A bucket to catch events in. **AceBucket-3.0** provides throttling of events that fire in bursts and | |
2 -- your addon only needs to know about the full burst. | |
3 -- | |
4 -- This Bucket implementation works as follows:\\ | |
5 -- Initially, no schedule is running, and its waiting for the first event to happen.\\ | |
6 -- The first event will start the bucket, and get the scheduler running, which will collect all | |
7 -- events in the given interval. When that interval is reached, the bucket is pushed to the | |
8 -- callback and a new schedule is started. When a bucket is empty after its interval, the scheduler is | |
9 -- stopped, and the bucket is only listening for the next event to happen, basically back in its initial state. | |
10 -- | |
11 -- In addition, the buckets collect information about the "arg1" argument of the events that fire, and pass those as a | |
12 -- table to your callback. This functionality was mostly designed for the UNIT_* events.\\ | |
13 -- The table will have the different values of "arg1" as keys, and the number of occurances as their value, e.g.\\ | |
14 -- { ["player"] = 2, ["target"] = 1, ["party1"] = 1 } | |
15 -- | |
16 -- **AceBucket-3.0** can be embeded into your addon, either explicitly by calling AceBucket:Embed(MyAddon) or by | |
17 -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object | |
18 -- and can be accessed directly, without having to explicitly call AceBucket itself.\\ | |
19 -- It is recommended to embed AceBucket, otherwise you'll have to specify a custom `self` on all calls you | |
20 -- make into AceBucket. | |
21 -- @usage | |
22 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("BucketExample", "AceBucket-3.0") | |
23 -- | |
24 -- function MyAddon:OnEnable() | |
25 -- -- Register a bucket that listens to all the HP related events, | |
26 -- -- and fires once per second | |
27 -- self:RegisterBucketEvent({"UNIT_HEALTH", "UNIT_MAXHEALTH"}, 1, "UpdateHealth") | |
28 -- end | |
29 -- | |
30 -- function MyAddon:UpdateHealth(units) | |
31 -- if units.player then | |
32 -- print("Your HP changed!") | |
33 -- end | |
34 -- end | |
35 -- @class file | |
36 -- @name AceBucket-3.0.lua | |
37 -- @release $Id: AceBucket-3.0.lua 895 2009-12-06 16:28:55Z nevcairiel $ | |
38 | |
39 local MAJOR, MINOR = "AceBucket-3.0", 3 | |
40 local AceBucket, oldminor = LibStub:NewLibrary(MAJOR, MINOR) | |
41 | |
42 if not AceBucket then return end -- No Upgrade needed | |
43 | |
44 AceBucket.buckets = AceBucket.buckets or {} | |
45 AceBucket.embeds = AceBucket.embeds or {} | |
46 | |
47 -- the libraries will be lazyly bound later, to avoid errors due to loading order issues | |
48 local AceEvent, AceTimer | |
49 | |
50 -- Lua APIs | |
51 local tconcat = table.concat | |
52 local type, next, pairs, select = type, next, pairs, select | |
53 local tonumber, tostring, rawset = tonumber, tostring, rawset | |
54 local assert, loadstring, error = assert, loadstring, error | |
55 | |
56 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded | |
57 -- List them here for Mikk's FindGlobals script | |
58 -- GLOBALS: LibStub, geterrorhandler | |
59 | |
60 local bucketCache = setmetatable({}, {__mode='k'}) | |
61 | |
62 --[[ | |
63 xpcall safecall implementation | |
64 ]] | |
65 local xpcall = xpcall | |
66 | |
67 local function errorhandler(err) | |
68 return geterrorhandler()(err) | |
69 end | |
70 | |
71 local function CreateDispatcher(argCount) | |
72 local code = [[ | |
73 local xpcall, eh = ... | |
74 local method, ARGS | |
75 local function call() return method(ARGS) end | |
76 | |
77 local function dispatch(func, ...) | |
78 method = func | |
79 if not method then return end | |
80 ARGS = ... | |
81 return xpcall(call, eh) | |
82 end | |
83 | |
84 return dispatch | |
85 ]] | |
86 | |
87 local ARGS = {} | |
88 for i = 1, argCount do ARGS[i] = "arg"..i end | |
89 code = code:gsub("ARGS", tconcat(ARGS, ", ")) | |
90 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler) | |
91 end | |
92 | |
93 local Dispatchers = setmetatable({}, {__index=function(self, argCount) | |
94 local dispatcher = CreateDispatcher(argCount) | |
95 rawset(self, argCount, dispatcher) | |
96 return dispatcher | |
97 end}) | |
98 Dispatchers[0] = function(func) | |
99 return xpcall(func, errorhandler) | |
100 end | |
101 | |
102 local function safecall(func, ...) | |
103 return Dispatchers[select('#', ...)](func, ...) | |
104 end | |
105 | |
106 -- FireBucket ( bucket ) | |
107 -- | |
108 -- send the bucket to the callback function and schedule the next FireBucket in interval seconds | |
109 local function FireBucket(bucket) | |
110 local received = bucket.received | |
111 | |
112 -- we dont want to fire empty buckets | |
113 if next(received) then | |
114 local callback = bucket.callback | |
115 if type(callback) == "string" then | |
116 safecall(bucket.object[callback], bucket.object, received) | |
117 else | |
118 safecall(callback, received) | |
119 end | |
120 | |
121 for k in pairs(received) do | |
122 received[k] = nil | |
123 end | |
124 | |
125 -- if the bucket was not empty, schedule another FireBucket in interval seconds | |
126 bucket.timer = AceTimer.ScheduleTimer(bucket, FireBucket, bucket.interval, bucket) | |
127 else -- if it was empty, clear the timer and wait for the next event | |
128 bucket.timer = nil | |
129 end | |
130 end | |
131 | |
132 -- BucketHandler ( event, arg1 ) | |
133 -- | |
134 -- callback func for AceEvent | |
135 -- stores arg1 in the received table, and schedules the bucket if necessary | |
136 local function BucketHandler(self, event, arg1) | |
137 if arg1 == nil then | |
138 arg1 = "nil" | |
139 end | |
140 | |
141 self.received[arg1] = (self.received[arg1] or 0) + 1 | |
142 | |
143 -- if we are not scheduled yet, start a timer on the interval for our bucket to be cleared | |
144 if not self.timer then | |
145 self.timer = AceTimer.ScheduleTimer(self, FireBucket, self.interval, self) | |
146 end | |
147 end | |
148 | |
149 -- RegisterBucket( event, interval, callback, isMessage ) | |
150 -- | |
151 -- event(string or table) - the event, or a table with the events, that this bucket listens to | |
152 -- interval(int) - time between bucket fireings | |
153 -- callback(func or string) - function pointer, or method name of the object, that gets called when the bucket is cleared | |
154 -- isMessage(boolean) - register AceEvent Messages instead of game events | |
155 local function RegisterBucket(self, event, interval, callback, isMessage) | |
156 -- try to fetch the librarys | |
157 if not AceEvent or not AceTimer then | |
158 AceEvent = LibStub:GetLibrary("AceEvent-3.0", true) | |
159 AceTimer = LibStub:GetLibrary("AceTimer-3.0", true) | |
160 if not AceEvent or not AceTimer then | |
161 error(MAJOR .. " requires AceEvent-3.0 and AceTimer-3.0", 3) | |
162 end | |
163 end | |
164 | |
165 if type(event) ~= "string" and type(event) ~= "table" then error("Usage: RegisterBucket(event, interval, callback): 'event' - string or table expected.", 3) end | |
166 if not callback then | |
167 if type(event) == "string" then | |
168 callback = event | |
169 else | |
170 error("Usage: RegisterBucket(event, interval, callback): cannot omit callback when event is not a string.", 3) | |
171 end | |
172 end | |
173 if not tonumber(interval) then error("Usage: RegisterBucket(event, interval, callback): 'interval' - number expected.", 3) end | |
174 if type(callback) ~= "string" and type(callback) ~= "function" then error("Usage: RegisterBucket(event, interval, callback): 'callback' - string or function or nil expected.", 3) end | |
175 if type(callback) == "string" and type(self[callback]) ~= "function" then error("Usage: RegisterBucket(event, interval, callback): 'callback' - method not found on target object.", 3) end | |
176 | |
177 local bucket = next(bucketCache) | |
178 if bucket then | |
179 bucketCache[bucket] = nil | |
180 else | |
181 bucket = { handler = BucketHandler, received = {} } | |
182 end | |
183 bucket.object, bucket.callback, bucket.interval = self, callback, tonumber(interval) | |
184 | |
185 local regFunc = isMessage and AceEvent.RegisterMessage or AceEvent.RegisterEvent | |
186 | |
187 if type(event) == "table" then | |
188 for _,e in pairs(event) do | |
189 regFunc(bucket, e, "handler") | |
190 end | |
191 else | |
192 regFunc(bucket, event, "handler") | |
193 end | |
194 | |
195 local handle = tostring(bucket) | |
196 AceBucket.buckets[handle] = bucket | |
197 | |
198 return handle | |
199 end | |
200 | |
201 --- Register a Bucket for an event (or a set of events) | |
202 -- @param event The event to listen for, or a table of events. | |
203 -- @param interval The Bucket interval (burst interval) | |
204 -- @param callback The callback function, either as a function reference, or a string pointing to a method of the addon object. | |
205 -- @return The handle of the bucket (for unregistering) | |
206 -- @usage | |
207 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceBucket-3.0") | |
208 -- MyAddon:RegisterBucketEvent("BAG_UPDATE", 0.2, "UpdateBags") | |
209 -- | |
210 -- function MyAddon:UpdateBags() | |
211 -- -- do stuff | |
212 -- end | |
213 function AceBucket:RegisterBucketEvent(event, interval, callback) | |
214 return RegisterBucket(self, event, interval, callback, false) | |
215 end | |
216 | |
217 --- Register a Bucket for an AceEvent-3.0 addon message (or a set of messages) | |
218 -- @param message The message to listen for, or a table of messages. | |
219 -- @param interval The Bucket interval (burst interval) | |
220 -- @param callback The callback function, either as a function reference, or a string pointing to a method of the addon object. | |
221 -- @return The handle of the bucket (for unregistering) | |
222 -- @usage | |
223 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceBucket-3.0") | |
224 -- MyAddon:RegisterBucketEvent("SomeAddon_InformationMessage", 0.2, "ProcessData") | |
225 -- | |
226 -- function MyAddon:ProcessData() | |
227 -- -- do stuff | |
228 -- end | |
229 function AceBucket:RegisterBucketMessage(message, interval, callback) | |
230 return RegisterBucket(self, message, interval, callback, true) | |
231 end | |
232 | |
233 --- Unregister any events and messages from the bucket and clear any remaining data. | |
234 -- @param handle The handle of the bucket as returned by RegisterBucket* | |
235 function AceBucket:UnregisterBucket(handle) | |
236 local bucket = AceBucket.buckets[handle] | |
237 if bucket then | |
238 AceEvent.UnregisterAllEvents(bucket) | |
239 AceEvent.UnregisterAllMessages(bucket) | |
240 | |
241 -- clear any remaining data in the bucket | |
242 for k in pairs(bucket.received) do | |
243 bucket.received[k] = nil | |
244 end | |
245 | |
246 if bucket.timer then | |
247 AceTimer.CancelTimer(bucket, bucket.timer) | |
248 bucket.timer = nil | |
249 end | |
250 | |
251 AceBucket.buckets[handle] = nil | |
252 -- store our bucket in the cache | |
253 bucketCache[bucket] = true | |
254 end | |
255 end | |
256 | |
257 --- Unregister all buckets of the current addon object (or custom "self"). | |
258 function AceBucket:UnregisterAllBuckets() | |
259 -- hmm can we do this more efficient? (it is not done often so shouldn't matter much) | |
260 for handle, bucket in pairs(AceBucket.buckets) do | |
261 if bucket.object == self then | |
262 AceBucket.UnregisterBucket(self, handle) | |
263 end | |
264 end | |
265 end | |
266 | |
267 | |
268 | |
269 -- embedding and embed handling | |
270 local mixins = { | |
271 "RegisterBucketEvent", | |
272 "RegisterBucketMessage", | |
273 "UnregisterBucket", | |
274 "UnregisterAllBuckets", | |
275 } | |
276 | |
277 -- Embeds AceBucket into the target object making the functions from the mixins list available on target:.. | |
278 -- @param target target object to embed AceBucket in | |
279 function AceBucket:Embed( target ) | |
280 for _, v in pairs( mixins ) do | |
281 target[v] = self[v] | |
282 end | |
283 self.embeds[target] = true | |
284 return target | |
285 end | |
286 | |
287 function AceBucket:OnEmbedDisable( target ) | |
288 target:UnregisterAllBuckets() | |
289 end | |
290 | |
291 for addon in pairs(AceBucket.embeds) do | |
292 AceBucket:Embed(addon) | |
293 end |