pub struct Context { /* private fields */ }Expand description
Per-component context passed to every [Component::build] call.
Carries the component’s identity and provides access to persistent local
state via Context::state. State is keyed by (component_id, call_order)
— the hook model — so call order within build() must be stable across frames.
Implementations§
Source§impl Context
impl Context
pub fn new(id: ComponentId) -> Self
pub fn component_id(&self) -> ComponentId
Sourcepub fn state<T: Clone + Send + Sync + 'static>(&mut self, default: T) -> Atom<T>
pub fn state<T: Clone + Send + Sync + 'static>(&mut self, default: T) -> Atom<T>
Returns a persistent Atom<T> for local component state.
On first call per slot the atom is seeded with default. On subsequent
frames the existing atom is returned, preserving the last value.
Sourcepub fn state_permanent<T>(&mut self, key: &str, default: T) -> Atom<T>
pub fn state_permanent<T>(&mut self, key: &str, default: T) -> Atom<T>
D008’s permanent tier, on the hook model (D114/D121): like
Context::state, but the FIRST initialization reads key from
the installed persist backend (falling back to default if the
key is absent or its bytes are stale), and every later set
writes through — the value survives a full app restart.
key is app-global: two components using the same key share the
same stored value (by design — it’s a storage key, not a hook
slot). With no backend installed (headless tests, or before
App::launch) this behaves exactly like plain Context::state.
Uses the atom’s single on_change slot for the write-through —
see Atom::set_on_change’s doc.
Sourcepub fn on_cleanup(&mut self, f: impl FnOnce() + Send + 'static)
pub fn on_cleanup(&mut self, f: impl FnOnce() + Send + 'static)
Registers a cleanup function that runs when this component unmounts.
Stored in the persistent rosace_state::cleanup_store keyed by
component ID. The reconciler fires these callbacks when the component
disappears from the element tree.