skeleton of use_server_future with reactivity
This commit is contained in:
11 files changed
+200
-193
No files matched your search
@@ -127,7 +127,7 @@ impl<T> Coroutine<T> {
|
||||
/// Forces the component to re-render, which will re-invoke the coroutine.
|
||||
pub fn restart(&mut self) {
|
||||
self.needs_regen.set(true);
|
||||
self.task().stop();
|
||||
self.task().cancel();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#![allow(missing_docs)]
|
||||
use crate::{use_callback, use_hook_did_run, use_signal, UseCallback};
|
||||
use dioxus_core::{
|
||||
prelude::{flush_sync, spawn, use_drop, use_hook},
|
||||
prelude::{flush_sync, spawn, use_hook},
|
||||
Task,
|
||||
};
|
||||
use dioxus_signals::*;
|
||||
@@ -16,7 +16,7 @@ pub fn use_future<F>(mut future: impl FnMut() -> F + 'static) -> UseFuture
|
||||
where
|
||||
F: Future + 'static,
|
||||
{
|
||||
let mut complete = use_signal(|| UseFutureState::Pending);
|
||||
let mut state = use_signal(|| UseFutureState::Pending);
|
||||
|
||||
let mut callback = use_callback(move || {
|
||||
let fut = future();
|
||||
@@ -26,9 +26,9 @@ where
|
||||
// The point here is to not run use_future on the server... which like, shouldn't we?
|
||||
flush_sync().await;
|
||||
|
||||
complete.set(UseFutureState::Pending);
|
||||
state.set(UseFutureState::Pending);
|
||||
fut.await;
|
||||
complete.set(UseFutureState::Complete);
|
||||
state.set(UseFutureState::Complete);
|
||||
})
|
||||
});
|
||||
|
||||
@@ -46,11 +46,9 @@ where
|
||||
false => task.peek().pause(),
|
||||
});
|
||||
|
||||
use_drop(move || task.peek().stop());
|
||||
|
||||
UseFuture {
|
||||
task,
|
||||
state: complete,
|
||||
state,
|
||||
callback,
|
||||
}
|
||||
}
|
||||
@@ -62,13 +60,30 @@ pub struct UseFuture {
|
||||
callback: UseCallback<Task>,
|
||||
}
|
||||
|
||||
/// A signal that represents the state of a future
|
||||
// we might add more states (panicked, etc)
|
||||
#[derive(Clone, Copy, PartialEq, Hash, Eq, Debug)]
|
||||
pub enum UseFutureState {
|
||||
/// The future is still running
|
||||
Pending,
|
||||
|
||||
/// The future has been forcefully stopped
|
||||
Stopped,
|
||||
|
||||
/// The future has been paused, tempoarily
|
||||
Paused,
|
||||
|
||||
/// The future has completed
|
||||
Complete,
|
||||
}
|
||||
|
||||
impl UseFuture {
|
||||
/// Restart the future with new dependencies.
|
||||
///
|
||||
/// Will not cancel the previous future, but will ignore any values that it
|
||||
/// generates.
|
||||
pub fn restart(&mut self) {
|
||||
self.task.write().stop();
|
||||
self.task.write().cancel();
|
||||
let new_task = self.callback.call();
|
||||
self.task.set(new_task);
|
||||
}
|
||||
@@ -76,7 +91,7 @@ impl UseFuture {
|
||||
/// Forcefully cancel a future
|
||||
pub fn cancel(&mut self) {
|
||||
self.state.set(UseFutureState::Stopped);
|
||||
self.task.write().stop();
|
||||
self.task.write().cancel();
|
||||
}
|
||||
|
||||
/// Pause the future
|
||||
@@ -101,9 +116,14 @@ impl UseFuture {
|
||||
self.task.cloned()
|
||||
}
|
||||
|
||||
/// Get the current state of the future.
|
||||
/// Is the future currently finished running?
|
||||
///
|
||||
/// Reading this does not subscribe to the future's state
|
||||
pub fn finished(&self) -> bool {
|
||||
matches!(self.state.peek().clone(), UseFutureState::Complete)
|
||||
matches!(
|
||||
self.state.peek().clone(),
|
||||
UseFutureState::Complete | UseFutureState::Stopped
|
||||
)
|
||||
}
|
||||
|
||||
/// Get the current state of the future.
|
||||
@@ -111,20 +131,3 @@ impl UseFuture {
|
||||
self.state.clone().into()
|
||||
}
|
||||
}
|
||||
|
||||
/// A signal that represents the state of a future
|
||||
// we might add more states (panicked, etc)
|
||||
#[derive(Clone, Copy, PartialEq, Hash, Eq, Debug)]
|
||||
pub enum UseFutureState {
|
||||
/// The future is still running
|
||||
Pending,
|
||||
|
||||
/// The future has been forcefully stopped
|
||||
Stopped,
|
||||
|
||||
/// The future has been paused, tempoarily
|
||||
Paused,
|
||||
|
||||
/// The future has completed
|
||||
Complete,
|
||||
}
|
||||
@@ -34,7 +34,7 @@ pub fn use_memo<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<
|
||||
/// use dioxus::prelude::*;
|
||||
/// use dioxus_signals::*;
|
||||
///
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// fn App() -> Element {
|
||||
/// let mut count = use_signal(cx, || 0);
|
||||
/// let double = use_memo(cx, move || count * 2);
|
||||
/// count += 1;
|
||||
@@ -56,10 +56,9 @@ pub fn use_maybe_sync_memo<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
///
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
/// use dioxus_signals::*;
|
||||
///
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// let mut local_state = use_state(cx, || 0);
|
||||
/// fn App() -> Element {
|
||||
/// let mut local_state = use_state(|| 0);
|
||||
/// let double = use_memo_with_dependencies(cx, (local_state.get(),), move |(local_state,)| local_state * 2);
|
||||
/// local_state.set(1);
|
||||
///
|
||||
@@ -85,8 +84,8 @@ where
|
||||
/// use dioxus::prelude::*;
|
||||
/// use dioxus_signals::*;
|
||||
///
|
||||
/// fn App(cx: Scope) -> Element {
|
||||
/// let mut local_state = use_state(cx, || 0);
|
||||
/// fn App() -> Element {
|
||||
/// let mut local_state = use_state(|| 0);
|
||||
/// let double = use_memo_with_dependencies(cx, (local_state.get(),), move |(local_state,)| local_state * 2);
|
||||
/// local_state.set(1);
|
||||
///
|
||||
|
||||
@@ -1,25 +1,19 @@
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use crate::use_signal;
|
||||
use dioxus_core::{prelude::spawn, Task};
|
||||
use dioxus_core::{
|
||||
prelude::{spawn, suspend},
|
||||
Task,
|
||||
};
|
||||
use dioxus_signals::*;
|
||||
use futures_util::{future, pin_mut, FutureExt};
|
||||
use std::future::Future;
|
||||
|
||||
/// A future that resolves to a value.
|
||||
/// A memo that resolve to a value asynchronously.
|
||||
///
|
||||
/// This runs the future only once - though the future may be regenerated
|
||||
/// through the [`UseFuture::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 canceled before the new one is started.
|
||||
///
|
||||
/// - dependencies: a tuple of references to values that are PartialEq + Clone
|
||||
pub fn use_resource<T, F>(future: impl Fn() -> F + 'static) -> UseResource<T>
|
||||
/// Regular memos are synchronous and resolve immediately. However, you might want to resolve a memo
|
||||
#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
|
||||
pub fn use_async_memo<T, F>(future: impl Fn() -> F + 'static) -> AsyncMemo<T>
|
||||
where
|
||||
T: 'static,
|
||||
F: Future<Output = T> + 'static,
|
||||
@@ -46,23 +40,23 @@ where
|
||||
.await;
|
||||
|
||||
// Set the value
|
||||
value.set(Some(res));
|
||||
value.set(Some(Signal::new(res)));
|
||||
});
|
||||
|
||||
Some(task)
|
||||
});
|
||||
|
||||
UseResource { task, value, state }
|
||||
AsyncMemo { task, value, state }
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub struct UseResource<T: 'static> {
|
||||
value: Signal<Option<T>>,
|
||||
pub struct AsyncMemo<T: 'static> {
|
||||
value: Signal<Option<Signal<T>>>,
|
||||
task: Signal<Option<Task>>,
|
||||
state: Signal<UseResourceState<T>>,
|
||||
}
|
||||
|
||||
impl<T> UseResource<T> {
|
||||
impl<T> AsyncMemo<T> {
|
||||
/// Restart the future with new dependencies.
|
||||
///
|
||||
/// Will not cancel the previous future, but will ignore any values that it
|
||||
@@ -81,14 +75,15 @@ impl<T> UseResource<T> {
|
||||
|
||||
// Manually set the value in the future slot without starting the future over
|
||||
pub fn set(&mut self, new_value: T) {
|
||||
self.value.set(Some(new_value));
|
||||
todo!()
|
||||
// self.value.set(Some(new_value));
|
||||
}
|
||||
|
||||
/// 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) -> Signal<Option<T>> {
|
||||
self.value
|
||||
pub fn value(&self) -> Option<Signal<T>> {
|
||||
self.value.cloned()
|
||||
}
|
||||
|
||||
/// Get the ID of the future in Dioxus' internal scheduler
|
||||
@@ -114,6 +109,16 @@ impl<T> UseResource<T> {
|
||||
// (Some(_), None) => UseResourceState::Pending,
|
||||
// }
|
||||
}
|
||||
|
||||
/// Wait for this async memo to resolve, returning the inner signal value
|
||||
/// If the value is pending, returns none and suspends the current component
|
||||
pub fn suspend(&self) -> Option<ReadOnlySignal<T>> {
|
||||
let out = self.value();
|
||||
if out.is_none() {
|
||||
suspend();
|
||||
}
|
||||
out.map(|sig| sig.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub enum UseResourceState<T: 'static> {
|
||||
|
||||
Reference in new issue
Block a user