Example UsageΒΆ
First, we must include the exposed classes and functions:
from lazysignals import Signal, effect, derived
Let us define our first signal s, holding the initial value 1:
s = Signal(1)
Next, derive a signal p that checks the parity of s:
p = derived(lambda: s.value % 2 == 0)
Let us log the parity p to the console:
effect(lambda: print(f"parity:", "even" if p.value else "odd"))
Finally, perform some updates to s:
s.value = 1 # no change (s.value was 1 already), no output
s.value = 2 # output: "parity: even"
s.value = 4 # output: "parity: odd"
s.value = 5 # no change (p.value was False already), no output
s.value = 6 # output: "parity: even"
Note
We did not need to mention the dependency on s anywhere within in the effect.
Yet changes to s.value caused p.value to update and the effect to run.
Note
The effect only ran for those changes that mattered.
Whenever p.value stayed the same (because s.value did not change or its parity did not flip), no effect was run.