Skip to content

Commit

Permalink
fix(runtime-core): fix multiple .once event handlers on same component (
Browse files Browse the repository at this point in the history
  • Loading branch information
LYlanfeng committed Jun 8, 2021
1 parent 2b52d5d commit 011dee8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Expand Up @@ -245,21 +245,27 @@ describe('component: emit', () => {
const Foo = defineComponent({
render() {},
emits: {
foo: null
foo: null,
bar: null
},
created() {
this.$emit('foo')
this.$emit('foo')
this.$emit('bar')
this.$emit('bar')
}
})
const fn = jest.fn()
const barFn = jest.fn()
render(
h(Foo, {
onFooOnce: fn
onFooOnce: fn,
onBarOnce: barFn
}),
nodeOps.createElement('div')
)
expect(fn).toHaveBeenCalledTimes(1)
expect(barFn).toHaveBeenCalledTimes(1)
})

test('.once with normal listener of the same name', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/componentEmits.ts
Expand Up @@ -149,10 +149,11 @@ export function emit(
const onceHandler = props[handlerName + `Once`]
if (onceHandler) {
if (!instance.emitted) {
;(instance.emitted = {} as Record<string, boolean>)[handlerName] = true
instance.emitted = {} as Record<any, boolean>
} else if (instance.emitted[handlerName]) {
return
}
instance.emitted[handlerName] = true
callWithAsyncErrorHandling(
onceHandler,
instance,
Expand Down

0 comments on commit 011dee8

Please sign in to comment.