skeleton of use_server_future with reactivity

This commit is contained in:
Jonathan Kelley committed 2024-02-01 13:21:52 -08:00
1 parent b3ed337b6b
commit 0c71b95e82
11 files changed
+200 -193

No files matched your search

+70 -120
View File
@@ -1,6 +1,5 @@
use dioxus_lib::prelude::*;
use serde::{de::DeserializeOwned, Serialize};
// use std::any::Any;
use std::cell::Cell;
use std::cell::Ref;
use std::cell::RefCell;
@@ -11,138 +10,89 @@ use std::sync::Arc;
/// A future that resolves to a value.
///
/// This runs the future only once - though the future may be regenerated
/// through the [`UseServerFuture::restart`] method.
///
/// This is commonly used for components that cannot be rendered until some
/// asynchronous operation has completed.
///
/// Whenever the hooks dependencies change, the future will be re-evaluated.
/// If a future is pending when the dependencies change, the previous future
/// will be allowed to continue
/// ```rust
/// fn User(id: String) -> Element {
/// let data = use_sever_future(move || fetch_user(id)).suspend()?;
///
/// - dependencies: a tuple of references to values that are PartialEq + Clone
///
/// }
///
/// ```
#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
pub fn use_server_future<T, F>(_future: impl FnOnce() -> F) -> Option<UseServerFuture<T>>
pub fn use_server_future<T, F>(_future: impl Fn() -> F) -> UseServerFuture<T>
where
T: 'static + Serialize + DeserializeOwned + Debug,
T: Serialize + DeserializeOwned + 'static,
F: Future<Output = T> + 'static,
{
let value: Signal<Option<T>> = use_signal(|| {
// Doesn't this need to be keyed by something?
// We should try and link these IDs across the server and client
// Just the file/line/col span should be fine (or byte index)
#[cfg(feature = "ssr")]
return crate::html_storage::deserialize::take_server_data::<T>();
#[cfg(not(feature = "ssr"))]
return None;
});
// Run the callback regardless, giving us the future without actually polling it
// This is where use_server_future gets its reactivity from
// If the client is using signals to drive the future itself, (say, via args to the server_fn), then we need to know
// what signals are being used
use_future(move || async move {
// watch the reactive context
// if it changes, restart the future
//
// if let Err(err) = crate::prelude::server_context().push_html_data(&data) {
// tracing::error!("Failed to push HTML data: {}", err);
// };
});
// if there's no value ready, mark this component as suspended and return early
if value.peek().is_none() {
suspend();
}
todo!()
// let state = use_hook(move || UseServerFuture {
// update: schedule_update(),
// needs_regen: Cell::new(true),
// value: Default::default(),
// task: Cell::new(None),
// dependencies: Vec::new(),
// });
// let first_run = { state.value.borrow().as_ref().is_none() && state.task.get().is_none() };
// #[cfg(not(feature = "ssr"))]
// {
// if first_run {
// match crate::html_storage::deserialize::take_server_data() {
// Some(data) => {
// tracing::trace!("Loaded {data:?} from server");
// *state.value.borrow_mut() = Some(Box::new(data));
// state.needs_regen.set(false);
// return Some(state);
// }
// None => {
// tracing::trace!("Failed to load from server... running future");
// }
// };
// }
// }
// if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() {
// // We don't need regen anymore
// state.needs_regen.set(false);
// // Create the new future
// let fut = future(dependencies.out());
// // Clone in our cells
// let value = state.value.clone();
// let schedule_update = state.update.clone();
// // Cancel the current future
// if let Some(current) = state.task.take() {
// remove_future(current);
// }
// state.task.set(Some(push_future(async move {
// let data;
// #[cfg(feature = "ssr")]
// {
// data = fut.await;
// if first_run {
// if let Err(err) = crate::prelude::server_context().push_html_data(&data) {
// tracing::error!("Failed to push HTML data: {}", err);
// };
// }
// }
// #[cfg(not(feature = "ssr"))]
// {
// data = fut.await;
// }
// *value.borrow_mut() = Some(Box::new(data));
// schedule_update();
// })));
// }
// if first_run {
// #[cfg(feature = "ssr")]
// {
// tracing::trace!("Suspending first run of use_server_future");
// cx.suspend();
// }
// None
// } else {
// Some(state)
// }
}
pub struct UseServerFuture<T> {
update: Arc<dyn Fn()>,
needs_regen: Cell<bool>,
task: Cell<Option<Task>>,
value: Rc<RefCell<Option<Box<T>>>>,
pub struct UseServerFuture<T: 'static> {
value: Signal<Option<Signal<T>>>,
}
impl<T> UseServerFuture<T> {
/// Restart the future with new dependencies.
///
/// Will not cancel the previous future, but will ignore any values that it
/// generates.
pub fn restart(&self) {
self.needs_regen.set(true);
(self.update)();
}
// impl<T> UseServerFuture<T> {
// /// Restart the future with new dependencies.
// ///
// /// Will not cancel the previous future, but will ignore any values that it
// /// generates.
// pub fn restart(&self) {
// self.needs_regen.set(true);
// (self.update)();
// }
/// Forcefully cancel a future
pub fn cancel(&self) {
if let Some(task) = self.task.take() {
remove_future(task);
}
}
// /// Forcefully cancel a future
// pub fn cancel(&self) {
// if let Some(task) = self.task.take() {
// remove_future(task);
// }
// }
/// Return any value, even old values if the future has not yet resolved.
///
/// If the future has never completed, the returned value will be `None`.
pub fn value(&self) -> Ref<'_, T> {
Ref::map(self.value.borrow(), |v| v.as_deref().unwrap())
}
// /// Return any value, even old values if the future has not yet resolved.
// ///
// /// If the future has never completed, the returned value will be `None`.
// pub fn value(&self) -> Ref<'_, T> {
// Ref::map(self.value.borrow(), |v| v.as_deref().unwrap())
// }
/// Get the ID of the future in Dioxus' internal scheduler
pub fn task(&self) -> Option<Task> {
self.task.get()
}
// /// Get the ID of the future in Dioxus' internal scheduler
// pub fn task(&self) -> Option<Task> {
// self.task.get()
// }
/// Get the current state of the future.
pub fn reloading(&self) -> bool {
self.task.get().is_some()
}
}
// /// Get the current state of the future.
// pub fn reloading(&self) -> bool {
// self.task.get().is_some()
// }
// }