Skip to main content

Resource

Struct Resource 

Source
pub struct Resource<T: 'static, E: 'static, K: 'static = ()> { /* private fields */ }
Expand description

Executor-agnostic asynchronous reactive resource driven by a reactive source signal.

Tracks a monotonic u64 request generation so stale or out-of-order completions from superseded requests are automatically ignored, and Self::drive drops in-flight futures whenever source changes or Self::refetch is called.

Implementations§

Source§

impl<T: PartialEq + 'static, E: PartialEq + 'static> Resource<T, E, ()>

Source

pub fn stateless(scope: &Scope) -> Self

Creates a keyless Resource<T, E> triggered only on initial drive and Self::refetch.

Source§

impl<T: PartialEq + 'static, E: PartialEq + 'static, K: 'static> Resource<T, E, K>

Source

pub fn new(scope: &Scope, source: impl Into<Signal<K>>) -> Self

Creates a new Resource starting in ResourceState::Pending and keyed by source.

Source

pub fn with_initial( scope: &Scope, source: impl Into<Signal<K>>, initial: T, ) -> Self

Creates a new Resource pre-populated in ResourceState::Ready.

Source

pub const fn state(&self) -> Atom<ResourceState<T, E>>

Returns the reactive Atom<ResourceState<T, E>> holding the current lifecycle state.

Source

pub fn generation(&self) -> u64

Returns the monotonic request generation counter.

Source

pub fn latest(&self) -> Option<T>
where T: Clone,

Returns the latest known good value, if any.

Source

pub fn is_pending(&self) -> bool

Returns true when the resource is currently Pending or Refreshing.

Source

pub fn refetch(&self)

Requests a reload from any active Self::drive loop without changing source.

Source

pub fn mutate(&self, value: T)

Optimistically or imperatively sets the resource to ResourceState::Ready and advances the generation counter so any currently in-flight request is ignored upon completion.

Source

pub fn dispose(&self)

Transitions the resource to ResourceState::Disposed, cancels any active Self::drive loop, and invalidates all in-flight requests. Safe to call repeatedly.

Source

pub async fn load<F, Fut>(&self, loader: F)
where T: Clone, K: Clone, F: FnOnce(K) -> Fut, Fut: Future<Output = Result<T, E>>,

Runs a single generation-guarded load for the current source value.

If another Self::load, Self::mutate, Self::refetch, or Self::dispose advances the generation before loader resolves, this completion is discarded.

Source

pub async fn drive<F, Fut>(&self, loader: F)
where T: Clone, K: Clone + PartialEq, F: FnMut(K) -> Fut, Fut: Future<Output = Result<T, E>>,

Continuously drives this resource on any async executor using Scope::changes.

Loads immediately for the initial source value, and whenever source changes or Self::refetch is called, drops any still-pending future (cancelling it) and starts a fresh load. Ends cleanly when Self::dispose is called or source is disposed.

Trait Implementations§

Source§

impl<T: 'static, E: 'static, K: 'static> Clone for Resource<T, E, K>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: 'static, E: 'static, K: 'static> Debug for Resource<T, E, K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, E, K = ()> !RefUnwindSafe for Resource<T, E, K>

§

impl<T, E, K = ()> !Send for Resource<T, E, K>

§

impl<T, E, K = ()> !Sync for Resource<T, E, K>

§

impl<T, E, K = ()> !UnwindSafe for Resource<T, E, K>

§

impl<T, E, K> Forget for Resource<T, E, K>

§

impl<T, E, K> Freeze for Resource<T, E, K>
where *const RcInner<RefCell<ScopeInner>> is !null: Freeze, PhantomData<RcInner<RefCell<ScopeInner>>>: Freeze, PhantomData<fn() -> K>: Freeze, PhantomData<fn() -> ResourceState<T, E>>: Freeze, PhantomData<fn() -> u64>: Freeze, *const RcInner<Cell<u64>> is !null: Freeze, PhantomData<RcInner<Cell<u64>>>: Freeze, *const RcInner<Cell<bool>> is !null: Freeze, PhantomData<RcInner<Cell<bool>>>: Freeze,

§

impl<T, E, K> Move for Resource<T, E, K>

§

impl<T, E, K> Unpin for Resource<T, E, K>
where Rc<RefCell<ScopeInner>>: Unpin, Rc<Cell<u64>>: Unpin, Rc<Cell<bool>>: Unpin,

§

impl<T, E, K> UnsafeUnpin for Resource<T, E, K>
where *const RcInner<RefCell<ScopeInner>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<ScopeInner>>>: UnsafeUnpin, PhantomData<fn() -> K>: UnsafeUnpin, PhantomData<fn() -> ResourceState<T, E>>: UnsafeUnpin, PhantomData<fn() -> u64>: UnsafeUnpin, *const RcInner<Cell<u64>> is !null: UnsafeUnpin, PhantomData<RcInner<Cell<u64>>>: UnsafeUnpin, *const RcInner<Cell<bool>> is !null: UnsafeUnpin, PhantomData<RcInner<Cell<bool>>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.