From 57beec607193a81dbacfddfa2322daaa90415db0 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 26 Apr 2023 11:59:38 +0200 Subject: [PATCH] Allow direct read/update of `WeakViewHandle` only in `AsyncAppContext` --- crates/gpui/src/app.rs | 6 +++--- crates/search/src/buffer_search.rs | 9 +++++---- crates/workspace/src/pane.rs | 10 +++++----- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 461b751675..a67e28715b 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -4086,10 +4086,10 @@ impl WeakViewHandle { pub fn read_with( &self, - cx: &impl BorrowAppContext, + cx: &AsyncAppContext, read: impl FnOnce(&V, &ViewContext) -> T, ) -> Result { - cx.read_with(|cx| { + cx.read(|cx| { let handle = cx .upgrade_view_handle(self) .ok_or_else(|| anyhow!("view {} was dropped", V::ui_name()))?; @@ -4100,7 +4100,7 @@ impl WeakViewHandle { pub fn update( &self, - cx: &mut impl BorrowAppContext, + cx: &mut AsyncAppContext, update: impl FnOnce(&mut V, &mut ViewContext) -> T, ) -> Result { cx.update(|cx| { diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index 4b83a99555..91ca99c5c3 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -201,10 +201,11 @@ impl ToolbarItemView for BufferSearchBar { Some(searchable_item_handle.subscribe_to_search_events( cx, Box::new(move |search_event, cx| { - this.update(cx, |this, cx| { - this.on_active_searchable_item_event(search_event, cx) - }) - .log_err(); + if let Some(this) = this.upgrade(cx) { + this.update(cx, |this, cx| { + this.on_active_searchable_item_event(search_event, cx) + }); + } }), )); diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 10381743b9..b1214f4233 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -2005,11 +2005,11 @@ impl NavHistory { } fn did_update(&self, cx: &mut WindowContext) { - let pane = self.pane.clone(); - cx.defer(move |cx| { - pane.update(cx, |pane, cx| pane.history_updated(cx)) - .log_err(); - }); + if let Some(pane) = self.pane.upgrade(cx) { + cx.defer(move |cx| { + pane.update(cx, |pane, cx| pane.history_updated(cx)); + }); + } } }