Manav Rathi 1 سال پیش
والد
کامیت
f59ecdb8d8
2فایلهای تغییر یافته به همراه26 افزوده شده و 29 حذف شده
  1. 18 21
      web/apps/photos/src/services/watch.ts
  2. 8 8
      web/apps/photos/src/utils/file/index.ts

+ 18 - 21
web/apps/photos/src/services/watch.ts

@@ -38,8 +38,6 @@ class FolderWatcher {
      * If the file system directory corresponding to the (root) folder path of a
      * folder watch is deleted on disk, we note down that in this queue so that
      * we can ignore any file system events that come for it next.
-     *
-     * TODO: is this really needed? the mappings are pre-checked first.
      */
     private deletedFolderPaths: string[] = [];
     /** `true` if we are using the uploader. */
@@ -89,12 +87,12 @@ class FolderWatcher {
         this.syncWithDisk();
     }
 
-    /** `true` if we are currently using the uploader */
+    /** Return `true` if we are currently using the uploader. */
     isUploadRunning() {
         return this.uploadRunning;
     }
 
-    /** `true` if syncing has been temporarily paused */
+    /** Return `true` if syncing has been temporarily paused. */
     isSyncPaused() {
         return this.isPaused;
     }
@@ -501,31 +499,30 @@ class FolderWatcher {
         this.eventQueue = this.eventQueue.filter(
             (event) => !event.filePath.startsWith(deletedFolderPath),
         );
+
         return true;
     }
 
     private async moveToTrash(syncedFiles: FolderWatch["syncedFiles"]) {
+        const syncedFileForID = new Map<number, FolderWatchSyncedFile>();
+        for (const file of syncedFiles)
+            syncedFileForID.set(file.uploadedFileID, file);
+
         const files = await getLocalFiles();
-        const toTrashFilesMap = new Map<number, FolderWatchSyncedFile>();
-        for (const file of syncedFiles) {
-            toTrashFilesMap.set(file.uploadedFileID, file);
-        }
         const filesToTrash = files.filter((file) => {
-            if (toTrashFilesMap.has(file.id)) {
-                const fileToTrash = toTrashFilesMap.get(file.id);
-                if (fileToTrash.collectionID === file.collectionID) {
-                    return true;
-                }
+            const correspondingSyncedFile = syncedFileForID.get(file.id);
+            if (
+                correspondingSyncedFile &&
+                correspondingSyncedFile.collectionID == file.collectionID
+            ) {
+                return true;
             }
+            return false;
         });
-        const groupFilesByCollectionId =
-            groupFilesBasedOnCollectionID(filesToTrash);
-
-        for (const [
-            collectionID,
-            filesToTrash,
-        ] of groupFilesByCollectionId.entries()) {
-            await removeFromCollection(collectionID, filesToTrash);
+
+        const filesByCollectionID = groupFilesBasedOnCollectionID(filesToTrash);
+        for (const [id, files] of filesByCollectionID.entries()) {
+            await removeFromCollection(id, files);
         }
 
         this.requestSyncWithRemote();

+ 8 - 8
web/apps/photos/src/utils/file/index.ts

@@ -132,16 +132,16 @@ export async function downloadFile(file: EnteFile) {
     }
 }
 
-export function groupFilesBasedOnCollectionID(files: EnteFile[]) {
-    const collectionWiseFiles = new Map<number, EnteFile[]>();
+/** Segment the given {@link files} into lists indexed by their collection ID */
+export const groupFilesBasedOnCollectionID = (files: EnteFile[]) => {
+    const result = new Map<number, EnteFile[]>();
     for (const file of files) {
-        if (!collectionWiseFiles.has(file.collectionID)) {
-            collectionWiseFiles.set(file.collectionID, []);
-        }
-        collectionWiseFiles.get(file.collectionID).push(file);
+        const id = file.collectionID;
+        if (!result.has(id)) result.set(id, []);
+        result.get(id).push(file);
     }
-    return collectionWiseFiles;
-}
+    return result;
+};
 
 function getSelectedFileIds(selectedFiles: SelectedState) {
     const filesIDs: number[] = [];