Skip to content

fix: ensure tracking is paused when emit() calls handler so it can safely be called in effects (fix #6669) #6688

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 35 additions & 1 deletion packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
h,
nodeOps,
toHandlers,
nextTick
nextTick,
ref,
watchEffect
} from '@vue/runtime-test'
import { isEmitListener } from '../src/componentEmits'

Expand Down Expand Up @@ -431,4 +433,36 @@ describe('component: emit', () => {
await nextTick()
expect(fn).not.toHaveBeenCalled()
})

test('should not track during listener execution', async () => {
const counter = ref(0)
const Comp = defineComponent({
emits: ['interaction'],
setup(props, { emit }) {
const doEmit = ref(true)
watchEffect(() => {
if (doEmit.value) emit('interaction')
})
return () => h('div')
}
})
const el = nodeOps.createElement('div')
render(
h(Comp, {
onInteraction: async () => {
if (counter.value < 5) {
await nextTick()
counter.value++
}
}
}),
el
)

await nextTick()
await nextTick()
await nextTick()

expect(counter.value).toBe(1)
})
})
3 changes: 3 additions & 0 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
compatModelEventPrefix,
compatModelEmit
} from './compat/componentVModel'
import { pauseTracking, resetTracking } from '@vue/reactivity'

export type ObjectEmitsOptions = Record<
string,
Expand Down Expand Up @@ -161,12 +162,14 @@ export function emit(
}

if (handler) {
pauseTracking()
callWithAsyncErrorHandling(
handler,
instance,
ErrorCodes.COMPONENT_EVENT_HANDLER,
args
)
resetTracking()
Comment on lines +165 to +172
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Good fix: Pausing reactivity tracking during event handler execution.

This change prevents reactive dependencies from being tracked while executing event handlers called through emit(). This fixes issue #6669 where emitting events inside an effect could cause infinite loops when reactive state was accessed inside the event handlers.

However, the same fix should be applied to the onceHandler code block (lines 175-189) to maintain consistency.

  if (onceHandler) {
    if (!instance.emitted) {
      instance.emitted = {} as Record<any, boolean>
    } else if (instance.emitted[handlerName]) {
      return
    }
    instance.emitted[handlerName] = true
+   pauseTracking()
    callWithAsyncErrorHandling(
      onceHandler,
      instance,
      ErrorCodes.COMPONENT_EVENT_HANDLER,
      args
    )
+   resetTracking()
  }
🤖 Prompt for AI Agents
In packages/runtime-core/src/componentEmits.ts between lines 175 and 189, apply
the same pauseTracking and resetTracking calls around the onceHandler invocation
as done for the regular handler at lines 165-172. This ensures reactive
dependencies are not tracked during the execution of onceHandler event
callbacks, preventing infinite loops and maintaining consistency in event
handling.

}

const onceHandler = props[handlerName + `Once`]
Expand Down
Loading