Skip to main content

Module runtime

Module runtime 

Source
Expand description

compose-rust’s own runtime: positional memoisation on a slot table, with Compose’s shape and Compose’s names.

ⓘ
use compose_rust::runtime::*;
use compose_rust::ui::*;

#[composable]
fn Counter() {
    let count = remember(|| mutable_state_of(0));
    Column().content(|| {
        Text(format!("Count: {}", count.get()));
        let more = count.clone();
        Button("Increment").on_click(move || more.update(|count| *count += 1));
    });
}

fn main() {
    compose_rust::ui::application(Counter);
}

Nothing is compared to find out what changed. A state remembers which scopes read it; writing it marks those scopes invalid, and the next frame runs exactly them again. Inside a scope that runs, a composable whose parameters equal the ones it last had is skipped, its slots and nodes carried over untouched. What a widget writes is compared with what it wrote last time, one attribute at a time, and only an attribute that changed becomes a record.

Positional memoisation. remember belongs to its call site: the group path the #[composable] macro builds around every function and every branch, and the order of calls inside each group. A branch that stops being taken takes what it remembered with it; a loop that gets shorter drops the iterations it lost. Where order is not identity, because items move, key says which item is which.

One thread. Composition, effects and event handlers run on the Renderer’s UI thread, inside a Host call. Other threads write MutableStates and wake futures; both queue an id and ask for a frame, and the UI thread does the rest.

Structs§

Batch
One batch, written by the runtime and by the Host into the same arena.
ComposeHost
The Host of a composable application: compose-rust’s Host driving a Recomposer.
CoroutineScope
A place to start futures from an event handler, whose futures are cancelled when the call site that remembered it leaves the composition. Compose’s CoroutineScope.
DerivedState
A value computed from other states, which tells its readers only when the result changes. Compose’s derivedStateOf.
DisposableEffectResult
What a disposable_effect leaves behind: the cleanup to run when it leaves.
GroupGuard
Holds a group open for the rest of a block. The group closes on every way out of the block, return, ?, break and an unwind included, because it closes in Drop.
MutableState
A value that composition observes. Compose’s MutableState.
RecomposeScope
The scope composing now, to be told to run again by hand. Compose’s currentRecomposeScope.
Recomposer
The slot table runtime for one root: what composes the tree and runs its handlers.
SnapshotMutationPolicy
When a write counts as a change. Compose calls this a SnapshotMutationPolicy.

Traits§

Runtime
What builds and changes the tree, seen from the boundary.
State
Something composition can read. Compose’s State.

Functions§

application
The same as launch, under Compose Desktop’s name for it.
current_design_system
The design system the Renderer resolved the theme to, recorded as read.
current_notification_permission
Whether notifications may be shown, recorded as read.
current_recompose_scope
The innermost scope composing now.
current_window_size
The window’s size, recorded as read: the composable runs again when the Renderer reports a different size class.
delay
Resolves after duration. Compose’s delay.
derived_state_of
A state computed from the states calculation reads.
disposable_effect
Runs effect after this call site enters the composition and again whenever key changes, and runs the cleanup it returned before the next run and when the call site leaves. Compose’s DisposableEffect.
key
Gives content an identity of its own, made from key rather than from its position. Compose’s key.
launch
Runs a composable as the application, with every launch choice left to its default.
launched_effect
Runs block’s future when this call site enters the composition, and again, after cancelling the last one, whenever key changes. Cancelled when the call site leaves. Compose’s LaunchedEffect.
mutable_state_of
A state whose writes of an equal value change nothing. Compose’s mutableStateOf.
mutable_state_of_with_policy
A state with a policy of its own for what counts as a change.
never_equal_policy
Every write is a change, whatever it wrote.
on_dispose
The cleanup a disposable_effect returns. Compose’s onDispose.
on_notification_activated
Calls handler when the user presses a notification this application posted, for as long as this call site is in the composition.
recomposer_for
The runtime factory for one composable root: what crate::LaunchBuilder::launch_runtime and the browser’s start take.
remember
The value this call site remembered, or init()’s, kept for next time. Compose’s remember.
remember_coroutine_scope
A coroutine scope bound to this call site. Compose’s rememberCoroutineScope.
remember_node_size
Follows one node’s measured size, recorded as read. Compose has no single equivalent; it is onSizeChanged with the class judgement made for you.
remember_with_key
Like remember, but computed again whenever key changes. Compose’s remember(key) { }.
side_effect
Runs effect after every composition of the scope it is in that is applied. Compose’s SideEffect: how a composable publishes something to an object the composition does not manage.
structural_equality_policy
A write of an equal value changes nothing and runs nothing again.
with_frame_nanos
Resolves on the next frame with that frame’s time, in nanoseconds. Compose’s withFrameNanos, the clock animations run on.
with_worker
Runs work on a worker thread and resolves with its result on the UI thread. Compose’s withContext(Dispatchers.IO): the way domain work leaves the UI thread.

Attribute Macros§

composable
Turns a function into a composable.