From 7941c2f6a34479ed4840e78cd5998521f85289e6 Mon Sep 17 00:00:00 2001 From: Lucy Date: Wed, 2 Feb 2022 16:28:08 -0500 Subject: [PATCH] Add `task:wait_for_local` --- applets/cosmic-applet-network/src/task.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/applets/cosmic-applet-network/src/task.rs b/applets/cosmic-applet-network/src/task.rs index 9d75bfe4..61d79cf8 100644 --- a/applets/cosmic-applet-network/src/task.rs +++ b/applets/cosmic-applet-network/src/task.rs @@ -1,5 +1,6 @@ // SPDX-License-Identifier: LGPL-3.0-or-later use std::future::Future; +use tokio::sync::oneshot; pub fn spawn(future: F) -> tokio::task::JoinHandle where @@ -12,3 +13,12 @@ where pub fn spawn_local + 'static>(future: F) { gtk4::glib::MainContext::default().spawn_local(future); } + +pub async fn wait_for_local + '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); +}