sumi

Basics · Signals

Updating state

There are two ways to write a signal. Set(v) replaces the value outright. Update(fn) reads the current value, runs it through your function, and writes the result — a read-modify-write without a separate Get.

The counter below increments on every key, using Update so it can add one to whatever the count already is. A running total is a natural fit for Update; setting a fixed value is a fit for Set.

Your turn

Add a reset. In handleKey, before the increment, handle the r key by calling count.Set(0) and returning:

if evt.Rune == 'r' {
    count.Set(0)
    return
}

Press Run, click into the terminal, count up with any key, then press r to reset. Solve shows the finished version; Reset restores the start.

⌘↵ to run