Make the gpui_tokio crate generic over the context it spawns (#23995)

Part of  #21092

Makes `Tokio::spawn` generic over any `AppContext`.

Also removes a stray `model_context` I missed

Release Notes:

- N/A
This commit is contained in:
Mikayla Maki 2025-01-30 18:00:55 -08:00 committed by GitHub
parent ff43b6875b
commit 517e519bdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 133 additions and 18 deletions

View file

@ -1,6 +1,6 @@
use std::future::Future;
use gpui::{App, Global, ReadGlobal, Task};
use gpui::{App, AppContext, Global, ReadGlobal, Task};
use tokio::task::JoinError;
use util::defer;
@ -32,20 +32,23 @@ pub struct Tokio {}
impl Tokio {
/// Spawns the given future on Tokio's thread pool, and returns it via a GPUI task
/// Note that the Tokio task will be cancelled if the GPUI task is dropped
pub fn spawn<Fut, R>(cx: &mut App, f: Fut) -> Task<Result<R, JoinError>>
pub fn spawn<C, Fut, R>(cx: &mut C, f: Fut) -> C::Result<Task<Result<R, JoinError>>>
where
C: AppContext,
Fut: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
let join_handle = GlobalTokio::global(cx).runtime.spawn(f);
let abort_handle = join_handle.abort_handle();
let cancel = defer(move || {
abort_handle.abort();
});
cx.background_executor().spawn(async move {
let result = join_handle.await;
drop(cancel);
result
cx.read_global(|tokio: &GlobalTokio, cx| {
let join_handle = tokio.runtime.spawn(f);
let abort_handle = join_handle.abort_handle();
let cancel = defer(move || {
abort_handle.abort();
});
cx.background_spawn(async move {
let result = join_handle.await;
drop(cancel);
result
})
})
}