New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asyncComputed is not lazy like computed #248
Comments
I do not know if this can be fixed if the current API is maintained. I managed to get an asyncComputed version that is lazy like computed but it doesn't provide the onCancel callback: export function asyncComputed<T>(
evaluationCallback: () => T | Promise<T>,
initialState?: T,
evaluatingRef?: Ref<boolean>,
): Ref<T> {
return customRef<T>((track: Fn, trigger: Fn) => {
let counter = 0
let current = initialState
const tracker = computed(() => {
counter++
const counterAtBeginning = counter
// Defer initial setting of `evaluating` ref
// to avoid having it as a dependency
if (evaluatingRef) {
Promise.resolve().then(() => {
evaluatingRef.value = true
})
}
Promise.resolve(evaluationCallback()).then((result) => {
if (counterAtBeginning === counter)
current = result
}).finally(() => {
if (evaluatingRef)
evaluatingRef.value = false
trigger()
})
return counter
})
return {
get() {
track()
// eslint-disable-next-line no-unused-expressions
tracker.value
return current as T
},
set() {},
}
})
} There was an issue about this in vuejs/rfcs#163. If we would have a Just an idea, but one possible option is to offer the above version as |
The behaviour has already described as a caveat in the doc. I am not sure if it makes sense to have
With vuejs/rfcs#212 (likely in Vue 3.1), it will expose |
Nice! I didn't know that effect was going to be exposed 🙌🏼 |
Sorry, somehow missed caveat section in the docs. |
@Demivan Sure, let's have the |
Sure, I'll work on it. |
The current implementation of the Do you think it is feasible to solve this issue with the current vue features, @antfu, maybe using @patak-dev 's idea above? It seems |
Not sure if this should be a default but, I think, there should be option to make
asyncComputed
lazy like vuecomputed
. Right now even if you usev-if
to not render computed data it is still being retrieved.#247 - Example of failing test
The text was updated successfully, but these errors were encountered: