mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 17:18:32 +02:00
Optimistically-update key values
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister';
|
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister';
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
|
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
|
||||||
import { persistQueryClient } from '@tanstack/react-query-persist-client';
|
import { persistQueryClient } from '@tanstack/react-query-persist-client';
|
||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import { listen } from '@tauri-apps/api/event';
|
import { listen } from '@tauri-apps/api/event';
|
||||||
@@ -172,7 +173,7 @@ export function App() {
|
|||||||
<HelmetProvider>
|
<HelmetProvider>
|
||||||
<DndProvider backend={HTML5Backend}>
|
<DndProvider backend={HTML5Backend}>
|
||||||
<AppRouter />
|
<AppRouter />
|
||||||
{/*<ReactQueryDevtools initialIsOpen={false} />*/}
|
<ReactQueryDevtools initialIsOpen={false} />
|
||||||
</DndProvider>
|
</DndProvider>
|
||||||
</HelmetProvider>
|
</HelmetProvider>
|
||||||
</MotionConfig>
|
</MotionConfig>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { buildKeyValueKey, getKeyValue, setKeyValue } from '../lib/keyValueStore';
|
import { buildKeyValueKey, getKeyValue, setKeyValue } from '../lib/keyValueStore';
|
||||||
|
|
||||||
@@ -24,6 +24,7 @@ export function useKeyValue<T extends Object>({
|
|||||||
key: string | string[];
|
key: string | string[];
|
||||||
defaultValue: T;
|
defaultValue: T;
|
||||||
}) {
|
}) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
const query = useQuery<T>({
|
const query = useQuery<T>({
|
||||||
queryKey: keyValueQueryKey({ namespace, key }),
|
queryKey: keyValueQueryKey({ namespace, key }),
|
||||||
queryFn: async () => getKeyValue({ namespace, key, fallback: defaultValue }),
|
queryFn: async () => getKeyValue({ namespace, key, fallback: defaultValue }),
|
||||||
@@ -31,6 +32,10 @@ export function useKeyValue<T extends Object>({
|
|||||||
|
|
||||||
const mutate = useMutation<T, unknown, T>({
|
const mutate = useMutation<T, unknown, T>({
|
||||||
mutationFn: (value) => setKeyValue<T>({ namespace, key, value }),
|
mutationFn: (value) => setKeyValue<T>({ namespace, key, value }),
|
||||||
|
onMutate: (value) => {
|
||||||
|
// k/v should be as fast as possible, so optimistically update the cache
|
||||||
|
queryClient.setQueryData(keyValueQueryKey({ namespace, key }), value);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const set = useCallback(
|
const set = useCallback(
|
||||||
|
|||||||
Reference in New Issue
Block a user