Skip to content

Commit 8e7e54a

Browse files
committed
In storage API, rename deleteFile to delete.
Changed client_side_storage implementation of delete to work for directories and files. Update project to call storage.delete for a whole project and for a single module.
1 parent f511e85 commit 8e7e54a

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

src/storage/client_side_storage.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class ClientSideStorage implements commonStorage.Storage {
216216
return this.renameFile(oldPath, newPath);
217217
}
218218

219-
async renameDirectory(oldPath: string, newPath: string): Promise<void> {
219+
private async renameDirectory(oldPath: string, newPath: string): Promise<void> {
220220
return new Promise((resolve, reject) => {
221221
const transaction = this.db.transaction([FILES_STORE_NAME], 'readwrite');
222222
transaction.oncomplete = () => {
@@ -265,7 +265,7 @@ class ClientSideStorage implements commonStorage.Storage {
265265
});
266266
}
267267

268-
async renameFile(oldPath: string, newPath: string): Promise<void> {
268+
private async renameFile(oldPath: string, newPath: string): Promise<void> {
269269
return new Promise((resolve, reject) => {
270270
const transaction = this.db.transaction([FILES_STORE_NAME], 'readwrite');
271271
transaction.oncomplete = () => {
@@ -366,7 +366,52 @@ class ClientSideStorage implements commonStorage.Storage {
366366
});
367367
}
368368

369-
async deleteFile(filePath: string): Promise<void> {
369+
async delete(path: string): Promise<void> {
370+
if (path.endsWith('/')) {
371+
return this.deleteDirectory(path);
372+
}
373+
return this.deleteFile(path);
374+
}
375+
376+
private async deleteDirectory(path: string): Promise<void> {
377+
return new Promise((resolve, reject) => {
378+
const transaction = this.db.transaction([FILES_STORE_NAME], 'readwrite');
379+
transaction.oncomplete = () => {
380+
resolve();
381+
};
382+
transaction.onabort = () => {
383+
console.log('IndexedDB transaction aborted.');
384+
reject(new Error('IndexedDB transaction aborted.'));
385+
};
386+
const filesObjectStore = transaction.objectStore(FILES_STORE_NAME);
387+
const openKeyCursorRequest = filesObjectStore.openKeyCursor();
388+
openKeyCursorRequest.onerror = () => {
389+
console.log('IndexedDB openKeyCursor request failed. openKeyCursorRequest.error is...');
390+
console.log(openKeyCursorRequest.error);
391+
throw new Error('IndexedDB openKeyCursor request failed.');
392+
};
393+
openKeyCursorRequest.onsuccess = () => {
394+
const cursor = openKeyCursorRequest.result;
395+
if (cursor && cursor.key) {
396+
const filePath: string = cursor.key as string;
397+
if (filePath.startsWith(path)) {
398+
const deleteRequest = filesObjectStore.delete(filePath);
399+
deleteRequest.onerror = () => {
400+
console.log('IndexedDB delete request failed. deleteRequest.error is...');
401+
console.log(deleteRequest.error);
402+
throw new Error('IndexedDB delete request failed.');
403+
};
404+
}
405+
cursor.continue();
406+
} else {
407+
// The cursor is done. We have finished reading all the files.
408+
resolve();
409+
}
410+
};
411+
});
412+
}
413+
414+
private async deleteFile(filePath: string): Promise<void> {
370415
return new Promise((resolve, reject) => {
371416
const transaction = this.db.transaction([FILES_STORE_NAME], 'readwrite');
372417
transaction.oncomplete = () => {

src/storage/common_storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ export interface Storage {
3636

3737
saveFile(filePath: string, fileContentText: string): Promise<void>;
3838

39-
deleteFile(filePath: string): Promise<void>;
39+
delete(path: string): Promise<void>;
4040
}

src/storage/project.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,7 @@ export async function copyProject(
182182
*/
183183
export async function deleteProject(
184184
storage: commonStorage.Storage, projectName: string): Promise<void> {
185-
const projectFileNames: string[] = await storage.list(
186-
storageNames.makeProjectDirectoryPath(projectName));
187-
188-
for (const projectFileName of projectFileNames) {
189-
const filePath = storageNames.makeFilePath(projectName, projectFileName);
190-
await storage.deleteFile(filePath);
191-
}
185+
await storage.delete(storageNames.makeProjectDirectoryPath(projectName));
192186
}
193187

194188
/**
@@ -250,7 +244,7 @@ export async function removeModuleFromProject(
250244
project.opModes = project.opModes.filter(o => o.modulePath !== modulePath);
251245
break;
252246
}
253-
await storage.deleteFile(modulePath);
247+
await storage.delete(modulePath);
254248
await saveProjectInfo(storage, project.projectName);
255249
}
256250
}

0 commit comments

Comments
 (0)