From ba0f0d41654913258ba2fc0165e9f1deacfbf3f4 Mon Sep 17 00:00:00 2001 From: Laila Los <44241786+ElectronicBlueberry@users.noreply.github.com> Date: Thu, 12 Dec 2024 01:27:58 +0100 Subject: [PATCH] remove computed side-effect and simplify user scoped ref allow to sync existing ref --- client/src/composables/persistentRef.ts | 39 +++++++++++++--------- client/src/composables/userLocalStorage.ts | 36 ++++++++------------ 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/client/src/composables/persistentRef.ts b/client/src/composables/persistentRef.ts index bd925f7965f7..ab511f74caaa 100644 --- a/client/src/composables/persistentRef.ts +++ b/client/src/composables/persistentRef.ts @@ -24,37 +24,44 @@ function parse(value: string, type: "string" | "number" | "boolean" | "object }); } -export function usePersistentRef(key: string, defaultValue: string): Ref; -export function usePersistentRef(key: string, defaultValue: number): Ref; -export function usePersistentRef(key: string, defaultValue: T): Ref; -export function usePersistentRef( - key: string, - defaultValue: T -): Ref { - const storageSyncedRef = ref(defaultValue) as Ref; - +export function syncRefToLocalStorage(key: string, refToSync: Ref) { const stored = window.localStorage.getItem(key); + const sync = () => { + const stringified = stringify(refToSync.value); + window.localStorage.setItem(key, stringified); + }; + if (stored) { try { - storageSyncedRef.value = parse(stored, typeof defaultValue as "string" | "number" | "boolean" | "object"); + refToSync.value = parse(stored, typeof refToSync.value as "string" | "number" | "boolean" | "object"); } catch (e) { console.error(`Failed to parse value "${stored}" from local storage key "${key}". Resetting key`); - window.localStorage.removeItem(key); + sync(); } } else { - const stringified = stringify(storageSyncedRef.value); - window.localStorage.setItem(key, stringified); + sync(); } watch( - () => storageSyncedRef.value, + () => refToSync.value, () => { - const stringified = stringify(storageSyncedRef.value); - window.localStorage.setItem(key, stringified); + sync(); }, { deep: true } ); +} + +export function usePersistentRef(key: string, defaultValue: string): Ref; +export function usePersistentRef(key: string, defaultValue: number): Ref; +export function usePersistentRef(key: string, defaultValue: T): Ref; +export function usePersistentRef( + key: string, + defaultValue: T +): Ref { + const storageSyncedRef = ref(defaultValue) as Ref; + + syncRefToLocalStorage(key, storageSyncedRef); return storageSyncedRef; } diff --git a/client/src/composables/userLocalStorage.ts b/client/src/composables/userLocalStorage.ts index b0aa58b1a9ca..d81dfda55e09 100644 --- a/client/src/composables/userLocalStorage.ts +++ b/client/src/composables/userLocalStorage.ts @@ -1,9 +1,10 @@ -import { computed, customRef, type Ref, ref } from "vue"; +import { watchImmediate } from "@vueuse/core"; +import { type Ref, ref } from "vue"; import { type AnyUser } from "@/api"; import { useHashedUserId } from "./hashedUserId"; -import { usePersistentRef } from "./persistentRef"; +import { syncRefToLocalStorage } from "./persistentRef"; /** * Local storage composable specific to current user. @@ -13,27 +14,18 @@ import { usePersistentRef } from "./persistentRef"; export function useUserLocalStorage(key: string, initialValue: T, user?: Ref) { const { hashedUserId } = useHashedUserId(user); - let refSyncedRawValue = initialValue; + const refToSync = ref(initialValue); + let hasSynced = false; - const storedRef = computed(() => { - if (hashedUserId.value) { - return usePersistentRef(`${key}-${hashedUserId.value}`, refSyncedRawValue); - } else { - return ref(initialValue); + watchImmediate( + () => hashedUserId.value, + () => { + if (hashedUserId.value && !hasSynced) { + syncRefToLocalStorage(`${key}-${hashedUserId.value}`, refToSync); + hasSynced = true; + } } - }); + ); - const currentValue = customRef((track, trigger) => ({ - get() { - track(); - return storedRef.value.value; - }, - set(newValue) { - storedRef.value.value = newValue; - refSyncedRawValue = newValue as T; - trigger(); - }, - })); - - return currentValue; + return refToSync; }