Reactivity¶
Tesserae's reactivity lives in tess_ui.reactive and is exported from
tess_ui itself.
Signal is the basic reactive cell:
from tess_ui import Signal
count = Signal(0)
count.get() # 0 -- records a read-dependency when called during a binding's evaluation
count.set(5) # notifies subscribers, only if the value actually changed
count.update(lambda v: v + 1)
On top of it, derived values, side effects and batched writes:
from tess_ui import Computed, Effect, batch
price = Signal(10.0)
quantity = Signal(2)
total = Computed(lambda: price.get() * quantity.get()) # derived, cached
Effect(lambda: print(f"total is now {total.get()}")) # side effect only
def apply_discount():
price.update(lambda p: p * 0.9)
quantity.set(quantity.get() + 1)
batch(apply_discount) # total recomputes once, not twice
Computed(fn)-- a derived, cached value. Recomputes only when one of theSignal/Computedvaluesfnactually reads changes.Effect(fn)-- runsfnonce immediately, then again whenever something it reads changes. Side-effect-only -- has no.get()of its own to read back.Effect(fn, deferred=True)is for an effect that costs a lot and reads many signals (a window composed again): when it is told while a write or abatchis still notifying, it waits and runs once after everything that write set off has settled, instead of once per path that reached it. The write has still not returned when it runs, and it runs even when another subscriber of the same write raised. A window's own effect is deferred, so a change that touches ten signals and threefor:lists composes the window once.batch(fn)-- runsfn, collapsing everySignalwrite inside it into one notification pass at the end, instead of one pass per write. Use it when a single logical action touches severalSignals at once.untrack(fn)-- runsfnwithout recording any read-dependency, even inside aComputed/Effect/binding evaluation currently tracking reads. Use it to read aSignal's current value without making the enclosingComputed/Effectdepend on it.
Computed/Effect duck-type against Signal's own subscribe shape,
so a <| |> expression in a *_View.tdl can point straight at a
Computed with no special handling. A Signal or Computed reads as its value in a view, so <| total |> and <| total.get() |> are the same:
Text: <| total |>
A ViewModel is a class whose attributes are the names a view reads. It serves the views that name it, and
is made once, with no arguments, by app.bind:
from tess_ui import Computed, Signal, ViewModel
class CartViewModel(ViewModel):
views = "Cart"
def __init__(self):
super().__init__()
self.price = Signal(10.0)
self.quantity = Signal(2)
self.total = Computed(lambda: self.price.get() * self.quantity.get())
def add_one(self): # a handler: `handlers: {on_click: add_one}`
self.quantity.update(lambda q: q + 1)
The view that names it has Name: Cart on its root node, which must match views exactly.
See Binding Expressions for what a <| |> expression can contain.
Details worth knowing¶
- Unchanged writes don't notify.
setandupdateskip notification when the new value equals the current one. That's what stops a two-way binding recursing. Computedis eager. It recomputes as soon as a dependency changes, not when next read, and notifies only if its own value changed.- Writing a
Signalwhile it's notifying raises. If a subscriber writes back to the signal that's notifying it, directly or through other signals, you get aRuntimeErrorsaying so rather than endless recursion. Often the fix isuntrack(...)around a read that shouldn't be a dependency. - Use Tesserae's classes.
tess_ui.Signaland friends track only each other: a signal from another library read inside atess_ui.Computedisn't recorded. - Bindings use them. A bound node updates when a
SignalorComputedits<| |>expression reads changes. See Binding Expressions. - A list in a Signal is replaced, not changed in place. A
Forand a binding noticetasks.set([...])ortasks.update(lambda ts: [*ts, new]);tasks.get().append(new)changes the list without a notification, and nothing updates.