From fb1197e0ba063e8116935cc04df945efb67f830b Mon Sep 17 00:00:00 2001 From: "Amy J. Ko" Date: Sat, 14 Oct 2023 11:32:55 -0700 Subject: [PATCH] Only delete local if server query is not from cache. --- src/components/app/ProjectPreviewSet.svelte | 17 +++++------ src/db/ProjectsDatabase.ts | 33 ++++++++++----------- src/db/firebase.ts | 10 ++++--- src/routes/projects/+page.svelte | 2 +- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/components/app/ProjectPreviewSet.svelte b/src/components/app/ProjectPreviewSet.svelte index e18a0129b..00b4d3576 100644 --- a/src/components/app/ProjectPreviewSet.svelte +++ b/src/components/app/ProjectPreviewSet.svelte @@ -42,20 +42,19 @@ goto(getProjectLink(project, true)); }} delay={Math.random() * set.length * 50} - >{#if removeMeta}
- {#if edit}{/if}
+ {#if edit}{/if}{#if removeMeta} removeMeta ? removeMeta.action() : undefined} >{removeMeta.label}
{/if}{/if}
{/each} diff --git a/src/db/ProjectsDatabase.ts b/src/db/ProjectsDatabase.ts index 7761cf400..b2c407f0d 100644 --- a/src/db/ProjectsDatabase.ts +++ b/src/db/ProjectsDatabase.ts @@ -175,25 +175,27 @@ export default class ProjectsDatabase { const serialized: SerializedProject[] = []; const deleted: string[] = []; const projectIDs: Set = new Set(); - snapshot.docChanges().forEach((change) => { - const project = change.doc.data() as SerializedProject; + + // First, go through the entire set, gathering the latest versions and remembering what project IDs we know + // so we can delete ones that are gone from the server. + snapshot.forEach((doc) => { + const project = doc.data() as SerializedProject; + serialized.push(project); projectIDs.add(project.id); + }); + + // Next, go through the changes and see if any were explicitly removed, and if so, delete them. + snapshot.docChanges().forEach((change) => { // Removed? Delete the local cache of the project. - if (change.type === 'removed') { - deleted.push(project.id); - } - // Add the project to the list of projects to track. - else { - serialized.push(project); - } + if (change.type === 'removed') deleted.push(change.doc.id); }); // Deserialize the projects and track them, if they're not already tracked for (const project of await this.deserializeAll(serialized)) this.track(project, true, PersistenceType.Online, true); - // Find all projects known locally that didn't appear in the query - // and were previously persisted. + // Find all projects 1) known locally, 2) that didn't appear in latest update + // 3) were previously marked as cloud persisted, and 4) aren't pending for (const [ projectID, history, @@ -204,8 +206,9 @@ export default class ProjectsDatabase { ) deleted.push(projectID); - // Delete the deleted - for (const id of deleted) await this.deleteLocalProject(id); + // Delete the deleted if the data was from the server. + if (!snapshot.metadata.fromCache) + for (const id of deleted) await this.deleteLocalProject(id); // Refresh stores after everything is added and deleted. this.refreshEditableProjects(); @@ -239,7 +242,6 @@ export default class ProjectsDatabase { ): ProjectHistory | undefined { if (editable) { // If we're not tracking this yet, create a history and store the version given. - // If persisted, request a save. let history = this.projectHistories.get(project.id); if (history === undefined) { history = new ProjectHistory(project, persist, saved); @@ -248,9 +250,6 @@ export default class ProjectsDatabase { // Update the editable projects this.refreshEditableProjects(); - // Defer a save. - // if (persist === PersistenceType.Online) this.saveSoon(); - // Return the history return history; } diff --git a/src/db/firebase.ts b/src/db/firebase.ts index 0c45426b5..bb5f95dd8 100644 --- a/src/db/firebase.ts +++ b/src/db/firebase.ts @@ -37,11 +37,13 @@ if (typeof process === 'undefined') { measurementId: PUBLIC_FIREBASE_MEASUREMENT_ID, }; + const uninitialized = getApps().length === 0; + // Initialize Firebase - const app = - getApps().length === 0 ? initializeApp(firebaseConfig) : getApp(); + const app = uninitialized ? initializeApp(firebaseConfig) : getApp(); auth = getAuth(app); + firestore = getFirestore(app); functions = getFunctions(app); analytics = getAnalytics(app); @@ -53,8 +55,8 @@ if (typeof process === 'undefined') { connectFunctionsEmulator(functions, 'localhost', 5001); } } catch (err) { - console.log('*** NO ACCESS TO FIREBASE ***'); - console.log(err); + console.error('*** NO ACCESS TO FIREBASE ***'); + console.error(err); } } diff --git a/src/routes/projects/+page.svelte b/src/routes/projects/+page.svelte index 0b798b0bc..093347fa5 100644 --- a/src/routes/projects/+page.svelte +++ b/src/routes/projects/+page.svelte @@ -94,7 +94,7 @@ edit={{ description: $locale.ui.page.projects.button.unarchive, action: (project) => Projects.archiveProject(project.id, false), - label: '👆🏻', + label: '↑🗑️', }} remove={(project) => $user && project.owner === $user.uid