Remove unnecessary calls to WeakViewHandle::upgrade

This commit is contained in:
Antonio Scandurra 2023-04-26 11:13:46 +02:00
parent 94c2eaad23
commit 2b6830c798
19 changed files with 378 additions and 443 deletions

View File

@ -65,7 +65,6 @@ impl ActivityIndicator {
let mut status_events = languages.language_server_binary_statuses();
cx.spawn(|this, mut cx| async move {
while let Some((language, event)) = status_events.next().await {
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| {
this.statuses.retain(|s| s.name != language.name());
this.statuses.push(LspStatus {
@ -74,9 +73,6 @@ impl ActivityIndicator {
});
cx.notify();
})?;
} else {
break;
}
}
anyhow::Ok(())
})

View File

@ -104,7 +104,6 @@ pub fn notify_of_any_new_update(
cx.spawn(|mut cx| async move {
let should_show_notification = should_show_notification.await?;
if should_show_notification {
if let Some(workspace) = workspace.upgrade(&cx) {
workspace.update(&mut cx, |workspace, cx| {
workspace.show_notification(0, cx, |cx| {
cx.add_view(|_| UpdateNotification::new(version))
@ -115,7 +114,6 @@ pub fn notify_of_any_new_update(
.detach_and_log_err(cx);
})?;
}
}
anyhow::Ok(())
})
.detach();

View File

@ -2510,9 +2510,6 @@ impl Editor {
None
};
let this = this
.upgrade(&cx)
.ok_or_else(|| anyhow!("editor was dropped"))?;
this.update(&mut cx, |this, cx| {
this.completion_tasks.retain(|(task_id, _)| *task_id > id);
@ -2672,15 +2669,10 @@ impl Editor {
cx.spawn(|this, mut cx| async move {
while let Some(prev_task) = task {
prev_task.await;
task = this
.upgrade(&cx)
.ok_or_else(|| anyhow!("editor dropped"))?
.update(&mut cx, |this, _| this.code_actions_task.take())?;
task = this.update(&mut cx, |this, _| this.code_actions_task.take())?;
}
this.upgrade(&cx)
.ok_or_else(|| anyhow!("editor dropped"))?
.update(&mut cx, |this, cx| {
this.update(&mut cx, |this, cx| {
if this.focused {
if let Some((buffer, actions)) = this.available_code_actions.clone() {
this.show_context_menu(
@ -2828,7 +2820,6 @@ impl Editor {
});
self.code_actions_task = Some(cx.spawn(|this, mut cx| async move {
let actions = actions.await;
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| {
this.available_code_actions = actions.log_err().and_then(|actions| {
if actions.is_empty() {
@ -2840,7 +2831,6 @@ impl Editor {
cx.notify();
})
.log_err();
}
}));
None
}
@ -2866,8 +2856,7 @@ impl Editor {
});
self.document_highlights_task = Some(cx.spawn(|this, mut cx| async move {
let highlights = highlights.log_err().await;
if let Some((this, highlights)) = this.upgrade(&cx).zip(highlights) {
if let Some(highlights) = highlights.await.log_err() {
this.update(&mut cx, |this, cx| {
if this.pending_rename.is_some() {
return;
@ -2976,8 +2965,7 @@ impl Editor {
.flatten()
.collect_vec();
this.upgrade(&cx)?
.update(&mut cx, |this, cx| {
this.update(&mut cx, |this, cx| {
if !completions.is_empty() {
this.copilot_state.cycled = false;
this.copilot_state.pending_cycling_refresh = Task::ready(None);
@ -3021,8 +3009,7 @@ impl Editor {
})
.await;
this.upgrade(&cx)?
.update(&mut cx, |this, cx| {
this.update(&mut cx, |this, cx| {
this.copilot_state.cycled = true;
for completion in completions.log_err().into_iter().flatten() {
this.copilot_state.push_completion(completion);

View File

@ -201,7 +201,6 @@ fn show_hover(
})
});
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, _| {
this.hover_state.diagnostic_popover =
local_diagnostic.map(|local_diagnostic| DiagnosticPopover {
@ -209,7 +208,6 @@ fn show_hover(
primary_diagnostic,
});
})?;
}
// Construct new hover popover from hover request
let hover_popover = hover_request.await.ok().flatten().and_then(|hover_result| {
@ -239,7 +237,6 @@ fn show_hover(
})
});
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| {
if let Some(hover_popover) = hover_popover.as_ref() {
// Highlight the selected symbol using a background highlight
@ -255,7 +252,7 @@ fn show_hover(
this.hover_state.info_popover = hover_popover;
cx.notify();
})?;
}
Ok::<_, anyhow::Error>(())
}
.log_err()

View File

@ -202,7 +202,6 @@ pub fn show_link_definition(
)
});
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| {
// Clear any existing highlights
this.clear_text_highlights::<LinkGoToDefinitionState>(cx);
@ -262,7 +261,6 @@ pub fn show_link_definition(
}
}
})?;
}
Ok::<_, anyhow::Error>(())
}

View File

@ -247,14 +247,12 @@ impl ScrollManager {
if cx.default_global::<ScrollbarAutoHide>().0 {
self.hide_scrollbar_task = Some(cx.spawn(|editor, mut cx| async move {
cx.background().timer(SCROLLBAR_SHOW_INTERVAL).await;
if let Some(editor) = editor.upgrade(&cx) {
editor
.update(&mut cx, |editor, cx| {
editor.scroll_manager.show_scrollbars = false;
cx.notify();
})
.log_err();
}
}));
} else {
self.hide_scrollbar_task = None;

View File

@ -104,10 +104,8 @@ impl<V: View> Tooltip<V> {
|view, mut cx| async move {
cx.background().timer(DEBOUNCE_TIMEOUT).await;
state.visible.set(true);
if let Some(view) = view.upgrade(&cx) {
view.update(&mut cx, |_, cx| cx.notify()).log_err();
}
}
}));
}
}

View File

@ -162,7 +162,6 @@ impl PickerDelegate for LanguageSelectorDelegate {
.await
};
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| {
let delegate = this.delegate_mut();
delegate.matches = matches;
@ -172,7 +171,6 @@ impl PickerDelegate for LanguageSelectorDelegate {
cx.notify();
})
.log_err();
}
})
}

View File

@ -240,8 +240,7 @@ impl<D: PickerDelegate> Picker<D> {
self.matches_updated(cx);
self.pending_update_matches = cx.spawn(|this, mut cx| async move {
update.await;
this.upgrade(&cx)?
.update(&mut cx, |this, cx| this.matches_updated(cx))
this.update(&mut cx, |this, cx| this.matches_updated(cx))
.log_err()
});
}

View File

@ -1,4 +1,3 @@
use anyhow::anyhow;
use editor::{
combine_syntax_and_fuzzy_match_highlights, scroll::autoscroll::Autoscroll,
styled_runs_for_code_label, Bias, Editor,
@ -119,9 +118,6 @@ impl PickerDelegate for ProjectSymbolsDelegate {
let workspace = self.workspace.clone();
cx.spawn(|_, mut cx| async move {
let buffer = buffer.await?;
let workspace = workspace
.upgrade(&cx)
.ok_or_else(|| anyhow!("workspace was dropped"))?;
workspace.update(&mut cx, |workspace, cx| {
let position = buffer
.read(cx)
@ -163,7 +159,6 @@ impl PickerDelegate for ProjectSymbolsDelegate {
.update(cx, |project, cx| project.symbols(&query, cx));
cx.spawn(|this, mut cx| async move {
let symbols = symbols.await.log_err();
if let Some(this) = this.upgrade(&cx) {
if let Some(symbols) = symbols {
this.update(&mut cx, |this, cx| {
let delegate = this.delegate_mut();
@ -174,8 +169,7 @@ impl PickerDelegate for ProjectSymbolsDelegate {
.map(|(id, symbol)| {
StringMatchCandidate::new(
id,
symbol.label.text[symbol.label.filter_range.clone()]
.to_string(),
symbol.label.text[symbol.label.filter_range.clone()].to_string(),
)
})
.partition(|candidate| {
@ -191,7 +185,6 @@ impl PickerDelegate for ProjectSymbolsDelegate {
})
.log_err();
}
}
})
}

View File

@ -196,16 +196,15 @@ impl ToolbarItemView for BufferSearchBar {
if let Some(searchable_item_handle) =
item.and_then(|item| item.to_searchable_item_handle(cx))
{
let handle = cx.weak_handle();
let this = cx.weak_handle();
self.active_searchable_item_subscription =
Some(searchable_item_handle.subscribe_to_search_events(
cx,
Box::new(move |search_event, cx| {
if let Some(this) = handle.upgrade(cx) {
this.update(cx, |this, cx| {
this.on_active_searchable_item_event(search_event, cx)
});
}
})
.log_err();
}),
));
@ -584,12 +583,10 @@ impl BufferSearchBar {
let active_searchable_item = active_searchable_item.downgrade();
self.pending_search = Some(cx.spawn(|this, mut cx| async move {
let matches = matches.await;
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| {
if let Some(active_searchable_item) = WeakSearchableItemHandle::upgrade(
active_searchable_item.as_ref(),
cx,
) {
if let Some(active_searchable_item) =
WeakSearchableItemHandle::upgrade(active_searchable_item.as_ref(), cx)
{
this.seachable_items_with_matches
.insert(active_searchable_item.downgrade(), matches);
@ -611,7 +608,6 @@ impl BufferSearchBar {
}
})
.log_err();
}
}));
}
}

View File

@ -277,10 +277,8 @@ impl TerminalView {
let epoch = self.next_blink_epoch();
cx.spawn(|this, mut cx| async move {
Timer::after(CURSOR_BLINK_INTERVAL).await;
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| this.blink_cursors(epoch, cx))
.log_err();
}
})
.detach();
}
@ -293,10 +291,8 @@ impl TerminalView {
let epoch = self.next_blink_epoch();
cx.spawn(|this, mut cx| async move {
Timer::after(CURSOR_BLINK_INTERVAL).await;
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| this.resume_cursor_blinking(epoch, cx))
.log_err();
}
})
.detach();
}

View File

@ -187,7 +187,6 @@ impl PickerDelegate for ThemeSelectorDelegate {
.await
};
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| {
let delegate = this.delegate_mut();
delegate.matches = matches;
@ -197,7 +196,6 @@ impl PickerDelegate for ThemeSelectorDelegate {
delegate.show_selected_theme(cx);
})
.log_err();
}
})
}

View File

@ -105,7 +105,6 @@ impl PickerDelegate for BaseKeymapSelectorDelegate {
.await
};
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, _| {
let delegate = this.delegate_mut();
delegate.matches = matches;
@ -114,7 +113,6 @@ impl PickerDelegate for BaseKeymapSelectorDelegate {
.min(delegate.matches.len().saturating_sub(1));
})
.log_err();
}
})
}

View File

@ -3,7 +3,7 @@ use crate::{
FollowableItemBuilders, ItemNavHistory, Pane, ToolbarItemLocation, ViewId, Workspace,
WorkspaceId,
};
use anyhow::{anyhow, Result};
use anyhow::Result;
use client::{proto, Client};
use gpui::{
fonts::HighlightStyle, AnyElement, AnyViewHandle, AppContext, ModelHandle, Task, View,
@ -481,8 +481,6 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
} else {
cx.spawn(|workspace, mut cx| async move {
workspace
.upgrade(&cx)
.ok_or_else(|| anyhow!("workspace was dropped"))?
.update(&mut cx, |workspace, cx| {
item.git_diff_recalc(
workspace.project().clone(),

View File

@ -2005,9 +2005,11 @@ impl NavHistory {
}
fn did_update(&self, cx: &mut WindowContext) {
if let Some(pane) = self.pane.upgrade(cx) {
cx.defer(move |cx| pane.update(cx, |pane, cx| pane.history_updated(cx)));
}
let pane = self.pane.clone();
cx.defer(move |cx| {
pane.update(cx, |pane, cx| pane.history_updated(cx))
.log_err();
});
}
}

View File

@ -6,7 +6,9 @@ use std::{
use anyhow::{Context, Result};
use async_recursion::async_recursion;
use gpui::{platform::WindowBounds, AsyncAppContext, Axis, ModelHandle, Task, ViewHandle};
use gpui::{
platform::WindowBounds, AsyncAppContext, Axis, ModelHandle, Task, ViewHandle, WeakViewHandle,
};
use db::sqlez::{
bindable::{Bind, Column, StaticColumnCount},
@ -97,7 +99,7 @@ impl SerializedPaneGroup {
&self,
project: &ModelHandle<Project>,
workspace_id: WorkspaceId,
workspace: &ViewHandle<Workspace>,
workspace: &WeakViewHandle<Workspace>,
cx: &mut AsyncAppContext,
) -> Option<(Member, Option<ViewHandle<Pane>>)> {
match self {
@ -172,7 +174,7 @@ impl SerializedPane {
project: &ModelHandle<Project>,
pane_handle: &ViewHandle<Pane>,
workspace_id: WorkspaceId,
workspace: &ViewHandle<Workspace>,
workspace: &WeakViewHandle<Workspace>,
cx: &mut AsyncAppContext,
) -> Result<()> {
let mut active_item_index = None;
@ -181,13 +183,7 @@ impl SerializedPane {
let item_handle = pane_handle
.update(cx, |_, cx| {
if let Some(deserializer) = cx.global::<ItemDeserializers>().get(&item.kind) {
deserializer(
project,
workspace.downgrade(),
workspace_id,
item.item_id,
cx,
)
deserializer(project, workspace.clone(), workspace_id, item.item_id, cx)
} else {
Task::ready(Err(anyhow::anyhow!(
"Deserializer does not exist for item kind: {}",

View File

@ -599,14 +599,12 @@ impl DelayedDebouncedEditAction {
_ = timer => {}
}
if let Some(workspace) = workspace.upgrade(&cx) {
if let Some(result) = workspace
.update(&mut cx, |workspace, cx| (f)(workspace, cx))
.log_err()
{
result.await.log_err();
}
}
}));
}
}
@ -740,10 +738,8 @@ impl Workspace {
Stream::map(current_user, drop).merge(Stream::map(connection_status, drop));
while stream.recv().await.is_some() {
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |_, cx| cx.notify())?;
}
}
anyhow::Ok(())
});
let handle = cx.handle();
@ -754,8 +750,7 @@ impl Workspace {
mpsc::unbounded::<(PeerId, proto::UpdateFollowers)>();
let _apply_leader_updates = cx.spawn(|this, mut cx| async move {
while let Some((leader_id, update)) = leader_updates_rx.next().await {
let Some(this) = this.upgrade(&cx) else { break };
Self::process_leader_update(this, leader_id, update, &mut cx)
Self::process_leader_update(&this, leader_id, update, &mut cx)
.await
.log_err();
}
@ -1977,7 +1972,6 @@ impl Workspace {
Some(cx.spawn(|this, mut cx| async move {
let response = request.await?;
if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, _| {
let state = this
.follower_states_by_leader
@ -2000,7 +1994,6 @@ impl Workspace {
)
.await?;
this.update(&mut cx, |this, cx| this.leader_updated(leader_id, cx))?;
}
Ok(())
}))
}
@ -2303,7 +2296,7 @@ impl Workspace {
}
async fn process_leader_update(
this: ViewHandle<Self>,
this: &WeakViewHandle<Self>,
leader_id: PeerId,
update: proto::UpdateFollowers,
cx: &mut AsyncAppContext,
@ -2363,7 +2356,7 @@ impl Workspace {
}
async fn add_views_from_leader(
this: ViewHandle<Self>,
this: WeakViewHandle<Self>,
leader_id: PeerId,
panes: Vec<ViewHandle<Pane>>,
views: Vec<proto::View>,
@ -2691,7 +2684,6 @@ impl Workspace {
cx: &mut AppContext,
) {
cx.spawn(|mut cx| async move {
if let Some(workspace) = workspace.upgrade(&cx) {
let (project, dock_pane_handle, old_center_pane) =
workspace.read_with(&cx, |workspace, _| {
(
@ -2765,8 +2757,7 @@ impl Workspace {
})?;
// Serialize ourself to make sure our timestamps and any pane / item changes are replicated
workspace.read_with(&cx, |workspace, cx| workspace.serialize_workspace(cx))?
}
workspace.read_with(&cx, |workspace, cx| workspace.serialize_workspace(cx))?;
anyhow::Ok(())
})
.detach_and_log_err(cx);

View File

@ -509,7 +509,6 @@ fn open_log_file(
app_state.fs.load(&paths::LOG)
);
if let Some(workspace) = workspace.upgrade(&cx) {
let mut lines = VecDeque::with_capacity(MAX_LINES);
for line in old_log
.iter()
@ -538,14 +537,15 @@ fn open_log_file(
MultiBuffer::singleton(buffer, cx).with_title("Log".into())
});
workspace.add_item(
Box::new(cx.add_view(|cx| {
Box::new(
cx.add_view(|cx| {
Editor::for_multibuffer(buffer, Some(project), cx)
})),
}),
),
cx,
);
})
.log_err();
}
})
.detach();
})
@ -559,8 +559,6 @@ fn open_telemetry_log_file(
) {
workspace.with_local_workspace(&app_state.clone(), cx, move |_, cx| {
cx.spawn(|workspace, mut cx| async move {
let workspace = workspace.upgrade(&cx)?;
async fn fetch_log_string(app_state: &Arc<AppState>) -> Option<String> {
let path = app_state.client.telemetry_log_file_path()?;
app_state.fs.load(&path).await.log_err()