Events

A Tcl program that waits for a timer, a channel, or a socket uses the event loop. after puts a script in the loop, and vwait runs the loop until a variable changes. A coroutine is a proc that stops at yield and goes on at its next call. This lesson gives jot reminders.

The shell of this page has the event loop on a virtual clock. A wait of the loop moves the clock to the next timer at once, so a timer of 200 ms runs with no wait. ztcl on your computer waits the real time. The event loop is a capability there: run a script with ztcl --allow event FILE.

Timers

after MS SCRIPT runs the script after MS milliseconds, from the event loop. The script runs at the top level, so it gets its values as a list, made when after runs.

proc remind {text ms} {
    after $ms [list puts "reminder: $text"]
}

remind "call Ada" 200 and remind "buy milk" 100 put two timers in the loop. No reminder prints yet. after 300 {set ::done 1} adds a third timer. vwait done runs the loop until the third timer sets done: the reminders print in the order of their times.

after gives an id, and after cancel ID removes the timer: after cancel [remind "never" 50]. A script that after runs has no caller to give an error to. The error goes to bgerror: after 0 {error boom}; update prints the error and goes on.

Coroutines

coroutine NAME CMD makes a command NAME and runs CMD until its first yield. The value of that yield is the result of coroutine. Each call of NAME runs to the next yield.

The shell of this page has no coroutine, so run this example in ztcl on your computer.

coroutine tags apply {{notes} {
    yield
    foreach n $notes {
        foreach t [regexp -all -inline {#\w+} $n] { yield $t }
    }
    return ""
}} {"buy milk #shop" "fix the bike #home #shed"}
while {[set t [tags]] ne ""} { puts $t }
#shop
#home
#shed

The first yield has no value, so that the first call of tags gives the first tag. A coroutine that ends with return is gone, and a call after that is an error.

Reading code an agent wrote

Ask an agent

Add jot::remind {i minutes} that prints the note i after the given minutes with after. Build the script with list, keep the timer ids in a dict, and add jot::cancel {i}.

Try this