]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat: watchPostEffect
authorEvan You <yyx990803@gmail.com>
Sat, 10 Jul 2021 01:47:15 +0000 (21:47 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 16 Jul 2021 18:30:49 +0000 (14:30 -0400)
packages/runtime-core/__tests__/apiWatch.spec.ts
packages/runtime-core/src/apiWatch.ts

index 9611b761f03befe84ce84891e60ba14ff65d1ceb..87644742cca320c3c38d5b1b23d326c2e2869110 100644 (file)
@@ -27,6 +27,7 @@ import {
   shallowRef,
   Ref
 } from '@vue/reactivity'
+import { watchPostEffect } from '../src/apiWatch'
 
 // reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
 
@@ -363,6 +364,32 @@ describe('api: watch', () => {
     expect(result).toBe(true)
   })
 
+  it('watchPostEffect', async () => {
+    const count = ref(0)
+    let result
+    const assertion = jest.fn(count => {
+      result = serializeInner(root) === `${count}`
+    })
+
+    const Comp = {
+      setup() {
+        watchPostEffect(() => {
+          assertion(count.value)
+        })
+        return () => count.value
+      }
+    }
+    const root = nodeOps.createElement('div')
+    render(h(Comp), root)
+    expect(assertion).toHaveBeenCalledTimes(1)
+    expect(result).toBe(true)
+
+    count.value++
+    await nextTick()
+    expect(assertion).toHaveBeenCalledTimes(2)
+    expect(result).toBe(true)
+  })
+
   it('flush timing: sync', async () => {
     const count = ref(0)
     const count2 = ref(0)
index eb3a4503cebc9cedc9aa799b23c38e1857c31150..2c2d8964754912d8551433121ea73c22604afbff 100644 (file)
@@ -3,10 +3,10 @@ import {
   Ref,
   ComputedRef,
   ReactiveEffect,
-  ReactiveEffectOptions,
   isReactive,
   ReactiveFlags,
-  EffectScheduler
+  EffectScheduler,
+  DebuggerOptions
 } from '@vue/reactivity'
 import { SchedulerJob, queuePreFlushCb } from './scheduler'
 import {
@@ -58,10 +58,8 @@ type MapSources<T, Immediate> = {
 
 type InvalidateCbRegistrator = (cb: () => void) => void
 
-export interface WatchOptionsBase {
+export interface WatchOptionsBase extends DebuggerOptions {
   flush?: 'pre' | 'post' | 'sync'
-  onTrack?: ReactiveEffectOptions['onTrack']
-  onTrigger?: ReactiveEffectOptions['onTrigger']
 }
 
 export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
@@ -79,6 +77,15 @@ export function watchEffect(
   return doWatch(effect, null, options)
 }
 
+export function watchPostEffect(
+  effect: WatchEffect,
+  options?: DebuggerOptions
+) {
+  return doWatch(effect, null, (__DEV__
+    ? Object.assign(options || {}, { flush: 'post' })
+    : { flush: 'post' }) as WatchOptionsBase)
+}
+
 // initial value for watchers to trigger on undefined initial values
 const INITIAL_WATCHER_VALUE = {}