diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts index c568c8fac4..07cb348960 100644 --- a/web/packages/new/photos/services/ml/index.ts +++ b/web/packages/new/photos/services/ml/index.ts @@ -14,6 +14,7 @@ import type { UploadItem } from "ente-gallery/services/upload"; import type { EnteFile } from "ente-media/file"; import { FileType } from "ente-media/file-type"; import { throttled } from "ente-utils/promise"; +import pDebounce from "p-debounce"; import { getRemoteFlag, updateRemoteFlag } from "../remote-store"; import { setSearchPeople } from "../search"; import { @@ -385,8 +386,6 @@ export const mlSync = async () => { _state.isSyncing = false; }; -const workerDidUnawaitedIndex = () => void updateClustersAndPeople(); - const updateClustersAndPeople = async () => { const masterKey = await masterKeyFromSession(); @@ -400,6 +399,28 @@ const updateClustersAndPeople = async () => { await updatePeopleState(); }; +/** + * A debounced variant of {@link updateClustersAndPeople} suitable for use + * during potential in-progress uploads. + * + * The debounce uses a long interval (30 seconds) to avoid unnecessary reruns of + * the expensive clustering as individual files get uploaded. Usually we + * wouldn't get here as the live queue will keep getting refilled and the worker + * would keep ticking, but it is possible, depending on timing, for the queue to + * drain in the middle of uploads too. + * + * Ideally, we'd like to do the cluster update just once when the upload has + * completed, however currently we don't have access to {@link uploadManager} + * from here. So this gets us near that ideal, without adding too much impact or + * requiring us to be aware of the uploadManager status. + */ +const debounceUpdateClustersAndPeople = pDebounce( + updateClustersAndPeople, + 30 * 1e3, +); + +const workerDidUnawaitedIndex = () => void debounceUpdateClustersAndPeople(); + /** * Run indexing on a file which was uploaded from this client. *