mirror of
https://github.com/ankitects/anki.git
synced 2025-11-12 07:37:11 -05:00
Avoid duplicate initial fetching with asyncReactive
This commit is contained in:
parent
15b19e44b9
commit
3c5233297a
1 changed files with 5 additions and 4 deletions
|
|
@ -13,14 +13,15 @@ function useAsyncReactive<T, E>(
|
||||||
): AsyncReativeData<T, E> {
|
): AsyncReativeData<T, E> {
|
||||||
const promise = derived(
|
const promise = derived(
|
||||||
dependencies,
|
dependencies,
|
||||||
(_, set) => set(asyncFunction()),
|
(_, set: (value: Promise<T> | null) => void) => set(asyncFunction()),
|
||||||
asyncFunction()
|
// initialize with null to avoid duplicate fetch on init
|
||||||
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
const value = derived(
|
const value = derived(
|
||||||
promise,
|
promise,
|
||||||
($promise, set: (value: T) => void) => {
|
($promise, set: (value: T) => void) => {
|
||||||
$promise.then((value: T) => set(value));
|
$promise?.then((value: T) => set(value));
|
||||||
},
|
},
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
@ -28,7 +29,7 @@ function useAsyncReactive<T, E>(
|
||||||
const error = derived(
|
const error = derived(
|
||||||
promise,
|
promise,
|
||||||
($promise, set: (error: E | null) => void) => {
|
($promise, set: (error: E | null) => void) => {
|
||||||
$promise.catch((error: E) => set(error));
|
$promise?.catch((error: E) => set(error));
|
||||||
return () => set(null);
|
return () => set(null);
|
||||||
},
|
},
|
||||||
null
|
null
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue