This commit is contained in:
Eric Bailey
2025-08-11 15:04:13 -05:00
parent 2d9c0edbd4
commit 3f871a906f
+20 -14
View File
@@ -1,4 +1,4 @@
import {useCallback, useEffect, useState, useRef} from 'react' import {useCallback, useEffect, useState, useRef, useId} from 'react'
import {MMKV} from 'react-native-mmkv' import {MMKV} from 'react-native-mmkv'
import {type Account, type Device} from '#/storage/schema' import {type Account, type Device} from '#/storage/schema'
@@ -112,6 +112,12 @@ export function useStorage<
(data: StorageSchema<Store>[Key]) => void, (data: StorageSchema<Store>[Key]) => void,
] { ] {
type Schema = StorageSchema<Store> type Schema = StorageSchema<Store>
const id = useId()
if (debug) {
debug = `${debug} - ${id}`
}
const initialized = useRef(false) const initialized = useRef(false)
const [value, setValue] = useState<Schema[Key] | undefined>(() => { const [value, setValue] = useState<Schema[Key] | undefined>(() => {
if (debug) logger.debug(`${debug} - init`, { if (debug) logger.debug(`${debug} - init`, {
@@ -120,19 +126,19 @@ export function useStorage<
return storage.get(scopes) return storage.get(scopes)
}) })
useEffect(() => { // useEffect(() => {
if (!initialized.current) { // if (!initialized.current) {
const raw = storage.get(scopes) // const raw = storage.get(scopes)
if (value !== raw) { // if (value !== raw) {
logger.debug(`${debug} - syncing initial`, { // logger.debug(`${debug} - syncing initial`, {
value: value ?? 'unset', // value: value ?? 'unset',
raw: raw ?? 'unset', // raw: raw ?? 'unset',
}) // })
initialized.current = true // setValue(raw)
setValue(raw) // }
} // initialized.current = true
} // }
}, [value, scopes]) // }, [value, scopes])
useEffect(() => { useEffect(() => {
const sub = storage.addOnValueChangedListener(scopes, () => { const sub = storage.addOnValueChangedListener(scopes, () => {