Skip to main content

Resource

Struct Resource 

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

异步加载的数据:依赖变了自动重新加载,加载中、数据、错误都是响应式的,直接绑定到界面上。

let id = signal(1);
let user = resource(move || id.get(), |id| async move { fetch_user(id).await });
text(move || match (user.get(), user.error()) {
    (_, Some(error)) => format!("出错了:{error}"),
    (Some(name), _) => name,
    (None, _) => "加载中…".into(),
})
.opacity(move || if user.is_loading() { 0.5 } else { 1.0 });

和 resource 一起看:

  • source 在副作用里执行,读到的信号就是依赖;依赖变了就用新值调用 fetcher。
  • fetcher 在主线程调用(不追踪依赖),返回的 future 在后台线程池上运行(必须是 Send),结果交回主线程。
  • 竞态安全:依赖变化或 Resource::refetch 时,上一次还没完成的请求被取消(future 被丢弃), 晚到的旧结果不会覆盖新结果。
  • 加载新数据期间 Resource::get 仍是上一次成功的结果(界面不闪),用 Resource::is_loading 显示加载状态; 失败时 Resource::error 有值,数据保留上一次成功的;之后成功时清掉错误。
  • 归创建时的作用域所有:作用域释放(比如元素卸载)时取消进行中的请求。
  • fetcher 返回的 future panic 时只结束加载状态(panic 信息打印在终端),不影响界面

Implementations§

Source§

impl<T, E> Resource<T, E>
where T: 'static, E: 'static,

Source

pub fn is_loading(&self) -> bool

正在加载(第一次或重新加载)

Source

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

最近一次成功加载的数据;还没成功过时是 None

Source

pub fn with<R>(&self, f: impl FnOnce(Option<&T>) -> R) -> R

借用读取数据

Source

pub fn error(&self) -> Option<E>
where E: Clone,

最近一次加载的错误;之后加载成功时清掉

Source

pub fn with_error<R>(&self, f: impl FnOnce(Option<&E>) -> R) -> R

借用读取错误

Source

pub fn refetch(&self)

用同样的依赖重新加载(比如“刷新“按钮)

Source

pub fn set(&self, value: T)

直接改数据(比如乐观更新),不发请求

Trait Implementations§

Source§

impl<T, E> Clone for Resource<T, E>

Source§

fn clone(&self) -> Resource<T, E>

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, E> Copy for Resource<T, E>

Source§

impl<T, E> Debug for Resource<T, E>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, E = String> !Send for Resource<T, E>

§

impl<T, E = String> !Sync for Resource<T, E>

§

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

§

impl<T, E> Freeze for Resource<T, E>

§

impl<T, E> RefUnwindSafe for Resource<T, E>

§

impl<T, E> Unpin for Resource<T, E>
where Signal<Option<T>>: Unpin, Signal<Option<E>>: Unpin,

§

impl<T, E> UnsafeUnpin for Resource<T, E>

§

impl<T, E> UnwindSafe for Resource<T, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more