From 62c12cd549c8bd5fb0c9793c36729d303c006af8 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 29 Apr 2024 20:27:01 -0600 Subject: [PATCH] Reduce frequency of workspace save (#11183) Co-Authored-By: Mikayla Release Notes: - N/A --------- Co-authored-by: Mikayla --- crates/workspace/src/item.rs | 2 +- crates/workspace/src/pane_group.rs | 4 +- crates/workspace/src/workspace.rs | 59 +++++++++++++++++++----------- crates/zed/src/zed.rs | 3 +- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index 246cc8fe2dee0..1108ac3d6f8ba 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -603,7 +603,7 @@ impl ItemHandle for View { } cx.defer(|workspace, cx| { - workspace.serialize_workspace(cx).detach(); + workspace.serialize_workspace(cx); }); } diff --git a/crates/workspace/src/pane_group.rs b/crates/workspace/src/pane_group.rs index 062bbbe02eece..1c9e1ef9f0605 100644 --- a/crates/workspace/src/pane_group.rs +++ b/crates/workspace/src/pane_group.rs @@ -749,7 +749,7 @@ mod element { } workspace - .update(cx, |this, cx| this.schedule_serialize(cx)) + .update(cx, |this, cx| this.serialize_workspace(cx)) .log_err(); cx.stop_propagation(); cx.refresh(); @@ -935,7 +935,7 @@ mod element { let mut borrow = flexes.lock(); *borrow = vec![1.; borrow.len()]; workspace - .update(cx, |this, cx| this.schedule_serialize(cx)) + .update(cx, |this, cx| this.serialize_workspace(cx)) .log_err(); cx.refresh(); diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 9b42e5da9fdfe..6cc5581c0f60d 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -631,7 +631,7 @@ impl Workspace { project::Event::WorktreeRemoved(_) | project::Event::WorktreeAdded => { this.update_window_title(cx); - this.serialize_workspace(cx).detach(); + this.serialize_workspace(cx); } project::Event::DisconnectedFromHost => { @@ -823,15 +823,15 @@ impl Workspace { ThemeSettings::reload_current_theme(cx); }), cx.observe(&left_dock, |this, _, cx| { - this.serialize_workspace(cx).detach(); + this.serialize_workspace(cx); cx.notify(); }), cx.observe(&bottom_dock, |this, _, cx| { - this.serialize_workspace(cx).detach(); + this.serialize_workspace(cx); cx.notify(); }), cx.observe(&right_dock, |this, _, cx| { - this.serialize_workspace(cx).detach(); + this.serialize_workspace(cx); cx.notify(); }), cx.on_release(|this, window, cx| { @@ -1897,7 +1897,7 @@ impl Workspace { } cx.notify(); - self.serialize_workspace(cx).detach(); + self.serialize_workspace(cx); } pub fn close_all_docks(&mut self, cx: &mut ViewContext) { @@ -1911,7 +1911,7 @@ impl Workspace { cx.focus_self(); cx.notify(); - self.serialize_workspace(cx).detach(); + self.serialize_workspace(cx); } /// Transfer focus to the panel of the given type. @@ -1934,6 +1934,8 @@ impl Workspace { cx: &mut ViewContext, should_focus: impl Fn(&dyn PanelHandle, &mut ViewContext) -> bool, ) -> Option> { + let mut result_panel = None; + let mut serialize = false; for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] { if let Some(panel_index) = dock.read(cx).panel_index_for_type::() { let mut focus_center = false; @@ -1956,12 +1958,18 @@ impl Workspace { self.active_pane.update(cx, |pane, cx| pane.focus(cx)) } - self.serialize_workspace(cx).detach(); - cx.notify(); - return panel; + result_panel = panel; + serialize = true; + break; } } - None + + if serialize { + self.serialize_workspace(cx); + } + + cx.notify(); + result_panel } /// Open the panel of the given type @@ -2559,7 +2567,7 @@ impl Workspace { } } - self.serialize_workspace(cx).detach(); + self.serialize_workspace(cx); } pub fn split_pane( @@ -3511,17 +3519,22 @@ impl Workspace { cx.notify(); } - fn schedule_serialize(&mut self, cx: &mut ViewContext) { - self._schedule_serialize = Some(cx.spawn(|this, mut cx| async move { - cx.background_executor() - .timer(Duration::from_millis(100)) - .await; - this.update(&mut cx, |this, cx| this.serialize_workspace(cx).detach()) + fn serialize_workspace(&mut self, cx: &mut ViewContext) { + if self._schedule_serialize.is_none() { + self._schedule_serialize = Some(cx.spawn(|this, mut cx| async move { + cx.background_executor() + .timer(Duration::from_millis(100)) + .await; + this.update(&mut cx, |this, cx| { + this.serialize_workspace_internal(cx).detach(); + this._schedule_serialize.take(); + }) .log_err(); - })); + })); + } } - fn serialize_workspace(&self, cx: &mut WindowContext) -> Task<()> { + fn serialize_workspace_internal(&self, cx: &mut WindowContext) -> Task<()> { fn serialize_pane_handle(pane_handle: &View, cx: &WindowContext) -> SerializedPane { let (items, active) = { let pane = pane_handle.read(cx); @@ -3741,9 +3754,11 @@ impl Workspace { })?; // Serialize ourself to make sure our timestamps and any pane / item changes are replicated - workspace.update(&mut cx, |workspace, cx| { - workspace.serialize_workspace(cx).detach() - })?; + workspace + .update(&mut cx, |workspace, cx| { + workspace.serialize_workspace_internal(cx).detach(); + }) + .ok(); Ok(opened_items) }) diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index dbd6a34774289..d0bb0c43d96f1 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -2842,11 +2842,12 @@ mod tests { handle_keymap_file_changes(keymap_rx, cx); }); workspace - .update(cx, |workspace, _| { + .update(cx, |workspace, cx| { workspace.register_action(|_, _: &A, _cx| {}); workspace.register_action(|_, _: &B, _cx| {}); workspace.register_action(|_, _: &ActivatePreviousPane, _cx| {}); workspace.register_action(|_, _: &ActivatePrevItem, _cx| {}); + cx.notify(); }) .unwrap(); executor.run_until_parked();