Skip to content

armgit / ente

Public Imported from GitHub
0

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

In memory flag

Manav Rathi9e69798d793d
diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx
index 9c2f75c514..f01a540f85 100644
--- a/web/apps/photos/src/pages/_app.tsx
+++ b/web/apps/photos/src/pages/_app.tsx
@@ -1,6 +1,7 @@
import type { AccountsContextT } from "@/accounts/types/context";
import DownloadManager from "@/new/photos/services/download";
import {
+ initML,
isFaceIndexingEnabled,
setIsFaceIndexingEnabled,
} from "@/new/photos/services/ml";
@@ -199,6 +200,7 @@ export default function App({ Component, pageProps }: AppProps) {
const enabled = isFaceIndexingEnabled();
setMlSearchEnabled(enabled);
mlWorkManager.setMlSearchEnabled(enabled);
+ initML();
} catch (e) {
log.error("Error while loading mlSearchEnabled", e);
}
diff --git a/web/apps/photos/src/services/logout.ts b/web/apps/photos/src/services/logout.ts
index 6b0c9c9f88..99bed49ba9 100644
--- a/web/apps/photos/src/services/logout.ts
+++ b/web/apps/photos/src/services/logout.ts
@@ -1,8 +1,7 @@
import { accountLogout } from "@/accounts/services/logout";
import DownloadManager from "@/new/photos/services/download";
import { clearFeatureFlagSessionState } from "@/new/photos/services/feature-flags";
-import { terminateMLWorker } from "@/new/photos/services/ml";
-import { clearFaceDB } from "@/new/photos/services/ml/db";
+import { logoutML, terminateMLWorker } from "@/new/photos/services/ml";
import mlWorkManager from "@/new/photos/services/ml/mlWorkManager";
import log from "@/next/log";
import { clipService } from "services/clip-service";
@@ -65,9 +64,9 @@ export const photosLogout = async () => {
}
try {
- await clearFaceDB();
+ await logoutML();
} catch (e) {
- ignoreError("face", e);
+ ignoreError("ML", e);
}
try {
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 3a8ddebea7..14ad9f73c4 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -11,6 +11,7 @@ import {
getLocalTrashedFiles,
} from "@/new/photos/services/files";
import {
+ clearFaceDB,
faceIndex,
indexableFileIDs,
indexedAndIndexableCounts,
@@ -21,6 +22,19 @@ import { ComlinkWorker } from "@/next/worker/comlink-worker";
import { ensure } from "@/utils/ensure";
import { MLWorker } from "./worker";
+/**
+ * In-memory flag that tracks if ML is enabled.
+ *
+ * - On app start, this is read from local storage in the `initML` function.
+ *
+ * - If the user updates their preference, then `setMLEnabled` will get called
+ * with the updated preference where this value will be updated (in addition
+ * to updating local storage).
+ *
+ * - It is cleared in `logoutML`.
+ */
+let _isMLEnabled = false;
+
/** Cached instance of the {@link ComlinkWorker} that wraps our web worker. */
let _comlinkWorker: ComlinkWorker<typeof MLWorker> | undefined;
@@ -48,6 +62,23 @@ export const terminateMLWorker = () => {
}
};
+/**
+ * Initialize the ML subsystem if the user has enabled it in preferences.
+ */
+export const initML = () => {
+ // TODO-ML: Rename
+ _isMLEnabled = isFaceIndexingEnabled();
+};
+
+export const logoutML = async () => {
+ // `terminateMLWorker` is conceptually also part of this, but for the
+ // reasons mentioned in [Note: Caching IDB instances in separate execution
+ // contexts], it gets called first in the logout sequence, and this this
+ // `logoutML` gets called at a later point in time.
+ _isMLEnabled = false;
+ await clearFaceDB();
+};
+
export interface FaceIndexingStatus {
/**
* Which phase we are in within the indexing pipeline when viewed across the
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index 41067db86e..c9e98b8eb8 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -72,9 +72,9 @@ export class MLWorker {
private async tick() {
// Schedule a new macrotask (by using setTimeout) instead of scheduling
// a new microtask (by directly resolving the promise). This is likely
- // unnecessary; I'm doing this as a partially out of superstition aiming
- // to to give GC a chance to run if needed, and generally ease execution
- // and memory pressure.
+ // unnecessary; I'm doing this partially out of superstition, aiming to
+ // to give GC a chance to run if needed, and also generally ease
+ // execution and memory pressure.
const next = () => setTimeout(() => this.tick(), 0);
// If we've been asked to sync, do that irrespective of anything else.