fix(watch): allow handler to be a string (#1775)

fix #1774
This commit is contained in:
Eduardo San Martin Morote 2020-08-04 18:42:47 +02:00 committed by GitHub
parent b0d01e9db9
commit b5f91ff570
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -112,6 +112,7 @@ describe('api: options', () => {
const spyA = jest.fn(returnThis) const spyA = jest.fn(returnThis)
const spyB = jest.fn(returnThis) const spyB = jest.fn(returnThis)
const spyC = jest.fn(returnThis) const spyC = jest.fn(returnThis)
const spyD = jest.fn(returnThis)
let ctx: any let ctx: any
const Comp = { const Comp = {
@ -121,7 +122,8 @@ describe('api: options', () => {
bar: 2, bar: 2,
baz: { baz: {
qux: 3 qux: 3
} },
qux: 4
} }
}, },
watch: { watch: {
@ -132,10 +134,14 @@ describe('api: options', () => {
baz: { baz: {
handler: spyC, handler: spyC,
deep: true deep: true
},
qux: {
handler: 'onQuxChange'
} }
}, },
methods: { methods: {
onFooChange: spyA onFooChange: spyA,
onQuxChange: spyD
}, },
render() { render() {
ctx = this ctx = this
@ -164,6 +170,11 @@ describe('api: options', () => {
expect(spyC).toHaveBeenCalledTimes(1) expect(spyC).toHaveBeenCalledTimes(1)
// new and old objects have same identity // new and old objects have same identity
assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }]) assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
ctx.qux++
await nextTick()
expect(spyD).toHaveBeenCalledTimes(1)
assertCall(spyD, 0, [5, 4])
}) })
test('watch array', async () => { test('watch array', async () => {
@ -707,7 +718,10 @@ describe('api: options', () => {
test('Expected a function as watch handler', () => { test('Expected a function as watch handler', () => {
const Comp = { const Comp = {
watch: { watch: {
foo: 'notExistingMethod' foo: 'notExistingMethod',
foo2: {
handler: 'notExistingMethod2'
}
}, },
render() {} render() {}
} }
@ -718,6 +732,9 @@ describe('api: options', () => {
expect( expect(
'Invalid watch handler specified by key "notExistingMethod"' 'Invalid watch handler specified by key "notExistingMethod"'
).toHaveBeenWarned() ).toHaveBeenWarned()
expect(
'Invalid watch handler specified by key "notExistingMethod2"'
).toHaveBeenWarned()
}) })
test('Invalid watch option', () => { test('Invalid watch option', () => {

View File

@ -266,7 +266,7 @@ export type ExtractComputedReturns<T extends any> = {
type WatchOptionItem = type WatchOptionItem =
| string | string
| WatchCallback | WatchCallback
| { handler: WatchCallback } & WatchOptions | { handler: WatchCallback | string } & WatchOptions
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[] type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
@ -704,7 +704,14 @@ function createWatcher(
if (isArray(raw)) { if (isArray(raw)) {
raw.forEach(r => createWatcher(r, ctx, publicThis, key)) raw.forEach(r => createWatcher(r, ctx, publicThis, key))
} else { } else {
watch(getter, raw.handler.bind(publicThis), raw) const handler = isFunction(raw.handler)
? raw.handler.bind(publicThis)
: (ctx[raw.handler] as WatchCallback)
if (isFunction(handler)) {
watch(getter, handler, raw)
} else if (__DEV__) {
warn(`Invalid watch handler specified by key "${raw.handler}"`, handler)
}
} }
} else if (__DEV__) { } else if (__DEV__) {
warn(`Invalid watch option: "${key}"`) warn(`Invalid watch option: "${key}"`)