ELScheduler

ELScheduler
Login

Embeddable Lua Scheduler is a pure Lua library 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.

Note: Time going backwards will not change/advance the scheduler time.

Note: (performance) A binary heap is used to manage timer priorities.

Install

See src/, rockspecs/ or luarocks.

API

Scheduler

self.time -- current time
self.timers -- binary heap of bound timers (as array/list)
-- #self.timers => number of waiting timers

-- scheduler constructor (the module itself)
-- time: (optional) init time (numeric type, default: 0)
Scheduler(time)

-- create a timer
-- delay: can be <= 0 for instantaneous timers (numeric type)
-- count: (optional) number of iterations (default: 1, infinite: -1)
-- callback(timer): called when the timer is triggered
-- return timer
Scheduler:timer(delay, count, callback)

-- do a scheduler tick
-- time: current time (numeric type)
Scheduler:tick(time)

Timer

self.scheduler -- timer's scheduler
self.wake -- wake-up time (useful to compute delta)
self.delay -- delay/interval (start_time = wake - delay)
self.count -- remaining iterations (excluding current scheduled iteration)

-- remove the timer
-- (safe to call multiple times)
Timer:remove()