Skip to content

Commit

Permalink
Only delete local if server query is not from cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Oct 14, 2023
1 parent 056c5da commit fb1197e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
17 changes: 8 additions & 9 deletions src/components/app/ProjectPreviewSet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,19 @@
goto(getProjectLink(project, true));
}}
delay={Math.random() * set.length * 50}
>{#if removeMeta}<div class="controls">
{#if edit}<Button
tip={edit.description}
action={() =>
edit ? edit.action(project) : undefined}
>{edit.label}</Button
>{/if}<ConfirmButton
><div class="controls">
{#if edit}<Button
tip={edit.description}
action={() => (edit ? edit.action(project) : undefined)}
>{edit.label}</Button
>{/if}{#if removeMeta}<ConfirmButton
prompt={removeMeta.prompt}
tip={removeMeta.description}
action={() =>
removeMeta ? removeMeta.action() : undefined}
>{removeMeta.label}</ConfirmButton
></div
>{/if}<slot /></ProjectPreview
>{/if}</div
><slot /></ProjectPreview
>
{/each}
</div>
Expand Down
33 changes: 16 additions & 17 deletions src/db/ProjectsDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,27 @@ export default class ProjectsDatabase {
const serialized: SerializedProject[] = [];
const deleted: string[] = [];
const projectIDs: Set<string> = 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,
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
10 changes: 6 additions & 4 deletions src/db/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/routes/projects/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fb1197e

Please sign in to comment.