Return Result from AsyncAppContext::update_view

This commit is contained in:
Antonio Scandurra 2023-04-18 12:03:53 +02:00
parent 493a418c91
commit 31e6bb4fc1
4 changed files with 62 additions and 56 deletions

View file

@ -210,9 +210,9 @@ impl ContextMenu {
cx.notify(); cx.notify();
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
cx.background().timer(Duration::from_millis(50)).await; cx.background().timer(Duration::from_millis(50)).await;
this.update(&mut cx, |this, cx| this.cancel(&Default::default(), cx)); this.update(&mut cx, |this, cx| this.cancel(&Default::default(), cx))
}) })
.detach(); .detach_and_log_err(cx);
} }
} }
} }

View file

@ -464,19 +464,20 @@ impl ReadModelWith for AsyncAppContext {
} }
impl UpdateView for AsyncAppContext { impl UpdateView for AsyncAppContext {
type Output<S> = Option<S>; type Output<S> = Result<S>;
fn update_view<T, S>( fn update_view<T, S>(
&mut self, &mut self,
handle: &ViewHandle<T>, handle: &ViewHandle<T>,
update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S, update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
) -> Option<S> ) -> Result<S>
where where
T: View, T: View,
{ {
self.0 self.0
.borrow_mut() .borrow_mut()
.update_window(handle.window_id, |cx| cx.update_view(handle, update)) .update_window(handle.window_id, |cx| cx.update_view(handle, update))
.ok_or_else(|| anyhow!("window was closed"))
} }
} }

View file

@ -7,7 +7,7 @@ use crate::{
toolbar::Toolbar, toolbar::Toolbar,
Item, NewFile, NewSearch, NewTerminal, Workspace, Item, NewFile, NewSearch, NewTerminal, Workspace,
}; };
use anyhow::Result; use anyhow::{anyhow, Result};
use collections::{HashMap, HashSet, VecDeque}; use collections::{HashMap, HashSet, VecDeque};
use context_menu::{ContextMenu, ContextMenuItem}; use context_menu::{ContextMenu, ContextMenuItem};
use drag_and_drop::Draggable; use drag_and_drop::Draggable;
@ -394,7 +394,7 @@ impl Pane {
workspace: &mut Workspace, workspace: &mut Workspace,
pane: Option<ViewHandle<Pane>>, pane: Option<ViewHandle<Pane>>,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Task<()> { ) -> Task<Result<()>> {
Self::navigate_history( Self::navigate_history(
workspace, workspace,
pane.unwrap_or_else(|| workspace.active_pane().clone()), pane.unwrap_or_else(|| workspace.active_pane().clone()),
@ -407,7 +407,7 @@ impl Pane {
workspace: &mut Workspace, workspace: &mut Workspace,
pane: Option<ViewHandle<Pane>>, pane: Option<ViewHandle<Pane>>,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Task<()> { ) -> Task<Result<()>> {
Self::navigate_history( Self::navigate_history(
workspace, workspace,
pane.unwrap_or_else(|| workspace.active_pane().clone()), pane.unwrap_or_else(|| workspace.active_pane().clone()),
@ -419,7 +419,7 @@ impl Pane {
pub fn reopen_closed_item( pub fn reopen_closed_item(
workspace: &mut Workspace, workspace: &mut Workspace,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Task<()> { ) -> Task<Result<()>> {
Self::navigate_history( Self::navigate_history(
workspace, workspace,
workspace.active_pane().clone(), workspace.active_pane().clone(),
@ -453,7 +453,7 @@ impl Pane {
pane: ViewHandle<Pane>, pane: ViewHandle<Pane>,
mode: NavigationMode, mode: NavigationMode,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Task<()> { ) -> Task<Result<()>> {
cx.focus(&pane); cx.focus(&pane);
let to_load = pane.update(cx, |pane, cx| { let to_load = pane.update(cx, |pane, cx| {
@ -503,13 +503,15 @@ impl Pane {
let task = workspace.load_path(project_path, cx); let task = workspace.load_path(project_path, cx);
cx.spawn(|workspace, mut cx| async move { cx.spawn(|workspace, mut cx| async move {
let task = task.await; let task = task.await;
if let Some(pane) = pane.upgrade(&cx) { let pane = pane
.upgrade(&cx)
.ok_or_else(|| anyhow!("pane was dropped"))?;
let mut navigated = false; let mut navigated = false;
if let Some((project_entry_id, build_item)) = task.log_err() { if let Some((project_entry_id, build_item)) = task.log_err() {
let prev_active_item_id = pane.update(&mut cx, |pane, _| { let prev_active_item_id = pane.update(&mut cx, |pane, _| {
pane.nav_history.borrow_mut().set_mode(mode); pane.nav_history.borrow_mut().set_mode(mode);
pane.active_item().map(|p| p.id()) pane.active_item().map(|p| p.id())
}); })?;
let item = workspace.update(&mut cx, |workspace, cx| { let item = workspace.update(&mut cx, |workspace, cx| {
Self::open_item( Self::open_item(
@ -520,7 +522,7 @@ impl Pane {
cx, cx,
build_item, build_item,
) )
}); })?;
pane.update(&mut cx, |pane, cx| { pane.update(&mut cx, |pane, cx| {
navigated |= Some(item.id()) != prev_active_item_id; navigated |= Some(item.id()) != prev_active_item_id;
@ -530,20 +532,21 @@ impl Pane {
if let Some(data) = entry.data { if let Some(data) = entry.data {
navigated |= item.navigate(data, cx); navigated |= item.navigate(data, cx);
} }
}); })?;
} }
if !navigated { if !navigated {
workspace workspace
.update(&mut cx, |workspace, cx| { .update(&mut cx, |workspace, cx| {
Self::navigate_history(workspace, pane, mode, cx) Self::navigate_history(workspace, pane, mode, cx)
}) })?
.await; .await;
} }
}
Ok(())
}) })
} else { } else {
Task::ready(()) Task::ready(Ok(()))
} }
} }
@ -1104,10 +1107,10 @@ impl Pane {
CONFLICT_MESSAGE, CONFLICT_MESSAGE,
&["Overwrite", "Discard", "Cancel"], &["Overwrite", "Discard", "Cancel"],
) )
}); })?;
match answer.next().await { match answer.next().await {
Some(0) => pane.update(cx, |_, cx| item.save(project, cx)).await?, Some(0) => pane.update(cx, |_, cx| item.save(project, cx))?.await?,
Some(1) => pane.update(cx, |_, cx| item.reload(project, cx)).await?, Some(1) => pane.update(cx, |_, cx| item.reload(project, cx))?.await?,
_ => return Ok(false), _ => return Ok(false),
} }
} else if is_dirty && (can_save || is_singleton) { } else if is_dirty && (can_save || is_singleton) {
@ -1125,7 +1128,7 @@ impl Pane {
DIRTY_MESSAGE, DIRTY_MESSAGE,
&["Save", "Don't Save", "Cancel"], &["Save", "Don't Save", "Cancel"],
) )
}); })?;
match answer.next().await { match answer.next().await {
Some(0) => true, Some(0) => true,
Some(1) => false, Some(1) => false,
@ -1137,7 +1140,7 @@ impl Pane {
if should_save { if should_save {
if can_save { if can_save {
pane.update(cx, |_, cx| item.save(project, cx)).await?; pane.update(cx, |_, cx| item.save(project, cx))?.await?;
} else if is_singleton { } else if is_singleton {
let start_abs_path = project let start_abs_path = project
.read_with(cx, |project, cx| { .read_with(cx, |project, cx| {
@ -1148,7 +1151,7 @@ impl Pane {
let mut abs_path = cx.update(|cx| cx.prompt_for_new_path(&start_abs_path)); let mut abs_path = cx.update(|cx| cx.prompt_for_new_path(&start_abs_path));
if let Some(abs_path) = abs_path.next().await.flatten() { if let Some(abs_path) = abs_path.next().await.flatten() {
pane.update(cx, |_, cx| item.save_as(project, abs_path, cx)) pane.update(cx, |_, cx| item.save_as(project, abs_path, cx))?
.await?; .await?;
} else { } else {
return Ok(false); return Ok(false);

View file

@ -131,7 +131,7 @@ impl SerializedPaneGroup {
)) ))
} }
SerializedPaneGroup::Pane(serialized_pane) => { SerializedPaneGroup::Pane(serialized_pane) => {
let pane = workspace.update(cx, |workspace, cx| workspace.add_pane(cx)); let pane = workspace.update(cx, |workspace, cx| workspace.add_pane(cx))?;
let active = serialized_pane.active; let active = serialized_pane.active;
serialized_pane serialized_pane
.deserialize_to(project, &pane, workspace_id, workspace, cx) .deserialize_to(project, &pane, workspace_id, workspace, cx)
@ -166,7 +166,7 @@ impl SerializedPane {
workspace_id: WorkspaceId, workspace_id: WorkspaceId,
workspace: &ViewHandle<Workspace>, workspace: &ViewHandle<Workspace>,
cx: &mut AsyncAppContext, cx: &mut AsyncAppContext,
) { ) -> Option<()> {
let mut active_item_index = None; let mut active_item_index = None;
for (index, item) in self.children.iter().enumerate() { for (index, item) in self.children.iter().enumerate() {
let project = project.clone(); let project = project.clone();
@ -186,14 +186,14 @@ impl SerializedPane {
item.kind item.kind
))) )))
} }
}) })?
.await .await
.log_err(); .log_err();
if let Some(item_handle) = item_handle { if let Some(item_handle) = item_handle {
workspace.update(cx, |workspace, cx| { workspace.update(cx, |workspace, cx| {
Pane::add_item(workspace, &pane_handle, item_handle, false, false, None, cx); Pane::add_item(workspace, &pane_handle, item_handle, false, false, None, cx);
}) });
} }
if item.active { if item.active {
@ -204,8 +204,10 @@ impl SerializedPane {
if let Some(active_item_index) = active_item_index { if let Some(active_item_index) = active_item_index {
pane_handle.update(cx, |pane, cx| { pane_handle.update(cx, |pane, cx| {
pane.activate_item(active_item_index, false, false, cx); pane.activate_item(active_item_index, false, false, cx);
}) })?;
} }
Some(())
} }
} }