Skip to content

armgit / ente

Public Imported from GitHub
0

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

Reduce noise in UI layer

Manav Rathi72a3f7f17a14
diff --git a/web/apps/photos/src/pages/gallery/index.tsx b/web/apps/photos/src/pages/gallery/index.tsx
index 65857d5f0a..5062aa6b6f 100644
--- a/web/apps/photos/src/pages/gallery/index.tsx
+++ b/web/apps/photos/src/pages/gallery/index.tsx
@@ -87,7 +87,7 @@ import {
import downloadManager from "services/download";
import { syncCLIPEmbeddings } from "services/embeddingService";
import { syncEntities } from "services/entityService";
-import { fetchAndSaveFeatureFlags } from "services/feature-flag";
+import { fetchAndSaveFeatureFlagsIfNeeded } from "services/feature-flag";
import { getLocalFiles, syncFiles } from "services/fileService";
import locationSearchService from "services/locationSearchService";
import { getLocalTrashedFiles, syncTrash } from "services/trashService";
@@ -341,7 +341,6 @@ export default function Gallery() {
return;
}
preloadImage("/images/subscription-card-background");
- let ffTimeout: ReturnType<typeof setTimeout> | undefined;
const electron = globalThis.electron;
const main = async () => {
const valid = await validateKey();
@@ -385,11 +384,7 @@ export default function Gallery() {
syncInterval.current = setInterval(() => {
syncWithRemote(false, true);
}, SYNC_INTERVAL_IN_MICROSECONDS);
- // Not critical, so fetch these after some delay.
- ffTimeout = setTimeout(() => {
- ffTimeout = undefined;
- void fetchAndSaveFeatureFlags();
- }, 5000);
+ fetchAndSaveFeatureFlagsIfNeeded();
if (electron) {
// void clipService.setupOnFileUploadListener();
electron.onMainWindowFocus(() => syncWithRemote(false, true));
@@ -398,7 +393,6 @@ export default function Gallery() {
main();
return () => {
clearInterval(syncInterval.current);
- if (ffTimeout) clearTimeout(ffTimeout);
if (electron) {
electron.onMainWindowFocus(undefined);
clipService.removeOnFileUploadListener();
diff --git a/web/apps/photos/src/services/feature-flag.ts b/web/apps/photos/src/services/feature-flag.ts
index c6d722bf0f..ffde49a6a1 100644
--- a/web/apps/photos/src/services/feature-flag.ts
+++ b/web/apps/photos/src/services/feature-flag.ts
@@ -1,8 +1,40 @@
+let _fetchTimeout: ReturnType<typeof setTimeout> | undefined;
+let _haveFetched = false;
+
+/**
+ * Fetch feature flags (potentially user specific) from remote and save them in
+ * local storage for subsequent lookup.
+ *
+ * It fetches only once per session, and so is safe to call as arbitrarily many
+ * times. Remember to call {@link clearFeatureFlagSessionState} on logout to
+ * forget that we've already fetched so that these can be fetched again on the
+ * subsequent login.
+ */
+export const fetchAndSaveFeatureFlagsIfNeeded = () => {
+ if (_haveFetched) return;
+ if (_fetchTimeout) return;
+ // Not critical, so fetch these after some delay.
+ _fetchTimeout = setTimeout(() => {
+ _fetchTimeout = undefined;
+ void fetchAndSaveFeatureFlags().then(() => {
+ _haveFetched = true;
+ });
+ }, 5000);
+};
+
+export const clearFeatureFlagSessionState = () => {
+ if (_fetchTimeout) {
+ clearTimeout(_fetchTimeout);
+ _fetchTimeout = undefined;
+ }
+ _haveFetched = false;
+};
+
/**
* Fetch feature flags (potentially user specific) from remote and save them in
* local storage for subsequent lookup.
*/
-export const fetchAndSaveFeatureFlags = async () => {};
+const fetchAndSaveFeatureFlags = async () => {};
/**
* Return `true` if the current user is marked as an "internal" user.