Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use module state

Manav Rathidd3243492a70
diff --git a/web/packages/new/photos/services/face/index.ts b/web/packages/new/photos/services/face/index.ts
new file mode 100644
index 0000000000..50560db93c
--- /dev/null
+++ b/web/packages/new/photos/services/face/index.ts
@@ -0,0 +1,36 @@
+/**
+ * @file Main thread interface to {@link FaceWorker}.
+ */
+
+import { ComlinkWorker } from "@/next/worker/comlink-worker";
+import { FaceWorker } from "./worker";
+
+/** Cached instance of the {@link ComlinkWorker} that wraps our web worker. */
+let _comlinkWorker: ComlinkWorker<typeof FaceWorker> | undefined;
+
+/** Lazily created, cached, instance of {@link FaceWorker}. */
+export const faceWorker = async () => {
+ let comlinkWorker = _comlinkWorker;
+ if (!comlinkWorker) _comlinkWorker = comlinkWorker = createComlinkWorker();
+ return await comlinkWorker.remote;
+};
+
+const createComlinkWorker = () =>
+ new ComlinkWorker<typeof FaceWorker>(
+ "face",
+ new Worker(new URL("worker.ts", import.meta.url)),
+ );
+
+/**
+ * Terminate {@link faceWorker} (if any).
+ *
+ * This is useful during logout to immediately stop any background face related
+ * operations that are in-flight for the current user. After the user logs in
+ * again, a new {@link faceWorker} will be created on demand.
+ */
+export const terminateFaceWorker = () => {
+ if (_comlinkWorker) {
+ _comlinkWorker.terminate();
+ _comlinkWorker = undefined;
+ }
+};
diff --git a/web/packages/next/worker/comlink-worker.ts b/web/packages/next/worker/comlink-worker.ts
index a546409839..1a6645d334 100644
--- a/web/packages/next/worker/comlink-worker.ts
+++ b/web/packages/next/worker/comlink-worker.ts
@@ -25,7 +25,7 @@ export class ComlinkWorker<T extends new () => InstanceType<T>> {
public terminate() {
this.worker.terminate();
- log.debug(() => `Terminated ${this.name}`);
+ log.debug(() => `Terminated web worker ${this.name}`);
}
}