Skip to content

Commit 42ace95

Browse files
committed
feat: watchPostEffect
1 parent 3b64508 commit 42ace95

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
shallowRef,
2828
Ref
2929
} from '@vue/reactivity'
30+
import { watchPostEffect } from '../src/apiWatch'
3031

3132
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
3233

@@ -363,6 +364,32 @@ describe('api: watch', () => {
363364
expect(result).toBe(true)
364365
})
365366

367+
it('watchPostEffect', async () => {
368+
const count = ref(0)
369+
let result
370+
const assertion = jest.fn(count => {
371+
result = serializeInner(root) === `${count}`
372+
})
373+
374+
const Comp = {
375+
setup() {
376+
watchPostEffect(() => {
377+
assertion(count.value)
378+
})
379+
return () => count.value
380+
}
381+
}
382+
const root = nodeOps.createElement('div')
383+
render(h(Comp), root)
384+
expect(assertion).toHaveBeenCalledTimes(1)
385+
expect(result).toBe(true)
386+
387+
count.value++
388+
await nextTick()
389+
expect(assertion).toHaveBeenCalledTimes(2)
390+
expect(result).toBe(true)
391+
})
392+
366393
it('flush timing: sync', async () => {
367394
const count = ref(0)
368395
const count2 = ref(0)

packages/runtime-core/src/apiWatch.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
Ref,
44
ComputedRef,
55
ReactiveEffect,
6-
ReactiveEffectOptions,
76
isReactive,
87
ReactiveFlags,
9-
EffectScheduler
8+
EffectScheduler,
9+
DebuggerOptions
1010
} from '@vue/reactivity'
1111
import { SchedulerJob, queuePreFlushCb } from './scheduler'
1212
import {
@@ -58,10 +58,8 @@ type MapSources<T, Immediate> = {
5858

5959
type InvalidateCbRegistrator = (cb: () => void) => void
6060

61-
export interface WatchOptionsBase {
61+
export interface WatchOptionsBase extends DebuggerOptions {
6262
flush?: 'pre' | 'post' | 'sync'
63-
onTrack?: ReactiveEffectOptions['onTrack']
64-
onTrigger?: ReactiveEffectOptions['onTrigger']
6563
}
6664

6765
export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
@@ -79,6 +77,15 @@ export function watchEffect(
7977
return doWatch(effect, null, options)
8078
}
8179

80+
export function watchPostEffect(
81+
effect: WatchEffect,
82+
options?: DebuggerOptions
83+
) {
84+
return doWatch(effect, null, (__DEV__
85+
? Object.assign(options || {}, { flush: 'post' })
86+
: { flush: 'post' }) as WatchOptionsBase)
87+
}
88+
8289
// initial value for watchers to trigger on undefined initial values
8390
const INITIAL_WATCHER_VALUE = {}
8491

0 commit comments

Comments
 (0)