fix(runtime-core): fix multiple .once event handlers on same component (#3904)
fix #3902
This commit is contained in:
parent
2b52d5d7c5
commit
011dee8644
@ -245,21 +245,27 @@ describe('component: emit', () => {
|
|||||||
const Foo = defineComponent({
|
const Foo = defineComponent({
|
||||||
render() {},
|
render() {},
|
||||||
emits: {
|
emits: {
|
||||||
foo: null
|
foo: null,
|
||||||
|
bar: null
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
this.$emit('foo')
|
this.$emit('foo')
|
||||||
this.$emit('foo')
|
this.$emit('foo')
|
||||||
|
this.$emit('bar')
|
||||||
|
this.$emit('bar')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const fn = jest.fn()
|
const fn = jest.fn()
|
||||||
|
const barFn = jest.fn()
|
||||||
render(
|
render(
|
||||||
h(Foo, {
|
h(Foo, {
|
||||||
onFooOnce: fn
|
onFooOnce: fn,
|
||||||
|
onBarOnce: barFn
|
||||||
}),
|
}),
|
||||||
nodeOps.createElement('div')
|
nodeOps.createElement('div')
|
||||||
)
|
)
|
||||||
expect(fn).toHaveBeenCalledTimes(1)
|
expect(fn).toHaveBeenCalledTimes(1)
|
||||||
|
expect(barFn).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('.once with normal listener of the same name', () => {
|
test('.once with normal listener of the same name', () => {
|
||||||
|
@ -149,10 +149,11 @@ export function emit(
|
|||||||
const onceHandler = props[handlerName + `Once`]
|
const onceHandler = props[handlerName + `Once`]
|
||||||
if (onceHandler) {
|
if (onceHandler) {
|
||||||
if (!instance.emitted) {
|
if (!instance.emitted) {
|
||||||
;(instance.emitted = {} as Record<string, boolean>)[handlerName] = true
|
instance.emitted = {} as Record<any, boolean>
|
||||||
} else if (instance.emitted[handlerName]) {
|
} else if (instance.emitted[handlerName]) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
instance.emitted[handlerName] = true
|
||||||
callWithAsyncErrorHandling(
|
callWithAsyncErrorHandling(
|
||||||
onceHandler,
|
onceHandler,
|
||||||
instance,
|
instance,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user