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
@ -1,10 +1,9 @@
|
||||
import { Readable, readable, derived } from "svelte/store";
|
||||
import { Readable, readable } from "svelte/store";
|
||||
|
||||
interface AsyncData<T, E> {
|
||||
value: Readable<T | null>;
|
||||
error: Readable<E | null>;
|
||||
loading: Readable<boolean>;
|
||||
success: Readable<boolean>;
|
||||
}
|
||||
|
||||
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));
|
||||
});
|
||||
|
||||
const success = derived([value], (_, set) => set(true), false);
|
||||
|
||||
return { value, error, loading, success };
|
||||
return { value, error, loading };
|
||||
}
|
||||
|
||||
export default useAsync;
|
||||
|
@ -4,7 +4,6 @@ interface AsyncReativeData<T, E> {
|
||||
value: Readable<T | null>;
|
||||
error: Readable<E | null>;
|
||||
loading: Readable<boolean>;
|
||||
success: Readable<boolean>;
|
||||
}
|
||||
|
||||
function useAsyncReactive<T, E>(
|
||||
@ -36,24 +35,15 @@ function useAsyncReactive<T, E>(
|
||||
);
|
||||
|
||||
const loading = derived(
|
||||
[value, error],
|
||||
(_, set: (value: boolean) => void) => {
|
||||
set(false);
|
||||
promise,
|
||||
($promise, set: (value: boolean) => void) => {
|
||||
$promise?.finally(() => set(false));
|
||||
return () => set(true);
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
const success = derived(
|
||||
[value],
|
||||
(_, set: (value: boolean) => void) => {
|
||||
set(true);
|
||||
return () => set(false);
|
||||
},
|
||||
false
|
||||
);
|
||||
|
||||
return { value, error, loading, success };
|
||||
return { value, error, loading };
|
||||
}
|
||||
|
||||
export default useAsyncReactive;
|
||||
|
Loading…
Reference in New Issue
Block a user