ATProto forum built with ESAV

remember queries

rimar1337 0419bd96 e82d8d2b

Changed files
+29 -5
src
+3 -1
src/esav/atoms.ts
··· 66 } 67 set(logEntriesAtom, newLog); 68 } 69 - );
··· 66 } 67 set(logEntriesAtom, newLog); 68 } 69 + ); 70 + 71 + export const queryCacheAtom = atom<Record<string, QueryState>>({});
+26 -4
src/esav/hooks.ts
··· 6 queryStateFamily, 7 websocketAtom, 8 websocketStatusAtom, 9 - addLogEntryAtom 10 } from './atoms'; 11 import type { EsavDocument, QueryDoc, SubscribeMessage, UnsubscribeMessage } from './types'; 12 import { atomWithStorage } from 'jotai/utils'; ··· 34 const ws = useAtomValue(websocketAtom); 35 const addLog = useSetAtom(addLogEntryAtom); 36 const wsStatus = useAtomValue(websocketStatusAtom); 37 - const queryState = useAtomValue(queryStateFamily(queryId)); 38 const allDocuments = useAtomValue(documentsAtom); 39 40 const { enabled = true } = options; ··· 87 } 88 }); 89 }; 90 - }, [queryId, stringifiedEsQuery, enabled, ws, wsStatus, setActiveSubscriptions]); 91 92 93 const hydratedData = useMemo(() => { ··· 97 .filter(Boolean); 98 }, [queryState?.result, allDocuments]); 99 100 - const isLoading = wsStatus !== 'open' || queryState === null; 101 102 return { 103 data: hydratedData,
··· 6 queryStateFamily, 7 websocketAtom, 8 websocketStatusAtom, 9 + addLogEntryAtom, 10 + queryCacheAtom 11 } from './atoms'; 12 import type { EsavDocument, QueryDoc, SubscribeMessage, UnsubscribeMessage } from './types'; 13 import { atomWithStorage } from 'jotai/utils'; ··· 35 const ws = useAtomValue(websocketAtom); 36 const addLog = useSetAtom(addLogEntryAtom); 37 const wsStatus = useAtomValue(websocketStatusAtom); 38 + //const queryState = useAtomValue(queryStateFamily(queryId)); 39 + const liveQueryState = useAtomValue(queryStateFamily(queryId)); 40 + const [cache, setCache] = useAtom(queryCacheAtom); 41 + const cachedQueryState = cache[queryId]; 42 + const queryState = liveQueryState ?? cachedQueryState; 43 + useEffect(() => { 44 + // If we receive valid new data from the live query, update our cache. 45 + if (liveQueryState?.result) { 46 + setCache((prevCache) => { 47 + // Avoid unnecessary updates if the data is identical 48 + if (prevCache[queryId] === liveQueryState) { 49 + return prevCache; 50 + } 51 + return { 52 + ...prevCache, 53 + [queryId]: liveQueryState, 54 + }; 55 + }); 56 + } 57 + }, [liveQueryState, queryId, setCache]); 58 + 59 const allDocuments = useAtomValue(documentsAtom); 60 61 const { enabled = true } = options; ··· 108 } 109 }); 110 }; 111 + }, [queryId, stringifiedEsQuery, enabled, ws, wsStatus, setActiveSubscriptions, addLog]); 112 113 114 const hydratedData = useMemo(() => { ··· 118 .filter(Boolean); 119 }, [queryState?.result, allDocuments]); 120 121 + //const isLoading = wsStatus !== 'open' || queryState === null; 122 + const isLoading = !queryState; 123 124 return { 125 data: hydratedData,