2
-- A timer allows delayed or periodic execution of a function according
3
-- to elapsed time in an app. In order for it to work properly, it must
4
-- receive update events, so it must be added somewhere in the current
5
-- view or app. If you are using the <View> class, then this is already
6
-- done for you; one is created as the View's timer property.
17
-- Delays a function call after a certain a mount of time.
20
-- * delay - how long to wait, in seconds
21
-- * func - function to call
24
-- A <Promise> that is fulfilled after the function is called
26
after = function (self, delay, func)
28
assert(type(func) == 'function', 'func property of timer must be a function')
29
assert(type(delay) == 'number', 'delay must be a number')
32
local info = debug.getinfo(2, 'Sl')
33
print('Warning: timer delay is ' .. delay .. ', will be triggered immediately (' ..
34
info.short_src .. ', line ' .. info.currentline .. ')')
39
local promise = Promise:new()
40
table.insert(self.timers, { func = func, timeLeft = delay, promise = promise })
45
-- Repeatedly makes a function call. To stop these calls from
46
-- happening in the future, you must call stop().
49
-- * delay - how often to make the function call, in seconds
50
-- * func - function to call
55
every = function (self, delay, func)
57
assert(type(func) == 'function', 'func property of timer must be a function')
58
assert(type(delay) == 'number', 'delay must be a number')
61
local info = debug.getinfo(2, 'Sl')
62
print('Warning: timer delay is ' .. delay .. ', will be triggered immediately (' ..
63
info.short_src .. ', line ' .. info.currentline .. ')')
68
table.insert(self.timers, { func = func, timeLeft = delay, interval = delay })
72
-- Returns how much time is left before a function call is scheduled.
75
-- func - the function that is queued
78
-- the time left until the soonest call matching these arguments,
79
-- or nil if there is no call scheduled
81
status = function (self, func, bind, arg)
84
for _, t in pairs(self.timers) do
85
if t.func == func and (not result or result < t.timeLeft) then
94
-- Stops a timer from executing. The promise belonging to it is failed.
95
-- If there is no function associated with this timer, then this has no effect.
98
-- func - function to stop; if omitted, stops all timers
103
stop = function (self, func, bind)
106
for i, timer in ipairs(self.timers) do
107
if not func or timer.func == func then
108
if timer.promise then
109
timer.promise:fail('Timer stopped')
112
table.remove(self.timers, i)
117
if STRICT and not found then
118
local info = debug.getinfo(2, 'Sl')
119
print('Warning: asked to stop a timer on a function that was not queued (' ..
120
info.short_src .. ', line ' .. info.currentline .. ')')
124
update = function (self, elapsed)
125
for i, timer in ipairs(self.timers) do
126
timer.timeLeft = timer.timeLeft - elapsed
128
if timer.timeLeft <= 0 then
130
if timer.promise then
131
timer.promise:fulfill(timer.func())
136
if timer.interval then
137
timer.timeLeft = timer.interval
140
table.remove(self.timers, i)
147
self.active = (#self.timers > 0)
150
__tostring = function (self)
151
local result = 'Timer ('
154
result = result .. 'active, '
155
result = result .. #self.timers .. ' timers running'
157
result = result .. 'inactive'