# State
State lives in **signals**. Views that read a signal while they build
subscribe to it, and update in place when it changes.
## Signal
```rust,no_run
use ferrokit::*;
let name = Signal::new(String::from("Ada"));
name.set("Grace".into()); // notifies readers
let len = name.with(|n| n.len()); // read without copying
let quiet = name.get_untracked(); // read without subscribing
let chars = name.with_untracked(|n| n.chars().count()); // the same, without copying
# let _ = (now, len, quiet, chars);
```
`set_if_changed` skips the notification when the value is equal.
## Handles and owners
`Signal` and `Memo` are `Copy` handles: closures capture them as they are,
with no cloning. Each belongs to the **owner** that was current when it was
made, and is freed with it:
- **A window's view tree:** signals made while its view builds, by its
effects, and by its buttons' actions (a sheet's form, say) are freed when
the window closes.
- **A `dynamic` branch or a `for_each` row:** freed when the branch is
replaced or the row removed.
- **A document:** its content, path and edited state go with it.
Signals made outside any owner, such as app state created in `main`, live as
long as the app. Reading a freed signal panics with a message saying so;
setting it does nothing, and `try_get` and `is_alive` check first.
`live_signals()` counts the signals in existence, for leak checks.
## Values in views: fixed, or from signals
What a view shows takes either a value or a closure that reads signals:
`text("Total")` or `text(move || format!("{} left", left.get()))`, and the
same for `.color(..)`, `.background(..)`, `.opacity(..)`, `.disabled(..)`, `.hidden(..)`,
a button's title and symbol, and a menu item's title. A closure runs again
when a signal it read changes, and only that view updates. (These take
`impl IntoValue<T>`.)
```rust,no_run
use ferrokit::*;
let done = Signal::new(false);
let row = text("Boots")
.padding(8.0)
.background(move || if done.get() { Color::Green.opacity(0.18) } else { Color::Clear })
.opacity(move || if done.get() { 0.5 } else { 1.0 });
```
## Memo: derived state
A memo recomputes when its inputs change, and notifies its own readers only
when its value actually changes.
```rust,no_run
use ferrokit::*;
let items = Signal::new(vec![3, 1, 4, 1, 5]);
# let _ = view;
```
## Effect: reacting to changes
An effect runs now, then again whenever a signal it read changes. It stops
when its handle is dropped, so keep it.
```rust,no_run
use ferrokit::*;
let theme = Signal::new(Appearance::System);
let _keep = Effect::new(move || set_appearance(theme.get()));
```
Inside a view tree, `watch(…)` does the same and lives as long as its place
in the tree.
## Batching
Event handlers (button actions, field edits, menu items) already run in a
**batch**: effects and layout run once at the end, however many signals
changed. Use `batch` yourself when you change several signals from
elsewhere:
```rust,no_run
use ferrokit::*;
let (first, last) = (Signal::new(String::new()), Signal::new(String::new()));
last.set("Lovelace".into());
});
```
`untrack(|| …)` reads signals without subscribing the running effect.
## Background work
`spawn(work, then)` runs `work` on a background thread and `then` with its
result on the main thread, inside a batch.
```rust,no_run
use ferrokit::*;
let rows = Signal::new(Vec::<String>::new());
spawn(
|| std::fs::read_to_string("/etc/hosts").unwrap_or_default().lines().map(String::from).collect::<Vec<_>>(),
move |lines| rows.set(lines),
);
```
`later(f)` runs `f` on the next pass of the main run loop.
## Async tasks
`spawn_local(future)` runs a future on the main thread. Between its
`.await`s it can read and set signals as an action does. Each poll is a
batch. Inside a task:
- `background(|| …).await` runs blocking work on another thread (it's
`spawn` as a future);
- `sleep(duration).await` pauses without blocking anything.
```rust,no_run
use ferrokit::*;
use std::time::Duration;
let status = Signal::new(String::new());
status.set("Saving…".into());
let written = background(|| std::fs::write("/tmp/notes.txt", "…")).await;
status.set(match written {
Ok(()) => "Saved".into(),
Err(e) => format!("Couldn't save: {e}"),
});
sleep(Duration::from_secs(2)).await;
status.set(String::new());
});
});
# let _ = save;
```
- A task belongs to the owner current when it starts, as a signal does.
Closing its window, or removing its view, drops the future: the task stops
at the `.await` it's waiting at. `spawn_local` returns a `Task`:
`cancel()` stops it, `detach()` keeps it running after its owner goes.
- Tasks keep running while a menu is open or a window is being resized.
- A panic in a task, or in `background` work it awaits, is reported (see
`App::on_error`) and ends that task only.
- Ferrokit has no I/O runtime. Libraries such as reqwest and sqlx need
their own runtime (tokio). Keep one in your app, spawn the I/O on it, and
await the join handle from the task:
```rust,ignore
let rt = tokio::runtime::Runtime::new()?; // kept for the app's lifetime
let handle = rt.handle().clone();
spawn_local(async move {
match handle.spawn(async move { reqwest::get(url).await?.text().await }).await {
Ok(Ok(body)) => page.set(body),
Ok(Err(e)) => error.set(Some(e.to_string())),
Err(e) => error.set(Some(e.to_string())), // the runtime task panicked
}
});
```
Don't block inside a task: a poll runs on the main thread, and the UI waits
for it.
## Loading data: `Resource`
A resource loads a value on a background thread from the signals it reads,
and loads again when they change. It's made for search-as-you-type:
- Starting a new load cancels the one running. Its result is dropped, so an
older query's rows never replace a newer one's.
- The last value stays readable while the next loads, so a table doesn't
flash empty between keystrokes.
```rust,no_run
use ferrokit::*;
let query = Signal::new(String::new());
for i in 0..1_000_000u32 {
if i % 10_000 == 0 && cancel.is_cancelled() {
break; // the user typed again; this result won't be shown
}
if i.to_string().contains(&q) {
rows.push(i);
}
}
Ok::<_, String>(rows)
});
let view = vstack((
search_field(&query),
text(move || match results.state() {
Load::Loading => "Searching…".into(),
Load::Ready(rows) => format!("{} matches", rows.len()),
Load::Failed(error) => format!("Search failed: {error}"),
}),
));
# let _ = view;
```
- `get()` is the latest value (`None` before the first load); `loading()`
is whether a load is running; `error()` is the latest failure's text.
`state()` combines them.
- An error keeps the last value: `get()` still returns it.
- `refetch()` loads again with the same inputs.
- A resource is owned like a signal: closing its window cancels its load.
## Stored settings
`stored(key, default)` is a signal backed by the user's defaults: it starts
from the saved value and saves every change.
```rust,no_run
use ferrokit::*;
let sounds = stored("play sounds", true);
let view = checkbox("Play sounds", &sounds);
# let _ = view;
```