Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Return the count of items indexed

Manav Rathi27a0d7707e89
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index b9156e4106..ce460d3610 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -109,7 +109,13 @@ export class MLWorker {
private liveQ: IndexableItem[] = [];
private idleTimeout: ReturnType<typeof setTimeout> | undefined;
private idleDuration = idleDurationStart; /* unit: seconds */
- private onNextIdles: (() => void)[] = [];
+ /** Resolvers for pending promises returned from calls to {@link index}. */
+ private onNextIdles: ((count: number) => void)[] = [];
+ /**
+ * Number of items processed since the last time {@link onNextIdles} was
+ * drained.
+ */
+ private countSinceLastIdle = 0;
/**
* Initialize a new {@link MLWorker}.
@@ -140,9 +146,12 @@ export class MLWorker {
* During a backfill, we first attempt to fetch ML data for files which
* don't have that data locally. If on fetching we find what we need, we
* save it locally. Otherwise we index them.
+ *
+ * @return The count of items processed since the last last time we were
+ * idle.
*/
index() {
- const nextIdle = new Promise<void>((resolve) =>
+ const nextIdle = new Promise<number>((resolve) =>
this.onNextIdles.push(resolve),
);
this.wakeUp();
@@ -225,17 +234,23 @@ export class MLWorker {
// Use the liveQ if present, otherwise get the next batch to backfill.
const items = liveQ.length ? liveQ : await this.backfillQ();
- const allSuccess = await indexNextBatch(
- items,
- ensure(this.electron),
- this.delegate,
- );
- if (allSuccess) {
- // Everything is running smoothly. Reset the idle duration.
- this.idleDuration = idleDurationStart;
- // And tick again.
- scheduleTick();
- return;
+ this.countSinceLastIdle += items.length;
+
+ // If there is items remaining,
+ if (items.length > 0) {
+ // Index them.
+ const allSuccess = await indexNextBatch(
+ items,
+ ensure(this.electron),
+ this.delegate,
+ );
+ if (allSuccess) {
+ // Everything is running smoothly. Reset the idle duration.
+ this.idleDuration = idleDurationStart;
+ // And tick again.
+ scheduleTick();
+ return;
+ }
}
// We come here in three scenarios - either there is nothing left to do,
@@ -255,8 +270,10 @@ export class MLWorker {
// Resolve any awaiting promises returned from `index`.
const onNextIdles = this.onNextIdles;
+ const countSinceLastIdle = this.countSinceLastIdle;
this.onNextIdles = [];
- onNextIdles.forEach((f) => f());
+ this.countSinceLastIdle = 0;
+ onNextIdles.forEach((f) => f(countSinceLastIdle));
}
/** Return the next batch of items to backfill (if any). */
@@ -321,13 +338,13 @@ export class MLWorker {
expose(MLWorker);
/**
- * Find out files which need to be indexed. Then index the next batch of them.
+ * Index the given batch of items.
*
- * Returns `false` to indicate that either an error occurred, or there are no
- * more files to process, or that we cannot currently process files.
+ * Returns `false` to indicate that either an error occurred, or that we cannot
+ * currently process files since we don't have network connectivity.
*
- * Which means that when it returns true, all is well and there are more
- * things pending to process, so we should chug along at full speed.
+ * Which means that when it returns true, all is well and if there are more
+ * things pending to process, we should chug along at full speed.
*/
const indexNextBatch = async (
items: IndexableItem[],
@@ -342,9 +359,6 @@ const indexNextBatch = async (
return false;
}
- // Nothing to do.
- if (items.length == 0) return false;
-
// Keep track if any of the items failed.
let allSuccess = true;