Skip to content
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
20 changes: 20 additions & 0 deletions packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ describe('api: template refs', () => {
expect(fn2.mock.calls[0][0]).toBe(root.children[0])
})

it('function ref should not track dependencies read by callback', async () => {
const root = nodeOps.createElement('div')
const visible = ref(new Set<number>())
const fn = vi.fn((el: any) => {
if (!el || visible.value.has(0)) {
return
}
visible.value = new Set([0])
})

const Comp = defineComponent(() => () => h('div', { ref: fn }))

render(h(Comp), root)
expect(fn).toHaveBeenCalledTimes(1)
expect(visible.value.has(0)).toBe(true)

await nextTick()
expect(fn).toHaveBeenCalledTimes(1)
})

it('function ref unmount', async () => {
const root = nodeOps.createElement('div')
const fn = vi.fn()
Expand Down
9 changes: 7 additions & 2 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@vue/shared'
import { isAsyncWrapper } from './apiAsyncComponent'
import { warn } from './warning'
import { isRef, toRaw } from '@vue/reactivity'
import { isRef, pauseTracking, resetTracking, toRaw } from '@vue/reactivity'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import { type SchedulerJob, SchedulerJobFlags } from './scheduler'
import { queuePostRenderEffect } from './renderer'
Expand Down Expand Up @@ -136,7 +136,12 @@ export function setRef(
}

if (isFunction(ref)) {
callWithErrorHandling(ref, owner, ErrorCodes.FUNCTION_REF, [value, refs])
pauseTracking()
try {
callWithErrorHandling(ref, owner, ErrorCodes.FUNCTION_REF, [value, refs])
} finally {
resetTracking()
}
} else {
const _isString = isString(ref)
const _isRef = isRef(ref)
Expand Down
Loading