Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(computedAsync): add option to control watcher's flush timing #4746

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

Merged
merged 2 commits into from
May 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions 35 packages/core/computedAsync/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type { Fn } from '@vueuse/shared'
import type { Ref } from 'vue'
import { noop } from '@vueuse/shared'
import { computed, ref as deepRef, isRef, shallowRef, watchEffect } from 'vue'
import {
computed,
ref as deepRef,
isRef,
shallowRef,
watchEffect,
watchPostEffect,
watchSyncEffect,
} from 'vue'

/**
* Handle overlapping async evaluations.
Expand Down Expand Up @@ -30,6 +38,16 @@ export interface AsyncComputedOptions {
*/
shallow?: boolean

/**
* The flush option allows for greater control over the timing of a history point, default to `pre`
*
* Possible values: `pre`, `post`, `sync`
*
* It works in the same way as the flush option in watch and watch effect in vue reactivity
* @default 'pre'
*/
flush?: 'pre' | 'post' | 'sync'

/**
* Callback when error is caught.
*/
Expand Down Expand Up @@ -72,6 +90,7 @@ export function computedAsync<T>(

const {
lazy = false,
flush = 'pre',
evaluating = undefined,
shallow = true,
onError = noop,
Expand All @@ -81,7 +100,7 @@ export function computedAsync<T>(
const current = (shallow ? shallowRef(initialState) : deepRef(initialState)) as Ref<T>
let counter = 0

watchEffect(async (onInvalidate) => {
const cb = async (onInvalidate: AsyncComputedOnCancel) => {
if (!started.value)
return

Expand Down Expand Up @@ -120,7 +139,17 @@ export function computedAsync<T>(

hasFinished = true
}
})
}

if (flush === 'sync') {
watchSyncEffect(cb)
}
else if (flush === 'post') {
watchPostEffect(cb)
}
else {
watchEffect(cb)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not as options?

watchEffect(..., { flush })


if (lazy) {
return computed(() => {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.