Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/once event repeat exe #3904

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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