more fullstack cleanups: loaders serialize, ServeConfig only (#4785)

* some opinionated cleanups in dioxus-server

* add serialize to loader

* add `result` to Resource

* clippy

* fix

* writable memos, resources, `onmount`, `.pending`, and `.value()` on actions

* fix compile issues

* fix typo
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2025-10-14 17:34:52 -07:00
1 parent a3e40a5ea2
commit 7102bc3b6a
33 files changed
+2085 -672

No files matched your search

+48 -1
View File
@@ -11,8 +11,8 @@ use futures_util::{
future::{self},
pin_mut, FutureExt, StreamExt,
};
use std::ops::Deref;
use std::{cell::Cell, future::Future, rc::Rc};
use std::{fmt::Debug, ops::Deref};
#[doc = include_str!("../docs/use_resource.md")]
#[doc = include_str!("../docs/rules_of_hooks.md")]
@@ -324,6 +324,11 @@ impl<T> Resource<T> {
self.task.cloned()
}
/// Is the resource's future currently running?
pub fn pending(&self) -> bool {
matches!(*self.state.peek(), UseResourceState::Pending)
}
/// Is the resource's future currently finished running?
///
/// Reading this does not subscribe to the future's state
@@ -439,6 +444,35 @@ impl<T> Resource<T> {
}
}
impl<T, E> Resource<Result<T, E>> {
/// Convert the `Resource<Result<T, E>>` into an `Option<Result<MappedSignal<T>, MappedSignal<E>>>`
#[allow(clippy::type_complexity)]
pub fn result(
&self,
) -> Option<
Result<
MappedSignal<T, Signal<Option<Result<T, E>>>>,
MappedSignal<E, Signal<Option<Result<T, E>>>>,
>,
> {
let value: MappedSignal<T, Signal<Option<Result<T, E>>>> = self.value.map(|v| match v {
Some(Ok(ref res)) => res,
_ => panic!("Resource is not ready"),
});
let error: MappedSignal<E, Signal<Option<Result<T, E>>>> = self.value.map(|v| match v {
Some(Err(ref err)) => err,
_ => panic!("Resource is not ready"),
});
match &*self.value.peek() {
Some(Ok(_)) => Some(Ok(value)),
Some(Err(_)) => Some(Err(error)),
None => None,
}
}
}
impl<T> From<Resource<T>> for ReadSignal<Option<T>> {
fn from(val: Resource<T>) -> Self {
val.value.into()
@@ -468,6 +502,19 @@ impl<T> Readable for Resource<T> {
}
}
impl<T> Writable for Resource<T> {
type WriteMetadata = <Signal<Option<T>> as Writable>::WriteMetadata;
fn try_write_unchecked(
&self,
) -> Result<WritableRef<'static, Self>, generational_box::BorrowMutError>
where
Self::Target: 'static,
{
self.value.try_write_unchecked()
}
}
impl<T> IntoAttributeValue for Resource<T>
where
T: Clone + IntoAttributeValue,