Skip to content

Commit 011dee8

Browse files
authoredJun 8, 2021
fix(runtime-core): fix multiple .once event handlers on same component (#3904)
fix #3902
1 parent 2b52d5d commit 011dee8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed
 

‎packages/runtime-core/__tests__/componentEmits.spec.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -245,21 +245,27 @@ describe('component: emit', () => {
245245
const Foo = defineComponent({
246246
render() {},
247247
emits: {
248-
foo: null
248+
foo: null,
249+
bar: null
249250
},
250251
created() {
251252
this.$emit('foo')
252253
this.$emit('foo')
254+
this.$emit('bar')
255+
this.$emit('bar')
253256
}
254257
})
255258
const fn = jest.fn()
259+
const barFn = jest.fn()
256260
render(
257261
h(Foo, {
258-
onFooOnce: fn
262+
onFooOnce: fn,
263+
onBarOnce: barFn
259264
}),
260265
nodeOps.createElement('div')
261266
)
262267
expect(fn).toHaveBeenCalledTimes(1)
268+
expect(barFn).toHaveBeenCalledTimes(1)
263269
})
264270

265271
test('.once with normal listener of the same name', () => {

‎packages/runtime-core/src/componentEmits.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ export function emit(
149149
const onceHandler = props[handlerName + `Once`]
150150
if (onceHandler) {
151151
if (!instance.emitted) {
152-
;(instance.emitted = {} as Record<string, boolean>)[handlerName] = true
152+
instance.emitted = {} as Record<any, boolean>
153153
} else if (instance.emitted[handlerName]) {
154154
return
155155
}
156+
instance.emitted[handlerName] = true
156157
callWithAsyncErrorHandling(
157158
onceHandler,
158159
instance,

0 commit comments

Comments
 (0)
Please sign in to comment.