mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
fileStorage: add didDeleteFile and didFailToDeleteFile actions
This will allow handling errors if removing a file from storage fails.
This commit is contained in:
@@ -78,12 +78,37 @@ export const fileStorageDidFailToWriteFile = createAction(
|
||||
}),
|
||||
);
|
||||
|
||||
/** Request to delete a file from storage. */
|
||||
/**
|
||||
* Request to delete a file from storage.
|
||||
* @param fileName The name of the file to delete.
|
||||
*/
|
||||
export const fileStorageDeleteFile = createAction((fileName: string) => ({
|
||||
type: 'fileStorage.action.deleteFile',
|
||||
fileName,
|
||||
}));
|
||||
|
||||
/**
|
||||
* Indicates that fileStorageDeleteFile(fileName) succeeded.
|
||||
* @param fileName The name of the file that was deleted.
|
||||
*/
|
||||
export const fileStorageDidDeleteFile = createAction((fileName: string) => ({
|
||||
type: 'fileStorage.action.didDeleteFile',
|
||||
fileName,
|
||||
}));
|
||||
|
||||
/**
|
||||
* Indicates that fileStorageDeleteFile(fileName) failed.
|
||||
* @param fileName The name of the file that should have been deleted.
|
||||
* @param error The error.
|
||||
*/
|
||||
export const fileStorageDidFailToDeleteFile = createAction(
|
||||
(fileName: string, error: Error) => ({
|
||||
type: 'fileStorage.action.didFailToDeleteFile',
|
||||
fileName,
|
||||
error,
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* Request to export (download) a file.
|
||||
* @param fileName The name of the file.
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
fileStorageDeleteFile,
|
||||
fileStorageDidArchiveAllFiles,
|
||||
fileStorageDidChangeItem,
|
||||
fileStorageDidDeleteFile,
|
||||
fileStorageDidExportFile,
|
||||
fileStorageDidFailToArchiveAllFiles,
|
||||
fileStorageDidFailToExportFile,
|
||||
@@ -127,7 +128,10 @@ it('should delete files', async () => {
|
||||
saga.put(fileStorageDeleteFile(testFileName));
|
||||
|
||||
const action = await saga.take();
|
||||
expect(action).toEqual(fileStorageDidRemoveItem(testFileName));
|
||||
expect(action).toEqual(fileStorageDidDeleteFile(testFileName));
|
||||
|
||||
const action2 = await saga.take();
|
||||
expect(action2).toEqual(fileStorageDidRemoveItem(testFileName));
|
||||
|
||||
await saga.end();
|
||||
});
|
||||
|
||||
@@ -15,8 +15,10 @@ import {
|
||||
fileStorageDeleteFile,
|
||||
fileStorageDidArchiveAllFiles,
|
||||
fileStorageDidChangeItem,
|
||||
fileStorageDidDeleteFile,
|
||||
fileStorageDidExportFile,
|
||||
fileStorageDidFailToArchiveAllFiles,
|
||||
fileStorageDidFailToDeleteFile,
|
||||
fileStorageDidFailToExportFile,
|
||||
fileStorageDidFailToInitialize,
|
||||
fileStorageDidFailToReadFile,
|
||||
@@ -156,7 +158,12 @@ function* handleDeleteFile(
|
||||
files: LocalForage,
|
||||
action: ReturnType<typeof fileStorageDeleteFile>,
|
||||
) {
|
||||
yield* call(() => files.removeItem(action.fileName));
|
||||
try {
|
||||
yield* call(() => files.removeItem(action.fileName));
|
||||
yield* put(fileStorageDidDeleteFile(action.fileName));
|
||||
} catch (err) {
|
||||
yield* put(fileStorageDidFailToDeleteFile(action.fileName, ensureError(err)));
|
||||
}
|
||||
}
|
||||
|
||||
function* handleArchiveAllFiles(files: LocalForage): Generator {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
"failedToInitialize": "Failed to initial file storage. Changes will not be automatically saved.",
|
||||
"failedToRead": "Failed to read file.",
|
||||
"failedToWrite": "Failed to write file.",
|
||||
"failedToDelete": "Failed to delete file.",
|
||||
"failedToExport": "Failed to export file.'"
|
||||
},
|
||||
"flashFirmware": {
|
||||
|
||||
@@ -16,6 +16,7 @@ export enum MessageId {
|
||||
FileStorageFailedToInitialize = 'fileStorage.failedToInitialize',
|
||||
FileStorageFailedToRead = 'fileStorage.failedToRead',
|
||||
FileStorageFailedToWrite = 'fileStorage.failedToWrite',
|
||||
FileStorageFailedToDelete = 'fileStorage.failedToDelete',
|
||||
FileStorageFailedToExport = 'fileStorage.failedToExport',
|
||||
FlashFirmwareTimedOut = 'flashFirmware.timedOut',
|
||||
FlashFirmwareBleError = 'flashFirmware.bleError',
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
import { didFailToSaveAs } from '../editor/actions';
|
||||
import {
|
||||
fileStorageDidFailToArchiveAllFiles,
|
||||
fileStorageDidFailToDeleteFile,
|
||||
fileStorageDidFailToExportFile,
|
||||
fileStorageDidFailToInitialize,
|
||||
fileStorageDidFailToReadFile,
|
||||
@@ -95,6 +96,7 @@ test.each([
|
||||
fileStorageDidFailToInitialize(new Error('test error')),
|
||||
fileStorageDidFailToReadFile('test.file', new Error('test error')),
|
||||
fileStorageDidFailToWriteFile('test.file', new Error('test error')),
|
||||
fileStorageDidFailToDeleteFile('test.file', new Error('test error')),
|
||||
fileStorageDidFailToExportFile('test.file', new Error('test error')),
|
||||
fileStorageDidFailToArchiveAllFiles(new Error('test error')),
|
||||
])('actions that should show notification: %o', async (action: AnyAction) => {
|
||||
|
||||
Reference in New Issue
Block a user