pub struct Effect<Message> { /* private fields */ }Expand description
Work the runtime performs on behalf of an update, whose result returns as
a message.
An effect is a value. It runs nothing when it is built, it cannot touch the model, and it names no executor: which thread runs a closure and what polls a future is the runtime’s, behind a boundary an application never sees. Building one in a test therefore costs nothing and needs no terminal.
use urushi_tui_app::Effect;
enum Message {
Loaded(usize),
}
let effect = Effect::perform(|| Message::Loaded(1));§Which constructor
perform carries blocking work and
future carries a future, so an application whose I/O is
already asynchronous does not wrap it in a thread and one whose work is
blocking does not reach for an executor by name. The _latest forms of each
carry a Key: starting one replaces an unfinished effect running under
the same key, which is the one staleness the runtime knows about on its own.
Every other reason a completion no longer applies is the application’s to
check in update.
after is the one-shot timer: it waits and then sends its
message, reading the runtime’s clock rather than an executor the application
brought. after_latest is the same under a key, so a
second one restarts the wait rather than adding a second timer — which is
what debouncing is. A timer that repeats is
Subscription::interval instead, because it
lives for as long as the application declares it.
batch starts several effects concurrently and promises
nothing about the order their completions arrive in. There is no sequencing
combinator: work that must follow other work is returned from the update
that receives the first completion.
Implementations§
Source§impl<Message> Effect<Message>
impl<Message> Effect<Message>
Sourcepub fn shutdown() -> Self
pub fn shutdown() -> Self
The request to stop the runtime.
The runtime reads the request from update’s return value rather than
delivering it, so it never enters admission and is not a message. On it,
no sibling effect in the same return value starts, the completions of
effects still in flight are discarded, every subscription stops, the
terminal session is restored, and the entry point returns the final
model. Work that must finish before the application exits is an ordinary
effect whose completion is the update that returns this.
Sourcepub fn perform_latest<F>(key: impl Into<Key>, work: F) -> Self
pub fn perform_latest<F>(key: impl Into<Key>, work: F) -> Self
Blocking work that replaces any unfinished work running under key.
Sourcepub fn future_latest<F>(key: impl Into<Key>, future: F) -> Self
pub fn future_latest<F>(key: impl Into<Key>, future: F) -> Self
Asynchronous work that replaces any unfinished work running under key.
Sourcepub fn after<F>(delay: Duration, f: F) -> Self
pub fn after<F>(delay: Duration, f: F) -> Self
A message sent once delay has passed, timed by the runtime’s clock.
The clock is the runtime’s, which is what keeps a waiting application free of an executor of its own and what lets a test drive the wait by hand instead of sleeping.
Sourcepub fn after_latest<F>(key: impl Into<Key>, delay: Duration, f: F) -> Self
pub fn after_latest<F>(key: impl Into<Key>, delay: Duration, f: F) -> Self
A delayed message that replaces any unfired timer under key.
Replacing a timer restarts its wait, so an update that returns this on
every keystroke fires once the keystrokes stop: the debounce a search
field or a live preview needs, with no state in the model.
Sourcepub fn batch(effects: impl IntoIterator<Item = Self>) -> Self
pub fn batch(effects: impl IntoIterator<Item = Self>) -> Self
Several effects, started concurrently.
Their completions may arrive in any order.
Sourcepub fn map<To>(
self,
f: impl Fn(Message) -> To + Send + Sync + 'static,
) -> Effect<To>
pub fn map<To>( self, f: impl Fn(Message) -> To + Send + Sync + 'static, ) -> Effect<To>
The same effect with its message passed through f.
This is what lets a program hold another program: a parent whose message
wraps a child’s calls the child’s update, receives the child’s effect,
and returns it mapped, without the child knowing the parent’s message
type.