Make cx.spawn() return an Option<TaskId>, returns None if finished immediately

This commit is contained in:
kidkool850@gmail.com committed 2023-12-11 16:01:40 -06:00
1 parent 695bf009d1
commit 69ca1fd8a7
8 files changed
+38 -37

No files matched your search

+4 -6
View File
@@ -63,17 +63,15 @@ use std::future::Future;
/// }
/// })
/// ```
pub fn use_coroutine<M, G, F>(cx: &ScopeState, init: G) -> &Coroutine<M>
pub fn use_coroutine<M, G, F>(cx: &ScopeState, init: G) -> Option<&Coroutine<M>>
where
M: 'static,
G: FnOnce(UnboundedReceiver<M>) -> F,
F: Future<Output = ()> + 'static,
{
cx.use_hook(|| {
let (tx, rx) = futures_channel::mpsc::unbounded();
let task = cx.push_future(init(rx));
cx.provide_context(Coroutine { tx, task })
})
let (tx, rx) = futures_channel::mpsc::unbounded();
let task = cx.push_future(init(rx))?;
Some(cx.use_hook(|| cx.provide_context(Coroutine { tx, task })))
}
/// Get a handle to a coroutine higher in the tree
+3 -4
View File
@@ -117,7 +117,7 @@ where
T: Future<Output = ()> + 'static,
{
fn apply(self, _: UseEffectCleanup, cx: &ScopeState) -> Option<TaskId> {
Some(cx.push_future(self))
cx.push_future(self)
}
}
@@ -129,11 +129,10 @@ where
F: FnOnce() + 'static,
{
fn apply(self, oncleanup: UseEffectCleanup, cx: &ScopeState) -> Option<TaskId> {
let task = cx.push_future(async move {
cx.push_future(async move {
let cleanup = self.await;
*oncleanup.borrow_mut() = Some(Box::new(cleanup) as Box<dyn FnOnce()>);
});
Some(task)
})
}
}
+2 -2
View File
@@ -49,10 +49,10 @@ where
let val = val.clone();
let task = state.task.clone();
state.task.set(Some(cx.push_future(async move {
state.task.set(cx.push_future(async move {
val.set(Some(fut.await));
task.take();
})));
}));
// Mark that we don't need to regenerate
state.needs_regen.set(false);