Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ppom-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ export class PPOMController extends BaseControllerV2<
console.error(`Error in resetting ppom: ${error.message}`);
});
this.#clearDataFetchIntervals();
const storageMetadata = { ...this.state.storageMetadata };
this.update((draftState) => {
draftState.versionInfo = [];
const newChainStatus = { ...this.state.chainStatus };
Expand All @@ -491,6 +492,9 @@ export class PPOMController extends BaseControllerV2<
draftState.storageMetadata = [];
draftState.versionFileETag = '';
});
this.#storage.deleteAllFiles(storageMetadata).catch((error: Error) => {
console.error(`Error in deleting files: ${error.message}`);
});
}

/*
Expand Down
32 changes: 32 additions & 0 deletions src/ppom-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,36 @@ describe('PPOMStorage', () => {
});
});
});

describe('deleteAllFiles', () => {
it('should delete all files passed to it', async () => {
const mockDeleteFile = jest
.fn()
.mockImplementation(async () => Promise.resolve());
const storageBackend = buildStorageBackend({ delete: mockDeleteFile });
const ppomStorage = new PPOMStorage({
storageBackend,
readMetadata: () => [simpleFileData],
writeMetadata: () => undefined,
});
await ppomStorage.deleteAllFiles([simpleFileData]);
expect(mockDeleteFile).toHaveBeenCalledTimes(1);
});

it('should not throw error if delete fails', async () => {
const mockDeleteFile = jest
.fn()
.mockImplementation(async () =>
Promise.reject(new Error('some error')),
);
const storageBackend = buildStorageBackend({ delete: mockDeleteFile });
const ppomStorage = new PPOMStorage({
storageBackend,
readMetadata: () => [simpleFileData],
writeMetadata: () => undefined,
});
await ppomStorage.deleteAllFiles([simpleFileData]);
expect(mockDeleteFile).toHaveBeenCalledTimes(1);
});
});
});
16 changes: 16 additions & 0 deletions src/ppom-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ export class PPOMStorage {
return syncedMetadata;
}

/**
* Delete all files in storage.
*
* @param metadata - List of all files in storage.
*/
async deleteAllFiles(metadata: FileMetadataList): Promise<void> {
for (const fileMetadata of metadata) {
const { name, chainId } = fileMetadata;
try {
await this.#storageBackend.delete({ name, chainId });
} catch (exp: any) {
console.error(`Error in deleting file: ${name}, ${chainId}`, exp);
}
}
}

/**
* Read the file from the local storage.
* 1. Check if the file exists in the local storage.
Expand Down