Add gpui helpers

This commit is contained in:
Nathan Sobo
2025-04-20 00:16:08 -06:00
parent 107d8ca483
commit 152bbca238
2 changed files with 15 additions and 1 deletions

View File

@@ -178,7 +178,14 @@ impl TestAppContext {
&self.foreground_executor
}
fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T> {
/// Builds an entity that is owned by the application.
///
/// The given function will be invoked with a [`Context`] and must return an object representing the entity. An
/// [`Entity`] handle will be returned, which can be used to access the entity in a context.
pub fn new<T: 'static>(
&mut self,
build_entity: impl FnOnce(&mut Context<T>) -> T,
) -> Entity<T> {
let mut cx = self.app.borrow_mut();
cx.new(build_entity)
}

View File

@@ -95,6 +95,13 @@ where
.spawn(self.log_tracked_err(*location))
.detach();
}
/// Convert a Task<Result<T, E>> to a Task<()> that logs all errors.
pub fn log_err_in_task(self, cx: &App) -> Task<Option<T>> {
let location = core::panic::Location::caller();
cx.foreground_executor()
.spawn(async move { self.log_tracked_err(*location).await })
}
}
impl<T> Future for Task<T> {