]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): fix component .once listener logic
authorEvan You <yyx990803@gmail.com>
Tue, 20 Oct 2020 13:49:53 +0000 (09:49 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 20 Oct 2020 13:50:18 +0000 (09:50 -0400)
packages/runtime-core/__tests__/componentEmits.spec.ts
packages/runtime-core/src/componentEmits.ts

index 317b36f8ccbdee99572038b39a2c1bc20bc074b5..f93d547ab19aea3a9e3fe8377694949595d7ff87 100644 (file)
@@ -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)
index 7f1bd1813fb6aeb1c34e93882229f917eea0237e..726a16b87435ec0f2eac852834ac7ac273fac253 100644 (file)
@@ -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