Files
pybricks-code/src/fileStorage/reducers.ts
T
David Lechner 1515e5ae4f fileStorage: add file handles
This changes how files work. There is now an open function that
translates a path to a file handle (id). Then this handle is used
to perform other actions on the file. The file contents are moved
to a separate table so that the actual file storage is independent
of the database (e.g. in the future, we may use File Access API).

We store a hash of the file contents in the metadata file so we can
detect file changes without having to compare file contents.

We also separate the change and add actions.
2022-04-01 13:41:10 -05:00

41 lines
1001 B
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { Reducer, combineReducers } from 'redux';
import {
fileStorageDidAddItem,
fileStorageDidChangeItem,
fileStorageDidInitialize,
fileStorageDidRemoveItem,
} from './actions';
const isInitialized: Reducer<boolean> = (state = false, action) => {
if (fileStorageDidInitialize.matches(action)) {
return true;
}
return state;
};
const fileNames: Reducer<ReadonlyArray<string>> = (state = [], action) => {
if (fileStorageDidInitialize.matches(action)) {
return [...action.fileNames];
}
if (fileStorageDidAddItem.matches(action)) {
return [...state, action.id];
}
if (fileStorageDidChangeItem.matches(action)) {
return state;
}
if (fileStorageDidRemoveItem.matches(action)) {
return [...state].filter((value) => value !== action.id);
}
return state;
};
export default combineReducers({ isInitialized, fileNames });