A bit better handling of responses

This commit is contained in:
Gregory Schier
2024-02-02 13:32:06 -08:00
parent 67aa7b7268
commit e53693f605
7 changed files with 168 additions and 120 deletions

View File

@@ -37,19 +37,21 @@ export function useKeyValue<T extends Object | null>({
});
const set = useCallback(
(value: ((v: T) => T) | T) => {
async (value: ((v: T) => T) | T) => {
if (typeof value === 'function') {
getKeyValue({ namespace, key, fallback: defaultValue }).then((kv) => {
mutate.mutate(value(kv));
await getKeyValue({ namespace, key, fallback: defaultValue }).then((kv) => {
const newV = value(kv);
if (newV === kv) return;
return mutate.mutateAsync(newV);
});
} else {
mutate.mutate(value);
} else if (value !== query.data) {
await mutate.mutateAsync(value);
}
},
[defaultValue, key, mutate, namespace],
[defaultValue, key, mutate, namespace, query.data],
);
const reset = useCallback(() => mutate.mutate(defaultValue), [mutate, defaultValue]);
const reset = useCallback(async () => mutate.mutateAsync(defaultValue), [mutate, defaultValue]);
return useMemo(
() => ({