C_TimerAugment.lua

C_TimerAugment.lua (8.3.7.35284; unchanged since 7.3.5.25864)
  1. local TickerPrototype = {};
  2. local TickerMetatable = {
  3. __index = TickerPrototype,
  4. __metatable = true, --Probably not needed, but if I don't put this here someone is going to mess with this metatable and end up tainting everything...
  5. };
  6. --Creates and starts a ticker that calls callback every duration seconds for N iterations.
  7. --If iterations is nil, the ticker will loop until cancelled.
  8. --
  9. --If callback throws a Lua error, the ticker will stop firing.
  10. function C_Timer.NewTicker(duration, callback, iterations)
  11. local ticker = setmetatable({}, TickerMetatable);
  12. ticker._remainingIterations = iterations;
  13. ticker._callback = function()
  14. if ( not ticker._cancelled ) then
  15. callback(ticker);
  16. --Make sure we weren't cancelled during the callback
  17. if ( not ticker._cancelled ) then
  18. if ( ticker._remainingIterations ) then
  19. ticker._remainingIterations = ticker._remainingIterations - 1;
  20. end
  21. if ( not ticker._remainingIterations or ticker._remainingIterations > 0 ) then
  22. C_Timer.After(duration, ticker._callback);
  23. end
  24. end
  25. end
  26. end;
  27. C_Timer.After(duration, ticker._callback);
  28. return ticker;
  29. end
  30. --Creates and starts a cancellable timer that calls callback after duration seconds.
  31. --Note that C_Timer.NewTimer is significantly more expensive than C_Timer.After and should
  32. --only be used if you actually need any of its additional functionality.
  33. --
  34. --While timers are currently just tickers with an iteration count of 1, this is likely
  35. --to change in the future and shouldn't be relied on.
  36. function C_Timer.NewTimer(duration, callback)
  37. return C_Timer.NewTicker(duration, callback, 1);
  38. end
  39. --Cancels a ticker or timer. May be safely called within the ticker's callback in which
  40. --case the ticker simply won't be started again.
  41. --Cancel is guaranteed to be idempotent.
  42. function TickerPrototype:Cancel()
  43. self._cancelled = true;
  44. end
  45. function TickerPrototype:IsCancelled()
  46. return self._cancelled;
  47. end