Add task:wait_for_local

This commit is contained in:
Lucy 2022-02-02 16:28:08 -05:00
parent 585946bbfa
commit 7941c2f6a3
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
use std::future::Future;
use tokio::sync::oneshot;
pub fn spawn<O, F>(future: F) -> tokio::task::JoinHandle<O>
where
@ -12,3 +13,12 @@ where
pub fn spawn_local<F: Future<Output = ()> + 'static>(future: F) {
gtk4::glib::MainContext::default().spawn_local(future);
}
pub async fn wait_for_local<F: Future<Output = ()> + 'static>(future: F) {
let (tx, rx) = oneshot::channel::<()>();
gtk4::glib::MainContext::default().spawn_local(async move {
future.await;
let _ = tx.send(());
});
std::mem::drop(rx.await);
}