mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-03 08:03:50 +00:00
feat: add C_Timer.After and C_Timer.NewTicker for Lua addons
Implement WoW's C_Timer API used by most modern addons:
- C_Timer.After(seconds, callback) — fire callback after delay
- C_Timer.NewTicker(seconds, callback, iterations) — repeating timer
with optional iteration limit and :Cancel() method
Implemented in pure Lua using a hidden OnUpdate frame that
auto-hides when no timers are pending (zero overhead when idle).
Example:
C_Timer.After(3, function() print("3 sec later!") end)
local ticker = C_Timer.NewTicker(1, function() print("tick") end, 5)
This commit is contained in:
parent
1f8660f329
commit
b235345b2c
1 changed files with 41 additions and 0 deletions
|
|
@ -691,6 +691,47 @@ void LuaEngine::registerCoreAPI() {
|
||||||
// OnUpdate frame tracking table
|
// OnUpdate frame tracking table
|
||||||
lua_newtable(L_);
|
lua_newtable(L_);
|
||||||
lua_setglobal(L_, "__WoweeOnUpdateFrames");
|
lua_setglobal(L_, "__WoweeOnUpdateFrames");
|
||||||
|
|
||||||
|
// C_Timer implementation via Lua (uses OnUpdate internally)
|
||||||
|
luaL_dostring(L_,
|
||||||
|
"C_Timer = {}\n"
|
||||||
|
"local timers = {}\n"
|
||||||
|
"local timerFrame = CreateFrame('Frame', '__WoweeTimerFrame')\n"
|
||||||
|
"timerFrame:SetScript('OnUpdate', function(self, elapsed)\n"
|
||||||
|
" local i = 1\n"
|
||||||
|
" while i <= #timers do\n"
|
||||||
|
" timers[i].remaining = timers[i].remaining - elapsed\n"
|
||||||
|
" if timers[i].remaining <= 0 then\n"
|
||||||
|
" local cb = timers[i].callback\n"
|
||||||
|
" table.remove(timers, i)\n"
|
||||||
|
" cb()\n"
|
||||||
|
" else\n"
|
||||||
|
" i = i + 1\n"
|
||||||
|
" end\n"
|
||||||
|
" end\n"
|
||||||
|
" if #timers == 0 then self:Hide() end\n"
|
||||||
|
"end)\n"
|
||||||
|
"timerFrame:Hide()\n"
|
||||||
|
"function C_Timer.After(seconds, callback)\n"
|
||||||
|
" tinsert(timers, {remaining = seconds, callback = callback})\n"
|
||||||
|
" timerFrame:Show()\n"
|
||||||
|
"end\n"
|
||||||
|
"function C_Timer.NewTicker(seconds, callback, iterations)\n"
|
||||||
|
" local count = 0\n"
|
||||||
|
" local maxIter = iterations or -1\n"
|
||||||
|
" local ticker = {cancelled = false}\n"
|
||||||
|
" local function tick()\n"
|
||||||
|
" if ticker.cancelled then return end\n"
|
||||||
|
" count = count + 1\n"
|
||||||
|
" callback(ticker)\n"
|
||||||
|
" if maxIter > 0 and count >= maxIter then return end\n"
|
||||||
|
" C_Timer.After(seconds, tick)\n"
|
||||||
|
" end\n"
|
||||||
|
" C_Timer.After(seconds, tick)\n"
|
||||||
|
" function ticker:Cancel() self.cancelled = true end\n"
|
||||||
|
" return ticker\n"
|
||||||
|
"end\n"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Event System ----
|
// ---- Event System ----
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue