Clear stale projects if they no longer exist

This commit is contained in:
Kay Simmons 2022-12-16 15:45:17 -08:00
parent 81e3b48f37
commit 2c47bd4a97
3 changed files with 53 additions and 20 deletions

View file

@ -80,7 +80,7 @@ macro_rules! query {
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+); let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
self.select::<$return_type>(sql_stmt)?(()) self.select::<$return_type>(sql_stmt)?()
.context(::std::format!( .context(::std::format!(
"Error in {}, select_row failed to execute or parse for: {}", "Error in {}, select_row failed to execute or parse for: {}",
::std::stringify!($id), ::std::stringify!($id),
@ -95,7 +95,7 @@ macro_rules! query {
self.write(|connection| { self.write(|connection| {
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+); let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
connection.select::<$return_type>(sql_stmt)?(()) connection.select::<$return_type>(sql_stmt)?()
.context(::std::format!( .context(::std::format!(
"Error in {}, select_row failed to execute or parse for: {}", "Error in {}, select_row failed to execute or parse for: {}",
::std::stringify!($id), ::std::stringify!($id),

View file

@ -11,9 +11,7 @@ use highlighted_workspace_location::HighlightedWorkspaceLocation;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use settings::Settings; use settings::Settings;
use workspace::{OpenPaths, Workspace, WorkspaceLocation}; use workspace::{OpenPaths, Workspace, WorkspaceLocation, WORKSPACE_DB};
const RECENT_LIMIT: usize = 100;
actions!(recent_projects, [Toggle]); actions!(recent_projects, [Toggle]);
@ -30,14 +28,8 @@ struct RecentProjectsView {
} }
impl RecentProjectsView { impl RecentProjectsView {
fn new(cx: &mut ViewContext<Self>) -> Self { fn new(workspace_locations: Vec<WorkspaceLocation>, cx: &mut ViewContext<Self>) -> Self {
let handle = cx.weak_handle(); let handle = cx.weak_handle();
let workspace_locations: Vec<WorkspaceLocation> = workspace::WORKSPACE_DB
.recent_workspaces(RECENT_LIMIT)
.unwrap_or_default()
.into_iter()
.map(|(_, location)| location)
.collect();
Self { Self {
picker: cx.add_view(|cx| { picker: cx.add_view(|cx| {
Picker::new("Recent Projects...", handle, cx).with_max_size(800., 1200.) Picker::new("Recent Projects...", handle, cx).with_max_size(800., 1200.)
@ -48,12 +40,30 @@ impl RecentProjectsView {
} }
} }
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) { fn toggle(_: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
workspace.toggle_modal(cx, |_, cx| { cx.spawn(|workspace, mut cx| async move {
let view = cx.add_view(|cx| Self::new(cx)); let workspace_locations = cx
cx.subscribe(&view, Self::on_event).detach(); .background()
view .spawn(async {
}); WORKSPACE_DB
.recent_workspaces_on_disk()
.await
.unwrap_or_default()
.into_iter()
.map(|(_, location)| location)
.collect()
})
.await;
workspace.update(&mut cx, |workspace, cx| {
workspace.toggle_modal(cx, |_, cx| {
let view = cx.add_view(|cx| Self::new(workspace_locations, cx));
cx.subscribe(&view, Self::on_event).detach();
view
});
})
})
.detach();
} }
fn on_event( fn on_event(

View file

@ -196,14 +196,37 @@ impl WorkspaceDb {
} }
query! { query! {
pub fn recent_workspaces(limit: usize) -> Result<Vec<(WorkspaceId, WorkspaceLocation)>> { fn recent_workspaces() -> Result<Vec<(WorkspaceId, WorkspaceLocation)>> {
SELECT workspace_id, workspace_location SELECT workspace_id, workspace_location
FROM workspaces FROM workspaces
WHERE workspace_location IS NOT NULL WHERE workspace_location IS NOT NULL
ORDER BY timestamp DESC ORDER BY timestamp DESC
LIMIT ?
} }
} }
query! {
async fn delete_stale_workspace(id: WorkspaceId) -> Result<()> {
DELETE FROM workspaces
WHERE workspace_id IS ?
}
}
// Returns the recent locations which are still valid on disk and deletes ones which no longer
// exist.
pub async fn recent_workspaces_on_disk(&self) -> Result<Vec<(WorkspaceId, WorkspaceLocation)>> {
let mut result = Vec::new();
let mut delete_tasks = Vec::new();
for (id, location) in self.recent_workspaces()? {
if location.paths().iter().all(|path| dbg!(path).exists()) {
result.push((id, location));
} else {
delete_tasks.push(self.delete_stale_workspace(id));
}
}
futures::future::join_all(delete_tasks).await;
Ok(result)
}
fn get_center_pane_group(&self, workspace_id: WorkspaceId) -> Result<SerializedPaneGroup> { fn get_center_pane_group(&self, workspace_id: WorkspaceId) -> Result<SerializedPaneGroup> {
self.get_pane_group(workspace_id, None)? self.get_pane_group(workspace_id, None)?