Embeddable Lua Scheduler is a Lua module to manage timers.
Timers are bound to a point in time and are triggered when
current_time >= timer_time
.
Warning: Repeated timers are implemented as regular timers re-added if needed before being triggered. Thus, a timer can only be triggered once per tick; triggers may be shifted or discarded based on the tick period.
Performance note: A binary heap is used to manage timer priorities.
Install
See src/
, rockspecs/
or
luarocks.
API
Module
Scheduler ([time]) -> scheduler
Create a new scheduler. This function is returned when loading the module.
- time: initial time (numeric type, default is 0)
Scheduler
scheduler:timer (delay, [count, ] callback) -> timer
Create a timer.
delay
: can be<= 0
for instantaneous timers (numeric type).count
: number of iterations (default: 1, infinite: -1)callback (timer)
: called when the timer triggers
scheduler:tick (time)
Do a scheduler tick. Time going backwards will not change/advance the scheduler's time.
time
: current time for the tick (numeric type)
Fields
time
: current timetimers
: binary heap of bound timers (as array/sequence)
The expression #self.timers
gives the number of pending timers.
Timer
timer:remove ()
Remove the timer. Idempotent.
Once a timer is removed, it will never trigger again.
Fields
scheduler
: timer's schedulerwake
: wake-up time (useful to compute delta)delay
: period/interval (start_time = wake - delay
)count
: remaining iterations (excluding current scheduled iteration)