Type Definition ori_core::Scope

source ·
pub type Scope<'a> = BoundedScope<'a, 'a>;

Implementations§

source§

impl<'a> Scope<'a>

source

pub fn new(f: impl FnOnce(Scope<'a>) + 'a) -> ScopeDisposer<'a>

Creates a new scope.

This function returns a ScopeDisposer that must be used to dispose of the scope. If the disposer is not used, the scope will leak memory.

source

pub fn immediate(f: impl FnOnce(Scope<'a>) + 'a)

Creates a new scope and immediately disposes it.

source

pub fn child( self, f: impl for<'b> FnOnce(BoundedScope<'b, 'a>) ) -> ScopeDisposer<'a>

Creates a new child scope.

source

pub fn alloc<T: Sendable + 'static>(&self, item: T) -> &'a T

Allocates an item in the scope.

source

pub unsafe fn alloc_unsafe<T: Sendable + 'a>(self, item: T) -> &'a T

Allocates an item in the scope.

Safety
  • item must never reference any other item in the arena in it’s Drop implementation.
source

pub fn alloc_mut<T: Sendable + 'static>(&self, item: T) -> &'a mut T

Allocates an item in the scope.

source

pub unsafe fn alloc_mut_unsafe<T: Sendable + 'a>(self, item: T) -> &'a mut T

Allocates an item in the scope.

Safety
  • item must never reference any other item in the arena in it’s Drop implementation.
source

pub fn signal<T: SendSync + 'static>(self, value: T) -> &'a Signal<T>

Creates a signal in the scope.

source

pub fn untrack<T>(self, f: impl FnOnce() -> T) -> T

Runs a scope without tracking any dependencies.

source

pub fn effect(self, f: impl FnMut() + Sendable + 'a)

Creates an effect.

Effects are callbacks that are run whenever a dependency changes (eg. a signal is updated).

Examples
let signal = cx.signal(0);

cx.effect(|| {
    println!("signal is {}", signal.get()); // prints "signal is 0"
});

signal.set(1); // prints "signal is 1"
source

pub fn effect_scoped( self, f: impl for<'b> FnMut(BoundedScope<'b, 'a>) + Sendable + 'a )

Creates an effect in a child scope. See Scope::effect.

source

pub fn memo<T: SendSync + 'static>( self, f: impl FnMut() -> T + Sendable + 'a ) -> &'a ReadSignal<T>

Creates a signal that is recomputed every time a dependency is updated.

Examples
let signal = cx.signal(0);

let memo = cx.memo(|| *signal.get() * 2);

assert_eq!(*memo, 0);

signal.set(2);
assert_eq!(*memo, 4);
source

pub fn bind<T: Clone + PartialEq + SendSync + 'static>( self, signal_a: &'a Signal<T>, signal_b: &'a Signal<T> )

This will create an effect that binds two signals together. Whenever one of the signals is updated, the other will be updated to the same value. This is useful for creating two-way bindings (eg. a checkbox).

When initializing the binding, the value of signal_b will be used.

source

pub fn dynamic<T: SendSync + 'static>( self, f: impl FnMut(BoundedScope<'_, 'a>) -> T + Sendable + 'a ) -> SharedSignal<T>

Creates a shared signal that is recomputed every time a dependency is updated.