๐ฅ stew/state
The stew/state package is the heart of Stew's reactivity engine. It implements the Signals pattern with a high-performance Auto-Tracking system.
What is a Signal?
A Signal is a data cell that "tracks" who is using it. Unlike a standard variable, a Signal can automatically notify the parts of your interface that need to be updated when it changes.
Effects (Auto-Tracking)
The state.Effect function allows you to create a logic block that reacts to state changes. You don't need to manually declare dependencies: Stew detects them during execution.
<goscript client>
import "stew/state"
import "stew/io"
count := state.New(0)
// This effect runs immediately, then every time 'count' changes.
state.Effect(func() {
Console.Log("Counter is at:", count.Get())
})
// Mutation
increment := func() {
count.Set(count.Get() + 1)
}
</goscript>
HTML Integration
The Stew compiler makes it easy to use Signals in HTML. If you use .Get() in a binding, Stew automatically creates the Effect for you.
<!-- Automatically reactive thanks to the .Get() call -->
<h1>Counter: {{ count.Get() }}</h1>
<button on:click={{ count.Set(count.Get() + 1) }}>
Increment
</button>
Thanks to Signals, Stew never re-renders your entire page. Only the exact DOM nodes that depend on a modified Signal are updated, ensuring a smooth 60 FPS even on modest mobile devices.