Created
August 21, 2020 20:08
-
-
Save jacekkarczmarczyk/8bd17cb6fb6a19a5b6134b6c69c042e3 to your computer and use it in GitHub Desktop.
Async computed composable for Vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO debounce/throttle support | |
import { computed, ComputedRef, Ref, ref, watch } from '@vue/composition-api'; | |
export function useAsyncComputed<R> (load: () => Promise<R>): { | |
error: ComputedRef<unknown>; | |
loading: ComputedRef<boolean>; | |
value: ComputedRef<R | undefined>; | |
forceRefresh: () => void; | |
}; | |
export function useAsyncComputed<R> (load: () => Promise<R>, defaultValue: R): { | |
error: ComputedRef<unknown>; | |
loading: ComputedRef<boolean>; | |
value: ComputedRef<R>; | |
forceRefresh: () => void; | |
}; | |
export function useAsyncComputed<R> (load: () => Promise<R>, defaultValue?: R) { | |
const requestId = ref(0); | |
const asyncValue = ref(defaultValue) as Ref<R | undefined>; | |
const loading = ref(false); | |
const error = ref<unknown>(); | |
const effect = async (response: Promise<R>) => { | |
const currentRequestId = requestId.value; | |
try { | |
error.value = undefined; | |
loading.value = true; | |
const result = await response; | |
if (currentRequestId === requestId.value) { | |
asyncValue.value = result; | |
} | |
} catch (error) { | |
if (process.env.NODE_ENV === 'development') { | |
console.error(error); | |
} | |
if (currentRequestId === requestId.value) { | |
asyncValue.value = defaultValue; | |
error.value = error; | |
} | |
} finally { | |
if (currentRequestId === requestId.value) { | |
loading.value = false; | |
} | |
} | |
}; | |
watch(() => requestId.value ? load() : null, promise => promise && effect(promise)); | |
return { | |
error: computed(() => error.value), | |
loading: computed(() => loading.value), | |
value: computed(() => (requestId.value || ++requestId.value) && asyncValue.value), | |
forceRefresh: () => { | |
++requestId.value; | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Normal sync
computed
watch
+ref
useAsyncComputed