mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-21 00:49:17 +01:00
A bit better handling of responses
This commit is contained in:
@@ -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(
|
||||
() => ({
|
||||
|
||||
Reference in New Issue
Block a user