To schedule a task to be performed later, an “agenda” is used. There is a default, global agenda that is ready to be used, or additional agendas may be created for different purposes. The following example prints the text “hello” when the agenda has advanced to time unit 10.
(at 10 (display "hello\n"))
Most of the time it is more convenient to schedule tasks relative to
the current time. This is where after
comes in handy:
(after 10 (display "hello\n"))
Time units in the agenda are in no way connected to real time. It’s up to the programmer to decide what agenda time means. A simple and effective approach is to map each call of the update procedure (see Kernel) to 1 unit of agenda time, like so:
(define (update dt) (update-agenda 1))
It is important to call update-agenda
periodically, otherwise
no tasks will ever be run!
In addition to using the global agenda, it is useful to have multiple
agendas for different purposes. For example, the game world can use a
different agenda than the user interface, so that pausing the game is
a simple matter of not updating the world’s agenda while continuing to
update the user interface’s agenda. The current agenda is dynamically
scoped and can be changed using the with-agenda
special form:
(define game-world-agenda (make-agenda)) (with-agenda game-world-agenda (at 60 (spawn-goblin)) (at 120 (spawn-goblin)) (at 240 (spawn-goblin-king)))
Return a new task scheduler.
Return #t
if obj is an agenda.
When called with no arguments, return the current agenda. When called with one argument, set the current agenda to agenda.
Evaluate body with the current agenda set to agenda.
Return the current agenda time.
Advance the current agenda by dt.
Schedule thunk, a procedure of zero arguments, to be run at time.
Schedule thunk, a procedure of zero arguments, to be run after delay.
Schedule thunk, a procedure of zero arguments, to be run every interval amount of time. Repeat this n times, or indefinitely if not specified.
Schedule body to be evaluated at time.
Schedule body to be evaluated after delay.
Schedule body to be evaluated every interval amount of time. Repeat this n times, or indefinitely if not specified.
It is also possible to schedule things that are not dependent on how much time passes. The agenda will periodically poll to see if any registered conditions are met.
Call thunk sometime in the future when pred is satisfied
(returns a value other than #f
.)