feat(watch): warn when using lazy with simple callback
This commit is contained in:
parent
c2c9c2b57e
commit
3deb20df63
@ -1,5 +1,5 @@
|
|||||||
import { watch, reactive, computed, nextTick, ref, h } from '../src/index'
|
import { watch, reactive, computed, nextTick, ref, h } from '../src/index'
|
||||||
import { render, nodeOps, serializeInner } from '@vue/runtime-test'
|
import { render, nodeOps, serializeInner, mockWarn } from '@vue/runtime-test'
|
||||||
import {
|
import {
|
||||||
ITERATE_KEY,
|
ITERATE_KEY,
|
||||||
DebuggerEvent,
|
DebuggerEvent,
|
||||||
@ -10,6 +10,8 @@ import {
|
|||||||
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
|
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
|
||||||
|
|
||||||
describe('api: watch', () => {
|
describe('api: watch', () => {
|
||||||
|
mockWarn()
|
||||||
|
|
||||||
it('basic usage', async () => {
|
it('basic usage', async () => {
|
||||||
const state = reactive({ count: 0 })
|
const state = reactive({ count: 0 })
|
||||||
let dummy
|
let dummy
|
||||||
@ -344,7 +346,7 @@ describe('api: watch', () => {
|
|||||||
expect(cb).toHaveBeenCalled()
|
expect(cb).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('ignore lazy', async () => {
|
it('ignore lazy option when using simple callback', async () => {
|
||||||
const count = ref(0)
|
const count = ref(0)
|
||||||
let dummy
|
let dummy
|
||||||
watch(
|
watch(
|
||||||
@ -354,6 +356,7 @@ describe('api: watch', () => {
|
|||||||
{ lazy: true }
|
{ lazy: true }
|
||||||
)
|
)
|
||||||
expect(dummy).toBeUndefined()
|
expect(dummy).toBeUndefined()
|
||||||
|
expect(`lazy option is only respected`).toHaveBeenWarned()
|
||||||
|
|
||||||
await nextTick()
|
await nextTick()
|
||||||
expect(dummy).toBe(0)
|
expect(dummy).toBe(0)
|
||||||
|
@ -29,6 +29,7 @@ import {
|
|||||||
} from './errorHandling'
|
} from './errorHandling'
|
||||||
import { onBeforeUnmount } from './apiLifecycle'
|
import { onBeforeUnmount } from './apiLifecycle'
|
||||||
import { queuePostRenderEffect } from './renderer'
|
import { queuePostRenderEffect } from './renderer'
|
||||||
|
import { warn } from './warning'
|
||||||
|
|
||||||
export type WatchHandler<T = any> = (
|
export type WatchHandler<T = any> = (
|
||||||
value: T,
|
value: T,
|
||||||
@ -197,14 +198,20 @@ function doWatch(
|
|||||||
scheduler: applyCb ? () => scheduler(applyCb) : scheduler
|
scheduler: applyCb ? () => scheduler(applyCb) : scheduler
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!lazy || !cb) {
|
if (lazy && cb) {
|
||||||
|
oldValue = runner()
|
||||||
|
} else {
|
||||||
|
if (__DEV__ && lazy && !cb) {
|
||||||
|
warn(
|
||||||
|
`watch() lazy option is only respected when using the ` +
|
||||||
|
`watch(getter, callback) signature.`
|
||||||
|
)
|
||||||
|
}
|
||||||
if (applyCb) {
|
if (applyCb) {
|
||||||
scheduler(applyCb)
|
scheduler(applyCb)
|
||||||
} else {
|
} else {
|
||||||
scheduler(runner)
|
scheduler(runner)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
oldValue = runner()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
recordEffect(runner)
|
recordEffect(runner)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user