Make Resource Ready/Pending State Explicit
Feature Request
Background
It would be great if the explicitness of Resource<T> ready/pending was built in to the type.
Locally I use Pending as a type alias for Option when the resource has not finished yet
/// A type alias for the state of a resource
pub type Pending<T> = Option<T>;
I think it makes things more clear. Since
let resource: Resource<Option<i32>> = use_resource(move |_| async move {
todo!()
});
// rather than
let read: GenerationalRef<Ref<'_, Option<Option<i32>>>> = resource.read();
// It becomes
let read: GenerationalRef<Ref<'_, Pending<Option<i32>>>> = resource.read();
match &*read {
Pending::Some(_) => todo!(),
Pending::None => todo!(),
}
But from an IDE perspective, this currently does not make code actions more clear, since the rust-analyzer looks through type aliases, which is a known issue - https://github.com/rust-lang/rust-analyzer/issues/1666
Solution
Instead of using Option or a type alias here, we could do better with something like Poll. That would it would look like
let read: GenerationalRef<Ref<'_, Poll<Option<i32>>>> = resource.read();
match &*read {
Poll::Ready(_) => todo!(),
Poll::Pending => todo!(),
}
We would probably want to use our own type rather than the std libs Poll though. As our own type would be more flexible. Poll is a good name, but it would likely cause confusion with how Poll is used with Futures, which is not what is happening behind the scenes here. If we choose a different name, we should avoid a name like ResourceValue or ResourceState, so we can use it more generally. Something like AsyncValue may be more appropriate. But whatever the name is, the variants Ready/Pending are probably correct choice.
Related
Likely related to https://github.com/DioxusLabs/dioxus/pull/4846
This would be very nice with https://github.com/DioxusLabs/dioxus/pull/4846 if we could implement an async readable/store extension for methods like read_async added in #5088 so that method is available on anything that implements Readable<Target = Pending<T>>