Skip to content

Commit

Permalink
Timers can be cancelled
Browse files Browse the repository at this point in the history
Allow timers to be cancelled.
  • Loading branch information
wingo committed Dec 11, 2017
1 parent 3c5f169 commit 6080eca
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/core/timer.lua
Expand Up @@ -33,6 +33,7 @@ local function call_timers (l)
if debug then
print(string.format("running timer %s at tick %s", timer.name, ticks))
end
timer.next_tick = nil
timer.fn(timer)
if timer.repeating then activate(timer) end
end
Expand All @@ -49,6 +50,7 @@ function run_to_time (ns)
end

function activate (t)
assert(t.next_tick == nil, "timer already activated")
-- Initialize time
if not ticks then
ticks = math.floor(tonumber(C.get_time_ns() / ns_per_tick))
Expand All @@ -59,6 +61,19 @@ function activate (t)
else
timers[tick] = {t}
end
t.next_tick = tick
end

function cancel (t)
if t.next_tick then
for idx, timer in ipairs(timers[t.next_tick]) do
if timer == t then
table.remove(timers[t.next_tick], idx)
t.next_tick = nil
return true
end
end
end
end

function new (name, fn, nanos, mode)
Expand Down

0 comments on commit 6080eca

Please sign in to comment.