Manav Rathi 1 tahun lalu
induk
melakukan
722cc74e64
1 mengubah file dengan 26 tambahan dan 11 penghapusan
  1. 26 11
      web/apps/photos/src/services/watch.ts

+ 26 - 11
web/apps/photos/src/services/watch.ts

@@ -561,23 +561,26 @@ class FolderWatcher {
         }
         }
     }
     }
 
 
-    // Batches all the files to be uploaded (or trashed) from the
-    // event queue of same collection as the next event
-    private clubSameCollectionEvents(): EventQueueItem {
+    /**
+     * Batch the next run of events with the same action, collection and folder
+     * path into a single clubbed event that contains the list of all effected
+     * file paths from the individual events.
+     */
+    private dequeueClubbedEvent(): ClubbedWatchEvent | undefined {
         const event = this.eventQueue.shift();
         const event = this.eventQueue.shift();
+        if (!event) return undefined;
+
+        const filePaths = [event.filePath];
         while (
         while (
             this.eventQueue.length > 0 &&
             this.eventQueue.length > 0 &&
-            event.collectionName === this.eventQueue[0].collectionName &&
-            event.type === this.eventQueue[0].type
+            event.action === this.eventQueue[0].action &&
+            event.folderPath === this.eventQueue[0].folderPath &&
+            event.collectionName === this.eventQueue[0].collectionName
         ) {
         ) {
-            if (event.type === "trash") {
-                event.paths = [...event.paths, ...this.eventQueue[0].paths];
-            } else {
-                event.files = [...event.files, ...this.eventQueue[0].files];
-            }
+            filePaths.push(this.eventQueue[0].filePath);
             this.eventQueue.shift();
             this.eventQueue.shift();
         }
         }
-        return event;
+        return { ...event, filePaths };
     }
     }
 }
 }
 
 
@@ -614,6 +617,18 @@ interface WatchEvent {
     filePath: string;
     filePath: string;
 }
 }
 
 
+/**
+ * A composite of multiple {@link WatchEvent}s that only differ in their
+ * {@link filePath}.
+ *
+ * When processing events, we combine a run of events with the same
+ * {@link action}, {@link folderPath} and {@link collectionName}. This allows us
+ * to process all the affected {@link filePaths} in one shot.
+ */
+type ClubbedWatchEvent = Omit<WatchEvent, "filePath"> & {
+    filePaths: string[];
+};
+
 export function getValidFilesToUpload(
 export function getValidFilesToUpload(
     files: ElectronFile[],
     files: ElectronFile[],
     mapping: FolderWatch,
     mapping: FolderWatch,