cosmic-applets/applets/cosmic-applet-network/src/task.rs

36 lines
855 B
Rust
Raw Normal View History

2022-05-26 11:57:41 -04:00
// SPDX-License-Identifier: GPL-3.0-or-later
2022-02-01 16:46:25 -05:00
use std::future::Future;
2022-02-02 16:28:08 -05:00
use tokio::sync::oneshot;
2022-02-01 16:46:25 -05:00
pub fn spawn<O, F>(future: F) -> tokio::task::JoinHandle<O>
where
F: Future<Output = O> + Send + 'static,
O: Send + 'static,
{
crate::RT.spawn(future)
}
2022-02-07 14:14:55 -05:00
pub fn block_on<O, F>(future: F) -> O
where
F: Future<Output = O> + Send + 'static,
O: Send + 'static,
{
crate::RT.block_on(future)
}
2022-02-01 16:46:25 -05:00
pub fn spawn_local<F: Future<Output = ()> + 'static>(future: F) {
gtk4::glib::MainContext::default().spawn_local(future);
}
2022-02-02 16:28:08 -05:00
pub async fn wait_for_local<O, F>(future: F) -> Option<O>
where
O: Send + 'static,
F: Future<Output = O> + Send + 'static,
{
let (tx, rx) = oneshot::channel::<O>();
2022-02-02 16:28:08 -05:00
gtk4::glib::MainContext::default().spawn_local(async move {
std::mem::drop(tx.send(future.await));
2022-02-02 16:28:08 -05:00
});
rx.await.ok()
2022-02-02 16:28:08 -05:00
}