2021-03-21 21:07:35 +01:00
|
|
|
import { Readable, readable, derived } from "svelte/store";
|
2021-03-21 20:16:40 +01:00
|
|
|
|
|
|
|
interface AsyncRefreshData<T, E> {
|
2021-03-21 21:07:35 +01:00
|
|
|
value: Readable<T | null>;
|
|
|
|
error: Readable<E | null>;
|
|
|
|
pending: Readable<boolean>;
|
|
|
|
success: Readable<boolean>;
|
|
|
|
loading: Readable<boolean>;
|
2021-03-21 20:16:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 21:07:35 +01:00
|
|
|
function useAsyncRefresh<T, E>(
|
|
|
|
asyncFunction: () => Promise<T>,
|
|
|
|
dependencies: [Readable<unknown>, ...Readable<unknown>[]]
|
|
|
|
): AsyncRefreshData<T, E> {
|
|
|
|
const initial = asyncFunction();
|
|
|
|
const promise = derived(dependencies, (_, set) => set(asyncFunction()), initial);
|
2021-03-21 20:16:40 +01:00
|
|
|
|
2021-03-21 21:07:35 +01:00
|
|
|
const value = derived(
|
|
|
|
promise,
|
|
|
|
($promise, set: (value: T | null) => void) => {
|
|
|
|
$promise.then((value: T) => set(value));
|
|
|
|
return () => set(null);
|
|
|
|
},
|
|
|
|
null
|
|
|
|
);
|
2021-03-21 20:16:40 +01:00
|
|
|
|
2021-03-21 21:07:35 +01:00
|
|
|
const error = derived(
|
|
|
|
promise,
|
|
|
|
($promise, set: (error: E | null) => void) => {
|
|
|
|
$promise.catch((error: E) => set(error));
|
|
|
|
return () => set(null);
|
|
|
|
},
|
|
|
|
null
|
|
|
|
);
|
2021-03-21 20:16:40 +01:00
|
|
|
|
2021-03-21 21:07:35 +01:00
|
|
|
const pending = readable(true, (set: (value: boolean) => void) => {
|
|
|
|
initial.finally(() => set(false));
|
|
|
|
});
|
2021-03-21 20:16:40 +01:00
|
|
|
|
2021-03-21 21:07:35 +01:00
|
|
|
const loading = derived(
|
|
|
|
[value, error],
|
|
|
|
(_, set) => {
|
|
|
|
set(false);
|
|
|
|
return () => set(true);
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
const success = derived(
|
|
|
|
[value],
|
|
|
|
(_, set) => {
|
|
|
|
set(true);
|
|
|
|
return () => set(false);
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
return { value, error, pending, loading, success };
|
2021-03-21 20:16:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 21:07:35 +01:00
|
|
|
export default useAsyncRefresh;
|