Browse Source

Disentagle map from modifications

Manav Rathi 1 year ago
parent
commit
46d67f0c49
2 changed files with 8 additions and 7 deletions
  1. 2 2
      desktop/src/main/services/watch.ts
  2. 6 5
      desktop/src/main/stores/watch.ts

+ 2 - 2
desktop/src/main/services/watch.ts

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

+ 6 - 5
desktop/src/main/stores/watch.ts

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