| 1 | use std::future::Future; |
| 2 | use std::sync::OnceLock; |
| 3 | |
| 4 | use tokio::runtime::{Builder, Runtime}; |
| 5 | use tokio::sync::oneshot; |
| 6 | |
| 7 | static RUNTIME: OnceLock<Runtime> = OnceLock::new(); |
| 8 | |
| 9 | fn runtime() -> &'static Runtime { |
| 10 | RUNTIME.get_or_init(|| { |
| 11 | Builder::new_multi_thread() |
| 12 | .worker_threads(4) |
| 13 | .thread_name("teams-net") |
| 14 | .enable_all() |
| 15 | .build() |
| 16 | .expect("tokio runtime") |
| 17 | }) |
| 18 | } |
| 19 | |
| 20 | pub fn handle() -> tokio::runtime::Handle { |
| 21 | runtime().handle().clone() |
| 22 | } |
| 23 | |
| 24 | /// Runs the future on the network runtime. The receiver is runtime-agnostic, so the GPUI executor can await it. |
| 25 | pub fn spawn<T: Send + 'static>( |
| 26 | future: impl Future<Output = T> + Send + 'static, |
| 27 | ) -> oneshot::Receiver<T> { |
| 28 | let (sender, receiver) = oneshot::channel(); |
| 29 | runtime().spawn(async move { |
| 30 | let _ = sender.send(future.await); |
| 31 | }); |
| 32 | receiver |
| 33 | } |