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