pub struct ReactiveScope { /* private fields */ }Expand description
A scope that owns reactive effects and cleanup callbacks.
When a scope is dropped, all effects it owns are disposed and all cleanup callbacks are run in reverse registration order. Child scopes are dropped before their parent’s cleanups run.
§Examples
let mut scope = ReactiveScope::new();
let count = Signal::new(0);
scope.create_effect({
let count = count.clone();
move || println!("count = {}", count.get())
});
// Effect runs on creation and when count changes.
count.set(1);
drop(scope); // Effect is disposed, cleanup runs.
count.set(2); // Effect does NOT run.Implementations§
Source§impl ReactiveScope
impl ReactiveScope
Sourcepub fn create_signal<T>(&self, value: T) -> Signal<T>
pub fn create_signal<T>(&self, value: T) -> Signal<T>
Create a signal. Convenience method — signals are not owned by the scope since they are shared handles.
Sourcepub fn create_computed<T: Clone + 'static>(
&self,
f: impl Fn() -> T + 'static,
) -> Computed<T>
pub fn create_computed<T: Clone + 'static>( &self, f: impl Fn() -> T + 'static, ) -> Computed<T>
Create a computed value. Convenience method — computed values are shared handles and not owned by the scope.
Sourcepub fn create_effect(&mut self, f: impl FnMut() + 'static) -> Effect
pub fn create_effect(&mut self, f: impl FnMut() + 'static) -> Effect
Create an effect owned by this scope.
The effect will be automatically disposed when the scope is dropped.
Sourcepub fn on_cleanup(&mut self, f: impl FnOnce() + 'static)
pub fn on_cleanup(&mut self, f: impl FnOnce() + 'static)
Register a cleanup callback to run when this scope is dropped.
Callbacks are run in reverse registration order.
Sourcepub fn child(&mut self) -> &mut ReactiveScope
pub fn child(&mut self) -> &mut ReactiveScope
Create a nested child scope.
The child scope will be dropped before the parent’s cleanups run.
Sourcepub fn effect_count(&self) -> usize
pub fn effect_count(&self) -> usize
Get the number of effects owned by this scope.
Sourcepub fn child_count(&self) -> usize
pub fn child_count(&self) -> usize
Get the number of child scopes.