Skip to content

Commit

Permalink
fix(runtime-core): fix component .once listener logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Oct 20, 2020
1 parent 6d2a1cb commit 4bbb2b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
24 changes: 24 additions & 0 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,30 @@ describe('component: emit', () => {
expect(fn).toHaveBeenCalledTimes(1)
})

test('.once with normal listener of the same name', () => {
const Foo = defineComponent({
render() {},
emits: {
foo: null
},
created() {
this.$emit('foo')
this.$emit('foo')
}
})
const onFoo = jest.fn()
const onFooOnce = jest.fn()
render(
h(Foo, {
onFoo,
onFooOnce
}),
nodeOps.createElement('div')
)
expect(onFoo).toHaveBeenCalledTimes(2)
expect(onFooOnce).toHaveBeenCalledTimes(1)
})

test('isEmitListener', () => {
const options = { click: null }
expect(isEmitListener(options, 'onClick')).toBe(true)
Expand Down
18 changes: 13 additions & 5 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,25 @@ export function emit(
handlerName = toHandlerKey(hyphenate(event))
handler = props[handlerName]
}
if (!handler) {
handler = props[handlerName + `Once`]

if (handler) {
callWithAsyncErrorHandling(
handler,
instance,
ErrorCodes.COMPONENT_EVENT_HANDLER,
args
)
}

const onceHandler = props[handlerName + `Once`]
if (onceHandler) {
if (!instance.emitted) {
;(instance.emitted = {} as Record<string, boolean>)[handlerName] = true
} else if (instance.emitted[handlerName]) {
return
}
}
if (handler) {
callWithAsyncErrorHandling(
handler,
onceHandler,
instance,
ErrorCodes.COMPONENT_EVENT_HANDLER,
args
Expand Down

0 comments on commit 4bbb2b2

Please sign in to comment.