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:
Henrik Giesel 2021-03-22 15:23:48 +01:00
parent e23b1b77b7
commit 4cd60da7b8
2 changed files with 6 additions and 19 deletions

View File

@ -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;

View File

@ -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;