Return to just TaskId, still polling

This commit is contained in:
Exotik850 committed 2023-12-12 13:02:35 -06:00
1 parent 69ca1fd8a7
commit fdce7bbe5a
8 files changed
+26 -34

No files matched your search

+1 -2
View File
@@ -19,8 +19,7 @@ fn app(cx: Scope) -> Element {
emails_sent.write().push(message); emails_sent.write().push(message);
} }
} }
}) });
.unwrap();
cx.render(rsx! { cx.render(rsx! {
div { div {
+3 -9
View File
@@ -33,11 +33,7 @@ impl Scheduler {
/// ///
/// Spawning a future onto the root scope will cause it to be dropped when the root component is dropped - which /// Spawning a future onto the root scope will cause it to be dropped when the root component is dropped - which
/// will only occur when the VirtuaalDom itself has been dropped. /// will only occur when the VirtuaalDom itself has been dropped.
pub fn spawn( pub fn spawn(&self, scope: ScopeId, task: impl Future<Output = ()> + 'static) -> TaskId {
&self,
scope: ScopeId,
task: impl Future<Output = ()> + 'static,
) -> Option<TaskId> {
let mut tasks = self.tasks.borrow_mut(); let mut tasks = self.tasks.borrow_mut();
let entry = tasks.vacant_entry(); let entry = tasks.vacant_entry();
@@ -53,9 +49,7 @@ impl Scheduler {
}; };
let mut cx = std::task::Context::from_waker(&task.waker); let mut cx = std::task::Context::from_waker(&task.waker);
if task.task.borrow_mut().as_mut().poll(&mut cx).is_ready() { let _ = task.task.borrow_mut().as_mut().poll(&mut cx);
return None;
}
entry.insert(task); entry.insert(task);
@@ -63,7 +57,7 @@ impl Scheduler {
.unbounded_send(SchedulerMsg::TaskNotified(task_id)) .unbounded_send(SchedulerMsg::TaskNotified(task_id))
.expect("Scheduler should exist"); .expect("Scheduler should exist");
Some(task_id) task_id
} }
/// Drop the future with the given TaskId /// Drop the future with the given TaskId
+8 -8
View File
@@ -214,10 +214,10 @@ impl ScopeContext {
} }
/// Pushes the future onto the poll queue to be polled after the component renders. /// Pushes the future onto the poll queue to be polled after the component renders.
pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> { pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
let id = self.tasks.spawn(self.id, fut)?; let id = self.tasks.spawn(self.id, fut);
self.spawned_tasks.borrow_mut().insert(id); self.spawned_tasks.borrow_mut().insert(id);
Some(id) id
} }
/// Spawns the future but does not return the [`TaskId`] /// Spawns the future but does not return the [`TaskId`]
@@ -228,9 +228,9 @@ impl ScopeContext {
/// Spawn a future that Dioxus won't clean up when this component is unmounted /// Spawn a future that Dioxus won't clean up when this component is unmounted
/// ///
/// This is good for tasks that need to be run after the component has been dropped. /// This is good for tasks that need to be run after the component has been dropped.
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> { pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
// The root scope will never be unmounted so we can just add the task at the top of the app // The root scope will never be unmounted so we can just add the task at the top of the app
let id = self.tasks.spawn(ScopeId::ROOT, fut)?; let id = self.tasks.spawn(ScopeId::ROOT, fut);
// wake up the scheduler if it is sleeping // wake up the scheduler if it is sleeping
self.tasks self.tasks
@@ -240,7 +240,7 @@ impl ScopeContext {
self.spawned_tasks.borrow_mut().insert(id); self.spawned_tasks.borrow_mut().insert(id);
Some(id) id
} }
/// Informs the scheduler that this task is no longer needed and should be removed. /// Informs the scheduler that this task is no longer needed and should be removed.
@@ -339,7 +339,7 @@ pub fn throw(error: impl Debug + 'static) -> Option<()> {
/// Pushes the future onto the poll queue to be polled after the component renders. /// Pushes the future onto the poll queue to be polled after the component renders.
pub fn push_future(fut: impl Future<Output = ()> + 'static) -> Option<TaskId> { pub fn push_future(fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
with_current_scope(|cx| cx.push_future(fut)).flatten() with_current_scope(|cx| cx.push_future(fut))
} }
/// Spawns the future but does not return the [`TaskId`] /// Spawns the future but does not return the [`TaskId`]
@@ -351,7 +351,7 @@ pub fn spawn(fut: impl Future<Output = ()> + 'static) {
/// ///
/// This is good for tasks that need to be run after the component has been dropped. /// This is good for tasks that need to be run after the component has been dropped.
pub fn spawn_forever(fut: impl Future<Output = ()> + 'static) -> Option<TaskId> { pub fn spawn_forever(fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
with_current_scope(|cx| cx.spawn_forever(fut)).flatten() with_current_scope(|cx| cx.spawn_forever(fut))
} }
/// Informs the scheduler that this task is no longer needed and should be removed. /// Informs the scheduler that this task is no longer needed and should be removed.
+2 -2
View File
@@ -309,7 +309,7 @@ impl<'src> ScopeState {
} }
/// Pushes the future onto the poll queue to be polled after the component renders. /// Pushes the future onto the poll queue to be polled after the component renders.
pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> { pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
self.context().push_future(fut) self.context().push_future(fut)
} }
@@ -321,7 +321,7 @@ impl<'src> ScopeState {
/// Spawn a future that Dioxus won't clean up when this component is unmounted /// Spawn a future that Dioxus won't clean up when this component is unmounted
/// ///
/// This is good for tasks that need to be run after the component has been dropped. /// This is good for tasks that need to be run after the component has been dropped.
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> { pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
self.context().spawn_forever(fut) self.context().spawn_forever(fut)
} }
@@ -76,7 +76,7 @@ where
cx.remove_future(current); cx.remove_future(current);
} }
state.task.set(cx.push_future(async move { state.task.set(Some(cx.push_future(async move {
let data; let data;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
{ {
@@ -94,7 +94,7 @@ where
*value.borrow_mut() = Some(Box::new(data)); *value.borrow_mut() = Some(Box::new(data));
schedule_update(); schedule_update();
})); })));
} }
if first_run { if first_run {
+3 -3
View File
@@ -63,15 +63,15 @@ use std::future::Future;
/// } /// }
/// }) /// })
/// ``` /// ```
pub fn use_coroutine<M, G, F>(cx: &ScopeState, init: G) -> Option<&Coroutine<M>> pub fn use_coroutine<M, G, F>(cx: &ScopeState, init: G) -> &Coroutine<M>
where where
M: 'static, M: 'static,
G: FnOnce(UnboundedReceiver<M>) -> F, G: FnOnce(UnboundedReceiver<M>) -> F,
F: Future<Output = ()> + 'static, F: Future<Output = ()> + 'static,
{ {
let (tx, rx) = futures_channel::mpsc::unbounded(); let (tx, rx) = futures_channel::mpsc::unbounded();
let task = cx.push_future(init(rx))?; let task = cx.push_future(init(rx));
Some(cx.use_hook(|| cx.provide_context(Coroutine { tx, task }))) cx.use_hook(|| cx.provide_context(Coroutine { tx, task }))
} }
/// Get a handle to a coroutine higher in the tree /// Get a handle to a coroutine higher in the tree
+5 -6
View File
@@ -99,9 +99,8 @@ where
// Create the new future // Create the new future
let return_value = future(dependencies.out()); let return_value = future(dependencies.out());
if let Some(task) = return_value.apply(state.cleanup.clone(), cx) { let task = return_value.apply(state.cleanup.clone(), cx);
state.task.set(Some(task)); state.task.set(Some(task));
}
} }
} }
@@ -109,14 +108,14 @@ type UseEffectCleanup = Rc<RefCell<Option<Box<dyn FnOnce()>>>>;
/// Something that can be returned from a `use_effect` hook. /// Something that can be returned from a `use_effect` hook.
pub trait UseEffectReturn<T> { pub trait UseEffectReturn<T> {
fn apply(self, oncleanup: UseEffectCleanup, cx: &ScopeState) -> Option<TaskId>; fn apply(self, oncleanup: UseEffectCleanup, cx: &ScopeState) -> TaskId;
} }
impl<T> UseEffectReturn<()> for T impl<T> UseEffectReturn<()> for T
where where
T: Future<Output = ()> + 'static, T: Future<Output = ()> + 'static,
{ {
fn apply(self, _: UseEffectCleanup, cx: &ScopeState) -> Option<TaskId> { fn apply(self, _: UseEffectCleanup, cx: &ScopeState) -> TaskId {
cx.push_future(self) cx.push_future(self)
} }
} }
@@ -128,7 +127,7 @@ where
T: Future<Output = F> + 'static, T: Future<Output = F> + 'static,
F: FnOnce() + 'static, F: FnOnce() + 'static,
{ {
fn apply(self, oncleanup: UseEffectCleanup, cx: &ScopeState) -> Option<TaskId> { fn apply(self, oncleanup: UseEffectCleanup, cx: &ScopeState) -> TaskId {
cx.push_future(async move { cx.push_future(async move {
let cleanup = self.await; let cleanup = self.await;
*oncleanup.borrow_mut() = Some(Box::new(cleanup) as Box<dyn FnOnce()>); *oncleanup.borrow_mut() = Some(Box::new(cleanup) as Box<dyn FnOnce()>);
+2 -2
View File
@@ -49,10 +49,10 @@ where
let val = val.clone(); let val = val.clone();
let task = state.task.clone(); let task = state.task.clone();
state.task.set(cx.push_future(async move { state.task.set(Some(cx.push_future(async move {
val.set(Some(fut.await)); val.set(Some(fut.await));
task.take(); task.take();
})); })));
// Mark that we don't need to regenerate // Mark that we don't need to regenerate
state.needs_regen.set(false); state.needs_regen.set(false);