Disentagle map from modifications

This commit is contained in:
Manav Rathi 2024-04-30 16:49:56 +05:30
parent 1076471d51
commit 46d67f0c49
No known key found for this signature in database
2 changed files with 8 additions and 7 deletions

View file

@ -73,7 +73,7 @@ export const watchAdd = async (
) => {
const watches = folderWatches();
if (!fsIsDir(folderPath))
if (!(await fsIsDir(folderPath)))
throw new Error(
`Attempting to add a folder watch for a folder path ${folderPath} that is not an existing directory`,
);
@ -97,7 +97,7 @@ export const watchAdd = async (
return watches;
};
export const watchRemove = async (watcher: FSWatcher, folderPath: string) => {
export const watchRemove = (watcher: FSWatcher, folderPath: string) => {
const watches = folderWatches();
const filtered = watches.filter((watch) => watch.folderPath != folderPath);
if (watches.length == filtered.length)

View file

@ -3,7 +3,7 @@ import { type FolderWatch } from "../../types/ipc";
import log from "../log";
interface WatchStore {
mappings: FolderWatchWithLegacyFields[];
mappings?: FolderWatchWithLegacyFields[];
}
type FolderWatchWithLegacyFields = FolderWatch & {
@ -54,7 +54,8 @@ export const watchStore = new Store({
*/
export const migrateLegacyWatchStoreIfNeeded = () => {
let needsUpdate = false;
const watches = watchStore.get("mappings").map((watch) => {
const updatedWatches = [];
for (const watch of watchStore.get("mappings") ?? []) {
let collectionMapping = watch.collectionMapping;
// The required type defines the latest schema, but before migration
// this'll be undefined, so tell ESLint to calm down.
@ -67,10 +68,10 @@ export const migrateLegacyWatchStoreIfNeeded = () => {
delete watch.rootFolderName;
needsUpdate = true;
}
return { ...watch, collectionMapping };
});
updatedWatches.push({ ...watch, collectionMapping });
}
if (needsUpdate) {
watchStore.set("mappings", watches);
watchStore.set("mappings", updatedWatches);
log.info("Migrated legacy watch store data to new schema");
}
};