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

.once event emit repetitive execution bug #3902

Closed
LYlanfeng opened this issue Jun 8, 2021 · 0 comments · Fixed by #3904
Closed

.once event emit repetitive execution bug #3902

LYlanfeng opened this issue Jun 8, 2021 · 0 comments · Fixed by #3904
Labels
🐞 bug Something isn't working

Comments

@LYlanfeng
Copy link
Contributor

Version

3.1.1

Reproduction link

https://jsfiddle.net/liyu1988/7pjdktvb/11/

Steps to reproduce

  test('.once', () => {
    const Foo = defineComponent({
      render() {},
      emits: {
        foo: null,
        bar: null
      },
      created() {
        this.$emit('foo')
        this.$emit('foo')
        this.$emit('bar')
        this.$emit('bar')
      }
    })
    const fn = jest.fn()
    const fn2 = jest.fn()
    render(
      h(Foo, {
        onFooOnce: fn,
        onBarOnce: fn2
      }),
      nodeOps.createElement('div')
    )
    document.createElement('#app')
    expect(fn).toHaveBeenCalledTimes(1)
    expect(fn2).toHaveBeenCalledTimes(2) // Should be equal to,this is 2
  })

What is expected?

expect(fn2).toHaveBeenCalledTimes(1)

What is actually happening?

expect(fn2).toHaveBeenCalledTimes(2)


  const onceHandler = props[handlerName   `Once`]
  if (onceHandler) {
    if (!instance.emitted) {
      ;(instance.emitted = {} as Record<string, boolean>)[handlerName] = true
    } else if (instance.emitted[handlerName]) {
      return
    }
   // 初次执行once事件不会有问题,执行不同的once事件,则会重复执行,需要加上 instance.emitted[handlerName] = true
    callWithAsyncErrorHandling(
      onceHandler,
      instance,
      ErrorCodes.COMPONENT_EVENT_HANDLER,
      args
    )
  }

@posva posva added the 🐞 bug Something isn't working label Jun 8, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants