mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 17:26:36 -04:00
Fix asyncReactive detection of loading
- Removed `success` store as it wouldn't work - We should check for a value in error instead
This commit is contained in:
parent
e23b1b77b7
commit
4cd60da7b8
2 changed files with 6 additions and 19 deletions
|
@ -1,10 +1,9 @@
|
||||||
import { Readable, readable, derived } from "svelte/store";
|
import { Readable, readable } from "svelte/store";
|
||||||
|
|
||||||
interface AsyncData<T, E> {
|
interface AsyncData<T, E> {
|
||||||
value: Readable<T | null>;
|
value: Readable<T | null>;
|
||||||
error: Readable<E | null>;
|
error: Readable<E | null>;
|
||||||
loading: Readable<boolean>;
|
loading: Readable<boolean>;
|
||||||
success: Readable<boolean>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function useAsync<T, E = unknown>(asyncFunction: () => Promise<T>): AsyncData<T, E> {
|
function useAsync<T, E = unknown>(asyncFunction: () => Promise<T>): AsyncData<T, E> {
|
||||||
|
@ -22,9 +21,7 @@ function useAsync<T, E = unknown>(asyncFunction: () => Promise<T>): AsyncData<T,
|
||||||
promise.finally(() => set(false));
|
promise.finally(() => set(false));
|
||||||
});
|
});
|
||||||
|
|
||||||
const success = derived([value], (_, set) => set(true), false);
|
return { value, error, loading };
|
||||||
|
|
||||||
return { value, error, loading, success };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useAsync;
|
export default useAsync;
|
||||||
|
|
|
@ -4,7 +4,6 @@ interface AsyncReativeData<T, E> {
|
||||||
value: Readable<T | null>;
|
value: Readable<T | null>;
|
||||||
error: Readable<E | null>;
|
error: Readable<E | null>;
|
||||||
loading: Readable<boolean>;
|
loading: Readable<boolean>;
|
||||||
success: Readable<boolean>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function useAsyncReactive<T, E>(
|
function useAsyncReactive<T, E>(
|
||||||
|
@ -36,24 +35,15 @@ function useAsyncReactive<T, E>(
|
||||||
);
|
);
|
||||||
|
|
||||||
const loading = derived(
|
const loading = derived(
|
||||||
[value, error],
|
promise,
|
||||||
(_, set: (value: boolean) => void) => {
|
($promise, set: (value: boolean) => void) => {
|
||||||
set(false);
|
$promise?.finally(() => set(false));
|
||||||
return () => set(true);
|
return () => set(true);
|
||||||
},
|
},
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
const success = derived(
|
return { value, error, loading };
|
||||||
[value],
|
|
||||||
(_, set: (value: boolean) => void) => {
|
|
||||||
set(true);
|
|
||||||
return () => set(false);
|
|
||||||
},
|
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
return { value, error, loading, success };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useAsyncReactive;
|
export default useAsyncReactive;
|
||||||
|
|
Loading…
Reference in a new issue