annotate Libs/AceTimer-3.0/AceTimer-3.0.lua @ 0:98c6f55e6619

First commit
author Xiiph
date Sat, 05 Feb 2011 16:45:02 +0100
parents
children
rev   line source
Xiiph@0 1 --- **AceTimer-3.0** provides a central facility for registering timers.
Xiiph@0 2 -- AceTimer supports one-shot timers and repeating timers. All timers are stored in an efficient
Xiiph@0 3 -- data structure that allows easy dispatching and fast rescheduling. Timers can be registered, rescheduled
Xiiph@0 4 -- or canceled at any time, even from within a running timer, without conflict or large overhead.\\
Xiiph@0 5 -- AceTimer is currently limited to firing timers at a frequency of 0.1s. This constant may change
Xiiph@0 6 -- in the future, but for now it seemed like a good compromise in efficiency and accuracy.
Xiiph@0 7 --
Xiiph@0 8 -- All `:Schedule` functions will return a handle to the current timer, which you will need to store if you
Xiiph@0 9 -- need to cancel or reschedule the timer you just registered.
Xiiph@0 10 --
Xiiph@0 11 -- **AceTimer-3.0** can be embeded into your addon, either explicitly by calling AceTimer:Embed(MyAddon) or by
Xiiph@0 12 -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object
Xiiph@0 13 -- and can be accessed directly, without having to explicitly call AceTimer itself.\\
Xiiph@0 14 -- It is recommended to embed AceTimer, otherwise you'll have to specify a custom `self` on all calls you
Xiiph@0 15 -- make into AceTimer.
Xiiph@0 16 -- @class file
Xiiph@0 17 -- @name AceTimer-3.0
Xiiph@0 18 -- @release $Id: AceTimer-3.0.lua 895 2009-12-06 16:28:55Z nevcairiel $
Xiiph@0 19
Xiiph@0 20 --[[
Xiiph@0 21 Basic assumptions:
Xiiph@0 22 * In a typical system, we do more re-scheduling per second than there are timer pulses per second
Xiiph@0 23 * Regardless of timer implementation, we cannot guarantee timely delivery due to FPS restriction (may be as low as 10)
Xiiph@0 24
Xiiph@0 25 This implementation:
Xiiph@0 26 CON: The smallest timer interval is constrained by HZ (currently 1/10s).
Xiiph@0 27 PRO: It will still correctly fire any timer slower than HZ over a length of time, e.g. 0.11s interval -> 90 times over 10 seconds
Xiiph@0 28 PRO: In lag bursts, the system simly skips missed timer intervals to decrease load
Xiiph@0 29 CON: Algorithms depending on a timer firing "N times per minute" will fail
Xiiph@0 30 PRO: (Re-)scheduling is O(1) with a VERY small constant. It's a simple linked list insertion in a hash bucket.
Xiiph@0 31 CAUTION: The BUCKETS constant constrains how many timers can be efficiently handled. With too many hash collisions, performance will decrease.
Xiiph@0 32
Xiiph@0 33 Major assumptions upheld:
Xiiph@0 34 - ALLOWS scheduling multiple timers with the same funcref/method
Xiiph@0 35 - ALLOWS scheduling more timers during OnUpdate processing
Xiiph@0 36 - ALLOWS unscheduling ANY timer (including the current running one) at any time, including during OnUpdate processing
Xiiph@0 37 ]]
Xiiph@0 38
Xiiph@0 39 local MAJOR, MINOR = "AceTimer-3.0", 5
Xiiph@0 40 local AceTimer, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
Xiiph@0 41
Xiiph@0 42 if not AceTimer then return end -- No upgrade needed
Xiiph@0 43
Xiiph@0 44 AceTimer.hash = AceTimer.hash or {} -- Array of [0..BUCKET-1] = linked list of timers (using .next member)
Xiiph@0 45 -- Linked list gets around ACE-88 and ACE-90.
Xiiph@0 46 AceTimer.selfs = AceTimer.selfs or {} -- Array of [self]={[handle]=timerobj, [handle2]=timerobj2, ...}
Xiiph@0 47 AceTimer.frame = AceTimer.frame or CreateFrame("Frame", "AceTimer30Frame")
Xiiph@0 48
Xiiph@0 49 -- Lua APIs
Xiiph@0 50 local assert, error, loadstring = assert, error, loadstring
Xiiph@0 51 local setmetatable, rawset, rawget = setmetatable, rawset, rawget
Xiiph@0 52 local select, pairs, type, next, tostring = select, pairs, type, next, tostring
Xiiph@0 53 local floor, max, min = math.floor, math.max, math.min
Xiiph@0 54 local tconcat = table.concat
Xiiph@0 55
Xiiph@0 56 -- WoW APIs
Xiiph@0 57 local GetTime = GetTime
Xiiph@0 58
Xiiph@0 59 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
Xiiph@0 60 -- List them here for Mikk's FindGlobals script
Xiiph@0 61 -- GLOBALS: DEFAULT_CHAT_FRAME, geterrorhandler
Xiiph@0 62
Xiiph@0 63 -- Simple ONE-SHOT timer cache. Much more efficient than a full compost for our purposes.
Xiiph@0 64 local timerCache = nil
Xiiph@0 65
Xiiph@0 66 --[[
Xiiph@0 67 Timers will not be fired more often than HZ-1 times per second.
Xiiph@0 68 Keep at intended speed PLUS ONE or we get bitten by floating point rounding errors (n.5 + 0.1 can be n.599999)
Xiiph@0 69 If this is ever LOWERED, all existing timers need to be enforced to have a delay >= 1/HZ on lib upgrade.
Xiiph@0 70 If this number is ever changed, all entries need to be rehashed on lib upgrade.
Xiiph@0 71 ]]
Xiiph@0 72 local HZ = 11
Xiiph@0 73
Xiiph@0 74 --[[
Xiiph@0 75 Prime for good distribution
Xiiph@0 76 If this number is ever changed, all entries need to be rehashed on lib upgrade.
Xiiph@0 77 ]]
Xiiph@0 78 local BUCKETS = 131
Xiiph@0 79
Xiiph@0 80 local hash = AceTimer.hash
Xiiph@0 81 for i=1,BUCKETS do
Xiiph@0 82 hash[i] = hash[i] or false -- make it an integer-indexed array; it's faster than hashes
Xiiph@0 83 end
Xiiph@0 84
Xiiph@0 85 --[[
Xiiph@0 86 xpcall safecall implementation
Xiiph@0 87 ]]
Xiiph@0 88 local xpcall = xpcall
Xiiph@0 89
Xiiph@0 90 local function errorhandler(err)
Xiiph@0 91 return geterrorhandler()(err)
Xiiph@0 92 end
Xiiph@0 93
Xiiph@0 94 local function CreateDispatcher(argCount)
Xiiph@0 95 local code = [[
Xiiph@0 96 local xpcall, eh = ... -- our arguments are received as unnamed values in "..." since we don't have a proper function declaration
Xiiph@0 97 local method, ARGS
Xiiph@0 98 local function call() return method(ARGS) end
Xiiph@0 99
Xiiph@0 100 local function dispatch(func, ...)
Xiiph@0 101 method = func
Xiiph@0 102 if not method then return end
Xiiph@0 103 ARGS = ...
Xiiph@0 104 return xpcall(call, eh)
Xiiph@0 105 end
Xiiph@0 106
Xiiph@0 107 return dispatch
Xiiph@0 108 ]]
Xiiph@0 109
Xiiph@0 110 local ARGS = {}
Xiiph@0 111 for i = 1, argCount do ARGS[i] = "arg"..i end
Xiiph@0 112 code = code:gsub("ARGS", tconcat(ARGS, ", "))
Xiiph@0 113 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler)
Xiiph@0 114 end
Xiiph@0 115
Xiiph@0 116 local Dispatchers = setmetatable({}, {
Xiiph@0 117 __index=function(self, argCount)
Xiiph@0 118 local dispatcher = CreateDispatcher(argCount)
Xiiph@0 119 rawset(self, argCount, dispatcher)
Xiiph@0 120 return dispatcher
Xiiph@0 121 end
Xiiph@0 122 })
Xiiph@0 123 Dispatchers[0] = function(func)
Xiiph@0 124 return xpcall(func, errorhandler)
Xiiph@0 125 end
Xiiph@0 126
Xiiph@0 127 local function safecall(func, ...)
Xiiph@0 128 return Dispatchers[select('#', ...)](func, ...)
Xiiph@0 129 end
Xiiph@0 130
Xiiph@0 131 local lastint = floor(GetTime() * HZ)
Xiiph@0 132
Xiiph@0 133 -- --------------------------------------------------------------------
Xiiph@0 134 -- OnUpdate handler
Xiiph@0 135 --
Xiiph@0 136 -- traverse buckets, always chasing "now", and fire timers that have expired
Xiiph@0 137
Xiiph@0 138 local function OnUpdate()
Xiiph@0 139 local now = GetTime()
Xiiph@0 140 local nowint = floor(now * HZ)
Xiiph@0 141
Xiiph@0 142 -- Have we passed into a new hash bucket?
Xiiph@0 143 if nowint == lastint then return end
Xiiph@0 144
Xiiph@0 145 local soon = now + 1 -- +1 is safe as long as 1 < HZ < BUCKETS/2
Xiiph@0 146
Xiiph@0 147 -- Pass through each bucket at most once
Xiiph@0 148 -- Happens on e.g. instance loads, but COULD happen on high local load situations also
Xiiph@0 149 for curint = (max(lastint, nowint - BUCKETS) + 1), nowint do -- loop until we catch up with "now", usually only 1 iteration
Xiiph@0 150 local curbucket = (curint % BUCKETS)+1
Xiiph@0 151 -- Yank the list of timers out of the bucket and empty it. This allows reinsertion in the currently-processed bucket from callbacks.
Xiiph@0 152 local nexttimer = hash[curbucket]
Xiiph@0 153 hash[curbucket] = false -- false rather than nil to prevent the array from becoming a hash
Xiiph@0 154
Xiiph@0 155 while nexttimer do
Xiiph@0 156 local timer = nexttimer
Xiiph@0 157 nexttimer = timer.next
Xiiph@0 158 local when = timer.when
Xiiph@0 159
Xiiph@0 160 if when < soon then
Xiiph@0 161 -- Call the timer func, either as a method on given object, or a straight function ref
Xiiph@0 162 local callback = timer.callback
Xiiph@0 163 if type(callback) == "string" then
Xiiph@0 164 safecall(timer.object[callback], timer.object, timer.arg)
Xiiph@0 165 elseif callback then
Xiiph@0 166 safecall(callback, timer.arg)
Xiiph@0 167 else
Xiiph@0 168 -- probably nilled out by CancelTimer
Xiiph@0 169 timer.delay = nil -- don't reschedule it
Xiiph@0 170 end
Xiiph@0 171
Xiiph@0 172 local delay = timer.delay -- NOW make a local copy, can't do it earlier in case the timer cancelled itself in the callback
Xiiph@0 173
Xiiph@0 174 if not delay then
Xiiph@0 175 -- single-shot timer (or cancelled)
Xiiph@0 176 AceTimer.selfs[timer.object][tostring(timer)] = nil
Xiiph@0 177 timerCache = timer
Xiiph@0 178 else
Xiiph@0 179 -- repeating timer
Xiiph@0 180 local newtime = when + delay
Xiiph@0 181 if newtime < now then -- Keep lag from making us firing a timer unnecessarily. (Note that this still won't catch too-short-delay timers though.)
Xiiph@0 182 newtime = now + delay
Xiiph@0 183 end
Xiiph@0 184 timer.when = newtime
Xiiph@0 185
Xiiph@0 186 -- add next timer execution to the correct bucket
Xiiph@0 187 local bucket = (floor(newtime * HZ) % BUCKETS) + 1
Xiiph@0 188 timer.next = hash[bucket]
Xiiph@0 189 hash[bucket] = timer
Xiiph@0 190 end
Xiiph@0 191 else -- if when>=soon
Xiiph@0 192 -- reinsert (yeah, somewhat expensive, but shouldn't be happening too often either due to hash distribution)
Xiiph@0 193 timer.next = hash[curbucket]
Xiiph@0 194 hash[curbucket] = timer
Xiiph@0 195 end -- if when<soon ... else
Xiiph@0 196 end -- while nexttimer do
Xiiph@0 197 end -- for curint=lastint,nowint
Xiiph@0 198
Xiiph@0 199 lastint = nowint
Xiiph@0 200 end
Xiiph@0 201
Xiiph@0 202 -- ---------------------------------------------------------------------
Xiiph@0 203 -- Reg( callback, delay, arg, repeating )
Xiiph@0 204 --
Xiiph@0 205 -- callback( function or string ) - direct function ref or method name in our object for the callback
Xiiph@0 206 -- delay(int) - delay for the timer
Xiiph@0 207 -- arg(variant) - any argument to be passed to the callback function
Xiiph@0 208 -- repeating(boolean) - repeating timer, or oneshot
Xiiph@0 209 --
Xiiph@0 210 -- returns the handle of the timer for later processing (canceling etc)
Xiiph@0 211 local function Reg(self, callback, delay, arg, repeating)
Xiiph@0 212 if type(callback) ~= "string" and type(callback) ~= "function" then
Xiiph@0 213 local error_origin = repeating and "ScheduleRepeatingTimer" or "ScheduleTimer"
Xiiph@0 214 error(MAJOR..": " .. error_origin .. "(callback, delay, arg): 'callback' - function or method name expected.", 3)
Xiiph@0 215 end
Xiiph@0 216 if type(callback) == "string" then
Xiiph@0 217 if type(self)~="table" then
Xiiph@0 218 local error_origin = repeating and "ScheduleRepeatingTimer" or "ScheduleTimer"
Xiiph@0 219 error(MAJOR..": " .. error_origin .. "(\"methodName\", delay, arg): 'self' - must be a table.", 3)
Xiiph@0 220 end
Xiiph@0 221 if type(self[callback]) ~= "function" then
Xiiph@0 222 local error_origin = repeating and "ScheduleRepeatingTimer" or "ScheduleTimer"
Xiiph@0 223 error(MAJOR..": " .. error_origin .. "(\"methodName\", delay, arg): 'methodName' - method not found on target object.", 3)
Xiiph@0 224 end
Xiiph@0 225 end
Xiiph@0 226
Xiiph@0 227 if delay < (1 / (HZ - 1)) then
Xiiph@0 228 delay = 1 / (HZ - 1)
Xiiph@0 229 end
Xiiph@0 230
Xiiph@0 231 -- Create and stuff timer in the correct hash bucket
Xiiph@0 232 local now = GetTime()
Xiiph@0 233
Xiiph@0 234 local timer = timerCache or {} -- Get new timer object (from cache if available)
Xiiph@0 235 timerCache = nil
Xiiph@0 236
Xiiph@0 237 timer.object = self
Xiiph@0 238 timer.callback = callback
Xiiph@0 239 timer.delay = (repeating and delay)
Xiiph@0 240 timer.arg = arg
Xiiph@0 241 timer.when = now + delay
Xiiph@0 242
Xiiph@0 243 local bucket = (floor((now+delay)*HZ) % BUCKETS) + 1
Xiiph@0 244 timer.next = hash[bucket]
Xiiph@0 245 hash[bucket] = timer
Xiiph@0 246
Xiiph@0 247 -- Insert timer in our self->handle->timer registry
Xiiph@0 248 local handle = tostring(timer)
Xiiph@0 249
Xiiph@0 250 local selftimers = AceTimer.selfs[self]
Xiiph@0 251 if not selftimers then
Xiiph@0 252 selftimers = {}
Xiiph@0 253 AceTimer.selfs[self] = selftimers
Xiiph@0 254 end
Xiiph@0 255 selftimers[handle] = timer
Xiiph@0 256 selftimers.__ops = (selftimers.__ops or 0) + 1
Xiiph@0 257
Xiiph@0 258 return handle
Xiiph@0 259 end
Xiiph@0 260
Xiiph@0 261 --- Schedule a new one-shot timer.
Xiiph@0 262 -- The timer will fire once in `delay` seconds, unless canceled before.
Xiiph@0 263 -- @param callback Callback function for the timer pulse (funcref or method name).
Xiiph@0 264 -- @param delay Delay for the timer, in seconds.
Xiiph@0 265 -- @param arg An optional argument to be passed to the callback function.
Xiiph@0 266 -- @usage
Xiiph@0 267 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("TimerTest", "AceTimer-3.0")
Xiiph@0 268 --
Xiiph@0 269 -- function MyAddon:OnEnable()
Xiiph@0 270 -- self:ScheduleTimer("TimerFeedback", 5)
Xiiph@0 271 -- end
Xiiph@0 272 --
Xiiph@0 273 -- function MyAddon:TimerFeedback()
Xiiph@0 274 -- print("5 seconds passed")
Xiiph@0 275 -- end
Xiiph@0 276 function AceTimer:ScheduleTimer(callback, delay, arg)
Xiiph@0 277 return Reg(self, callback, delay, arg)
Xiiph@0 278 end
Xiiph@0 279
Xiiph@0 280 --- Schedule a repeating timer.
Xiiph@0 281 -- The timer will fire every `delay` seconds, until canceled.
Xiiph@0 282 -- @param callback Callback function for the timer pulse (funcref or method name).
Xiiph@0 283 -- @param delay Delay for the timer, in seconds.
Xiiph@0 284 -- @param arg An optional argument to be passed to the callback function.
Xiiph@0 285 -- @usage
Xiiph@0 286 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("TimerTest", "AceTimer-3.0")
Xiiph@0 287 --
Xiiph@0 288 -- function MyAddon:OnEnable()
Xiiph@0 289 -- self.timerCount = 0
Xiiph@0 290 -- self.testTimer = self:ScheduleRepeatingTimer("TimerFeedback", 5)
Xiiph@0 291 -- end
Xiiph@0 292 --
Xiiph@0 293 -- function MyAddon:TimerFeedback()
Xiiph@0 294 -- self.timerCount = self.timerCount + 1
Xiiph@0 295 -- print(("%d seconds passed"):format(5 * self.timerCount))
Xiiph@0 296 -- -- run 30 seconds in total
Xiiph@0 297 -- if self.timerCount == 6 then
Xiiph@0 298 -- self:CancelTimer(self.testTimer)
Xiiph@0 299 -- end
Xiiph@0 300 -- end
Xiiph@0 301 function AceTimer:ScheduleRepeatingTimer(callback, delay, arg)
Xiiph@0 302 return Reg(self, callback, delay, arg, true)
Xiiph@0 303 end
Xiiph@0 304
Xiiph@0 305 --- Cancels a timer with the given handle, registered by the same addon object as used for `:ScheduleTimer`
Xiiph@0 306 -- Both one-shot and repeating timers can be canceled with this function, as long as the `handle` is valid
Xiiph@0 307 -- and the timer has not fired yet or was canceled before.
Xiiph@0 308 -- @param handle The handle of the timer, as returned by `:ScheduleTimer` or `:ScheduleRepeatingTimer`
Xiiph@0 309 -- @param silent If true, no error is raised if the timer handle is invalid (expired or already canceled)
Xiiph@0 310 -- @return True if the timer was successfully cancelled.
Xiiph@0 311 function AceTimer:CancelTimer(handle, silent)
Xiiph@0 312 if not handle then return end -- nil handle -> bail out without erroring
Xiiph@0 313 if type(handle) ~= "string" then
Xiiph@0 314 error(MAJOR..": CancelTimer(handle): 'handle' - expected a string", 2) -- for now, anyway
Xiiph@0 315 end
Xiiph@0 316 local selftimers = AceTimer.selfs[self]
Xiiph@0 317 local timer = selftimers and selftimers[handle]
Xiiph@0 318 if silent then
Xiiph@0 319 if timer then
Xiiph@0 320 timer.callback = nil -- don't run it again
Xiiph@0 321 timer.delay = nil -- if this is the currently-executing one: don't even reschedule
Xiiph@0 322 -- The timer object is removed in the OnUpdate loop
Xiiph@0 323 end
Xiiph@0 324 return not not timer -- might return "true" even if we double-cancel. we'll live.
Xiiph@0 325 else
Xiiph@0 326 if not timer then
Xiiph@0 327 geterrorhandler()(MAJOR..": CancelTimer(handle[, silent]): '"..tostring(handle).."' - no such timer registered")
Xiiph@0 328 return false
Xiiph@0 329 end
Xiiph@0 330 if not timer.callback then
Xiiph@0 331 geterrorhandler()(MAJOR..": CancelTimer(handle[, silent]): '"..tostring(handle).."' - timer already cancelled or expired")
Xiiph@0 332 return false
Xiiph@0 333 end
Xiiph@0 334 timer.callback = nil -- don't run it again
Xiiph@0 335 timer.delay = nil -- if this is the currently-executing one: don't even reschedule
Xiiph@0 336 return true
Xiiph@0 337 end
Xiiph@0 338 end
Xiiph@0 339
Xiiph@0 340 --- Cancels all timers registered to the current addon object ('self')
Xiiph@0 341 function AceTimer:CancelAllTimers()
Xiiph@0 342 if not(type(self) == "string" or type(self) == "table") then
Xiiph@0 343 error(MAJOR..": CancelAllTimers(): 'self' - must be a string or a table",2)
Xiiph@0 344 end
Xiiph@0 345 if self == AceTimer then
Xiiph@0 346 error(MAJOR..": CancelAllTimers(): supply a meaningful 'self'", 2)
Xiiph@0 347 end
Xiiph@0 348
Xiiph@0 349 local selftimers = AceTimer.selfs[self]
Xiiph@0 350 if selftimers then
Xiiph@0 351 for handle,v in pairs(selftimers) do
Xiiph@0 352 if type(v) == "table" then -- avoid __ops, etc
Xiiph@0 353 AceTimer.CancelTimer(self, handle, true)
Xiiph@0 354 end
Xiiph@0 355 end
Xiiph@0 356 end
Xiiph@0 357 end
Xiiph@0 358
Xiiph@0 359 --- Returns the time left for a timer with the given handle, registered by the current addon object ('self').
Xiiph@0 360 -- This function will raise a warning when the handle is invalid, but not stop execution.
Xiiph@0 361 -- @param handle The handle of the timer, as returned by `:ScheduleTimer` or `:ScheduleRepeatingTimer`
Xiiph@0 362 -- @return The time left on the timer, or false if the handle is invalid.
Xiiph@0 363 function AceTimer:TimeLeft(handle)
Xiiph@0 364 if not handle then return end
Xiiph@0 365 if type(handle) ~= "string" then
Xiiph@0 366 error(MAJOR..": TimeLeft(handle): 'handle' - expected a string", 2) -- for now, anyway
Xiiph@0 367 end
Xiiph@0 368 local selftimers = AceTimer.selfs[self]
Xiiph@0 369 local timer = selftimers and selftimers[handle]
Xiiph@0 370 if not timer then
Xiiph@0 371 geterrorhandler()(MAJOR..": TimeLeft(handle): '"..tostring(handle).."' - no such timer registered")
Xiiph@0 372 return false
Xiiph@0 373 end
Xiiph@0 374 return timer.when - GetTime()
Xiiph@0 375 end
Xiiph@0 376
Xiiph@0 377
Xiiph@0 378 -- ---------------------------------------------------------------------
Xiiph@0 379 -- PLAYER_REGEN_ENABLED: Run through our .selfs[] array step by step
Xiiph@0 380 -- and clean it out - otherwise the table indices can grow indefinitely
Xiiph@0 381 -- if an addon starts and stops a lot of timers. AceBucket does this!
Xiiph@0 382 --
Xiiph@0 383 -- See ACE-94 and tests/AceTimer-3.0-ACE-94.lua
Xiiph@0 384
Xiiph@0 385 local lastCleaned = nil
Xiiph@0 386
Xiiph@0 387 local function OnEvent(this, event)
Xiiph@0 388 if event~="PLAYER_REGEN_ENABLED" then
Xiiph@0 389 return
Xiiph@0 390 end
Xiiph@0 391
Xiiph@0 392 -- Get the next 'self' to process
Xiiph@0 393 local selfs = AceTimer.selfs
Xiiph@0 394 local self = next(selfs, lastCleaned)
Xiiph@0 395 if not self then
Xiiph@0 396 self = next(selfs)
Xiiph@0 397 end
Xiiph@0 398 lastCleaned = self
Xiiph@0 399 if not self then -- should only happen if .selfs[] is empty
Xiiph@0 400 return
Xiiph@0 401 end
Xiiph@0 402
Xiiph@0 403 -- Time to clean it out?
Xiiph@0 404 local list = selfs[self]
Xiiph@0 405 if (list.__ops or 0) < 250 then -- 250 slosh indices = ~10KB wasted (max!). For one 'self'.
Xiiph@0 406 return
Xiiph@0 407 end
Xiiph@0 408
Xiiph@0 409 -- Create a new table and copy all members over
Xiiph@0 410 local newlist = {}
Xiiph@0 411 local n=0
Xiiph@0 412 for k,v in pairs(list) do
Xiiph@0 413 newlist[k] = v
Xiiph@0 414 n=n+1
Xiiph@0 415 end
Xiiph@0 416 newlist.__ops = 0 -- Reset operation count
Xiiph@0 417
Xiiph@0 418 -- And since we now have a count of the number of live timers, check that it's reasonable. Emit a warning if not.
Xiiph@0 419 if n>BUCKETS then
Xiiph@0 420 DEFAULT_CHAT_FRAME:AddMessage(MAJOR..": Warning: The addon/module '"..tostring(self).."' has "..n.." live timers. Surely that's not intended?")
Xiiph@0 421 end
Xiiph@0 422
Xiiph@0 423 selfs[self] = newlist
Xiiph@0 424 end
Xiiph@0 425
Xiiph@0 426 -- ---------------------------------------------------------------------
Xiiph@0 427 -- Embed handling
Xiiph@0 428
Xiiph@0 429 AceTimer.embeds = AceTimer.embeds or {}
Xiiph@0 430
Xiiph@0 431 local mixins = {
Xiiph@0 432 "ScheduleTimer", "ScheduleRepeatingTimer",
Xiiph@0 433 "CancelTimer", "CancelAllTimers",
Xiiph@0 434 "TimeLeft"
Xiiph@0 435 }
Xiiph@0 436
Xiiph@0 437 function AceTimer:Embed(target)
Xiiph@0 438 AceTimer.embeds[target] = true
Xiiph@0 439 for _,v in pairs(mixins) do
Xiiph@0 440 target[v] = AceTimer[v]
Xiiph@0 441 end
Xiiph@0 442 return target
Xiiph@0 443 end
Xiiph@0 444
Xiiph@0 445 -- AceTimer:OnEmbedDisable( target )
Xiiph@0 446 -- target (object) - target object that AceTimer is embedded in.
Xiiph@0 447 --
Xiiph@0 448 -- cancel all timers registered for the object
Xiiph@0 449 function AceTimer:OnEmbedDisable( target )
Xiiph@0 450 target:CancelAllTimers()
Xiiph@0 451 end
Xiiph@0 452
Xiiph@0 453
Xiiph@0 454 for addon in pairs(AceTimer.embeds) do
Xiiph@0 455 AceTimer:Embed(addon)
Xiiph@0 456 end
Xiiph@0 457
Xiiph@0 458 -- ---------------------------------------------------------------------
Xiiph@0 459 -- Debug tools (expose copies of internals to test suites)
Xiiph@0 460 AceTimer.debug = AceTimer.debug or {}
Xiiph@0 461 AceTimer.debug.HZ = HZ
Xiiph@0 462 AceTimer.debug.BUCKETS = BUCKETS
Xiiph@0 463
Xiiph@0 464 -- ---------------------------------------------------------------------
Xiiph@0 465 -- Finishing touchups
Xiiph@0 466
Xiiph@0 467 AceTimer.frame:SetScript("OnUpdate", OnUpdate)
Xiiph@0 468 AceTimer.frame:SetScript("OnEvent", OnEvent)
Xiiph@0 469 AceTimer.frame:RegisterEvent("PLAYER_REGEN_ENABLED")
Xiiph@0 470
Xiiph@0 471 -- In theory, we should hide&show the frame based on there being timers or not.
Xiiph@0 472 -- However, this job is fairly expensive, and the chance that there will
Xiiph@0 473 -- actually be zero timers running is diminuitive to say the lest.