- Do not clear temp files that are locked. (#6214)

- Handle edge cases in sync workspace
This commit is contained in:
Satish Gandham
2024-12-17 17:46:24 +05:30
committed by GitHub
parent 1a715c98b2
commit ede4aad55b
4 changed files with 46 additions and 22 deletions

View File

@@ -59,10 +59,10 @@ export class Storage {
this.workspaceSlug = "";
};
clearStorage = async () => {
clearStorage = async (force = false) => {
try {
await this.db.close();
await clearOPFS();
await this.db?.close();
await clearOPFS(force);
this.reset();
} catch (e) {
console.error("Error clearing sqlite sync storage", e);

View File

@@ -109,7 +109,7 @@ const syncLabels = async (currentLabels: any) => {
const currentIdList = currentLabels.map((label: any) => label.id);
const existingLabels = await persistence.db.exec("SELECT id FROM labels;");
const existingIdList = existingLabels.map((label: any) => label.id);
const existingIdList = existingLabels ? existingLabels.map((label: any) => label.id) : [];
const deletedIds = difference(existingIdList, currentIdList);
@@ -140,7 +140,7 @@ export const syncIssuesWithDeletedLabels = async (deletedLabelIds: string[]) =>
const syncModules = async (currentModules: any) => {
const currentIdList = currentModules.map((module: any) => module.id);
const existingModules = await persistence.db.exec("SELECT id FROM modules;");
const existingIdList = existingModules.map((module: any) => module.id);
const existingIdList = existingModules ? existingModules.map((module: any) => module.id) : [];
const deletedIds = difference(existingIdList, currentIdList);
await syncIssuesWithDeletedModules(deletedIds as string[]);
};
@@ -167,7 +167,7 @@ export const syncIssuesWithDeletedModules = async (deletedModuleIds: string[]) =
const syncCycles = async (currentCycles: any) => {
const currentIdList = currentCycles.map((cycle: any) => cycle.id);
const existingCycles = await persistence.db.exec("SELECT id FROM cycles;");
const existingIdList = existingCycles.map((cycle: any) => cycle.id);
const existingIdList = existingCycles ? existingCycles.map((cycle: any) => cycle.id) : [];
const deletedIds = difference(existingIdList, currentIdList);
await syncIssuesWithDeletedCycles(deletedIds as string[]);
};
@@ -194,7 +194,7 @@ export const syncIssuesWithDeletedCycles = async (deletedCycleIds: string[]) =>
const syncStates = async (currentStates: any) => {
const currentIdList = currentStates.map((state: any) => state.id);
const existingStates = await persistence.db.exec("SELECT id FROM states;");
const existingIdList = existingStates.map((state: any) => state.id);
const existingIdList = existingStates ? existingStates.map((state: any) => state.id) : [];
const deletedIds = difference(existingIdList, currentIdList);
await syncIssuesWithDeletedStates(deletedIds as string[]);
};
@@ -221,7 +221,7 @@ export const syncIssuesWithDeletedStates = async (deletedStateIds: string[]) =>
const syncMembers = async (currentMembers: any) => {
const currentIdList = currentMembers.map((member: any) => member.id);
const existingMembers = await persistence.db.exec("SELECT id FROM members;");
const existingIdList = existingMembers.map((member: any) => member.id);
const existingIdList = existingMembers ? existingMembers.map((member: any) => member.id) : [];
const deletedIds = difference(existingIdList, currentIdList);
await syncIssuesWithDeletedMembers(deletedIds as string[]);
};

View File

@@ -160,24 +160,48 @@ const parseSQLite3Error = (error: any) => {
return error;
};
export const clearOPFS = async () => {
const storageManager = window.navigator.storage;
const fileSystemDirectoryHandle = await storageManager.getDirectory();
export const isChrome = () => {
const userAgent = navigator.userAgent;
const isChrome = userAgent.includes("Chrome") && !userAgent.includes("Edg") && !userAgent.includes("OPR");
return userAgent.includes("Chrome") && !userAgent.includes("Edg") && !userAgent.includes("OPR");
};
if (isChrome) {
await (fileSystemDirectoryHandle as any).remove({ recursive: true });
export const clearOPFS = async (force = false) => {
const storageManager = window.navigator.storage;
const root = await storageManager.getDirectory();
if (force && isChrome()) {
await (root as any).remove({ recursive: true });
return;
}
// ts-ignore
for await (const entry of root.values()) {
if (entry.kind === "directory" && entry.name.startsWith(".ahp-")) {
// A lock with the same name as the directory protects it from
// being deleted.
const entries = await (fileSystemDirectoryHandle as any).entries();
for await (const entry of entries) {
const [name] = entry;
try {
await fileSystemDirectoryHandle.removeEntry(name);
} catch (e) {
console.log("Error", e);
if (force) {
// don't wait for the lock
try {
await root.removeEntry(entry.name, { recursive: true });
} catch (e) {
console.log(e);
}
} else {
await navigator.locks.request(entry.name, { ifAvailable: true }, async (lock) => {
if (lock) {
log?.(`Deleting temporary directory ${entry.name}`);
try {
await root.removeEntry(entry.name, { recursive: true });
} catch (e) {
console.log(e);
}
} else {
log?.(`Temporary directory ${entry.name} is in use`);
}
});
}
} else {
root.removeEntry(entry.name);
}
}
};

View File

@@ -231,7 +231,7 @@ export class UserStore implements IUserStore {
*/
signOut = async (): Promise<void> => {
await this.authService.signOut(API_BASE_URL);
await persistence.clearStorage();
await persistence.clearStorage(true);
this.store.resetOnSignOut();
};