Skip to content

armgit / ente

Public Imported from GitHub
0

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

wip useSyncExternalStore

Manav Rathi6f577d150a3d
diff --git a/web/packages/new/photos/components/MLSettings.tsx b/web/packages/new/photos/components/MLSettings.tsx
index 3502739a54..34b1284287 100644
--- a/web/packages/new/photos/components/MLSettings.tsx
+++ b/web/packages/new/photos/components/MLSettings.tsx
@@ -26,7 +26,7 @@ import {
Typography,
} from "@mui/material";
import { t } from "i18next";
-import React, { useEffect, useState } from "react";
+import React, { useEffect, useState, useSyncExternalStore } from "react";
import { Trans } from "react-i18next";
import type { NewAppContextPhotos } from "../types/context";
import { openURL } from "../utils/web";
@@ -375,6 +375,7 @@ const ManageML: React.FC<ManageMLProps> = ({
onDisableML,
setDialogBoxAttributesV2,
}) => {
+ const status = useSyncExternalStore();
const confirmDisableML = () => {
setDialogBoxAttributesV2({
title: pt("Disable ML search"),
@@ -464,13 +465,23 @@ const ManageML: React.FC<ManageMLProps> = ({
</Stack>
<Paper variant="outlined">
<Stack gap={4} px={2} py={2}>
- <Stack direction="row" gap={2} justifyContent={"space-between"}>
+ <Stack
+ direction="row"
+ gap={2}
+ justifyContent={"space-between"}
+ >
<Typography color="text.muted">Status</Typography>
<Typography>Indexing</Typography>
</Stack>
- <Stack direction="row" gap={2} justifyContent={"space-between"}>
+ <Stack
+ direction="row"
+ gap={2}
+ justifyContent={"space-between"}
+ >
<Typography color="text.muted">Processed</Typography>
- <Typography textAlign="right">33,000,000 / 13,000,000</Typography>
+ <Typography textAlign="right">
+ 33,000,000 / 13,000,000
+ </Typography>
</Stack>
</Stack>
</Paper>
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 9e6c3fdda5..683b6fd8d7 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -35,6 +35,13 @@ let _isMLEnabled = false;
/** Cached instance of the {@link ComlinkWorker} that wraps our web worker. */
let _comlinkWorker: ComlinkWorker<typeof MLWorker> | undefined;
+/**
+ * Subscriptions to {@link MLStatus}.
+ *
+ * See {@link mlStatusSubscribe}.
+ */
+let _mlStatusListeners: (() => void)[] = [];
+
/** Lazily created, cached, instance of {@link MLWorker}. */
const worker = async () => {
if (!_comlinkWorker) _comlinkWorker = await createComlinkWorker();
@@ -89,6 +96,7 @@ export const logoutML = async () => {
// contexts], it gets called first in the logout sequence, and then this
// function (`logoutML`) gets called at a later point in time.
_isMLEnabled = false;
+ _mlStatusListeners = [];
await clearMLDB();
};
@@ -193,7 +201,15 @@ const setIsMLEnabledLocally = (enabled: boolean) =>
* Now it tracks the status of ML in general (which includes faces + consent).
*/
const mlRemoteKey = "faceSearchEnabled";
+
+/**
+ * Return `true` if the flag to enable ML is set on remote.
+ */
export const getIsMLEnabledRemote = () => getRemoteFlag(mlRemoteKey);
+
+/**
+ * Update the remote flag that tracks ML status across the user's devices.
+ */
const updateIsMLEnabledRemote = (enabled: boolean) =>
updateRemoteFlag(mlRemoteKey, enabled);
@@ -234,41 +250,63 @@ export const indexNewUpload = (enteFile: EnteFile, uploadItem: UploadItem) => {
void worker().then((w) => w.onUpload(enteFile, uploadItem));
};
-export interface FaceIndexingStatus {
+export interface MLStatus {
/**
* Which phase we are in within the indexing pipeline when viewed across the
* user's entire library:
*
+ * - "paused": ML is currently paused on this device.
+ *
* - "scheduled": There are files we know of that have not been indexed.
*
- * - "indexing": The face indexer is currently running.
+ * - "indexing": The indexer is currently running.
*
- * - "clustering": All files we know of have been indexed, and we are now
+ * - "clustering": All file we know of have been indexed, and we are now
* clustering the faces that were found.
*
- * - "done": Face indexing and clustering is complete for the user's
+ * - "done": ML indexing and face clustering is complete for the user's
* library.
*/
- phase: "scheduled" | "indexing" | "clustering" | "done";
+ phase: "paused" | "scheduled" | "indexing" | "clustering" | "done";
/** The number of files that have already been indexed. */
nSyncedFiles: number;
/** The total number of files that are eligible for indexing. */
nTotalFiles: number;
}
+/**
+ * A function that can be used to subscribe to updates in the ML status.
+ *
+ * This, along with {@link mlStatusSnapshot}, are meant to be used as arguments
+ * to React's {@link useSyncExternalStore}.
+ *
+ * @param callback A function that will be invoked whenever the result of
+ * {@link mlStatusSnapshot} changes.
+ *
+ * @returns A function that can be used to clear the subscription.
+ */
+export const mlStatusSubscribe = (onChange: () => void): (() => void) => {
+ _mlStatusListeners.push(onChange);
+ return () => {
+ _mlStatusListeners = _mlStatusListeners.filter((v) => v != onChange);
+ };
+};
+
+
+
/**
* Return the current state of the face indexing pipeline.
*
* Precondition: ML must be enabled.
*/
-export const faceIndexingStatus = async (): Promise<FaceIndexingStatus> => {
+export const faceIndexingStatus = async (): Promise<MLStatus> => {
if (!isMLEnabled())
throw new Error("Cannot get indexing status when ML is not enabled");
const { indexedCount, indexableCount } = await indexableAndIndexedCounts();
const isIndexing = await (await worker()).isIndexing();
- let phase: FaceIndexingStatus["phase"];
+ let phase: MLStatus["phase"];
if (indexableCount > 0) {
phase = !isIndexing ? "scheduled" : "indexing";
} else {