Propagate jitterbug cancel to bachata

This commit is contained in:
asonix 2023-08-27 17:16:18 -05:00
parent 807117eb8a
commit 35cf37c782
2 changed files with 21 additions and 2 deletions

View file

@ -20,6 +20,8 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
handle.await?; handle.await?;
} }
println!("{i} joined");
Ok(()) as Result<_, jive::task::sync::JoinError> Ok(()) as Result<_, jive::task::sync::JoinError>
}) })
}) })
@ -29,6 +31,8 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
handle.await??; handle.await??;
} }
println!("all joined");
Ok(()) Ok(())
}) })
} }

View file

@ -92,8 +92,23 @@ impl RuntimeHandle {
future: impl Future<Output = T> + 'static, future: impl Future<Output = T> + 'static,
) -> jitterbug::JoinHandle<T> { ) -> jitterbug::JoinHandle<T> {
let (tx, rx) = jitterbug::oneshot(); let (tx, rx) = jitterbug::oneshot();
bachata::spawn(async move { tx.send(future.await) }); let (cancel_tx, cancel_rx) = jitterbug::oneshot::<()>();
self.spawn(async move { rx.await.unwrap() })
bachata::spawn(async move {
let future = std::pin::pin!(future);
match select::select(cancel_rx, future).await {
select::Either::Left(_) => (), // canceled
select::Either::Right(item) => {
let _ = tx.send(item);
}
}
});
self.spawn(async move {
let out = rx.await.unwrap();
drop(cancel_tx);
out
})
} }
pub fn spawn_blocking<T: Send + 'static>( pub fn spawn_blocking<T: Send + 'static>(