import { isRef, toRaw } from '@vue/reactivity'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import { queuePostRenderEffect } from './renderer'
-import { type ComponentOptions, getComponentPublicInstance } from './component'
+import {
+ type ComponentOptions,
+ type Data,
+ getComponentPublicInstance,
+} from './component'
import { knownTemplateRefs } from './helpers/useTemplateRef'
/**
const oldRef = oldRawRef && (oldRawRef as VNodeNormalizedRefAtom).r
const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
const setupState = owner.setupState
- const rawSetupState = toRaw(setupState)
- const canSetSetupRef =
- setupState === EMPTY_OBJ
- ? () => false
- : (key: string) => {
- if (__DEV__) {
- if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
- warn(
- `Template ref "${key}" used on a non-ref value. ` +
- `It will not work in the production build.`,
- )
- }
-
- if (knownTemplateRefs.has(rawSetupState[key] as any)) {
- return false
- }
- }
- return hasOwn(rawSetupState, key)
- }
+ const canSetSetupRef = validateTemplateRef(setupState)
// dynamic ref changed. unset old ref
if (oldRef != null && oldRef !== ref) {
}
}
}
+
+export function validateTemplateRef(
+ setupState: Data,
+): (key: string) => boolean {
+ const rawSetupState = toRaw(setupState)
+ return setupState === EMPTY_OBJ
+ ? () => false
+ : (key: string) => {
+ if (__DEV__) {
+ if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
+ warn(
+ `Template ref "${key}" used on a non-ref value. ` +
+ `It will not work in the production build.`,
+ )
+ }
+
+ if (knownTemplateRefs.has(rawSetupState[key] as any)) {
+ return false
+ }
+ }
+ return hasOwn(rawSetupState, key)
+ }
+}
type SchedulerJob,
callWithErrorHandling,
queuePostFlushCb,
+ validateTemplateRef,
warn,
} from '@vue/runtime-dom'
import {
const refs =
instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs
+ const canSetSetupRef = validateTemplateRef(setupState)
// dynamic ref changed. unset old ref
if (oldRef != null && oldRef !== ref) {
if (isString(oldRef)) {
const doSet: SchedulerJob = () => {
if (refFor) {
existing = _isString
- ? __DEV__ && hasOwn(setupState, ref)
+ ? __DEV__ && canSetSetupRef(ref)
? setupState[ref]
: refs[ref]
: ref.value
existing = [refValue]
if (_isString) {
refs[ref] = existing
- if (__DEV__ && hasOwn(setupState, ref)) {
+ if (__DEV__ && canSetSetupRef(ref)) {
setupState[ref] = refs[ref]
// if setupState[ref] is a reactivity ref,
// the existing will also become reactivity too
}
} else if (_isString) {
refs[ref] = refValue
- if (__DEV__ && hasOwn(setupState, ref)) {
+ if (__DEV__ && canSetSetupRef(ref)) {
setupState[ref] = refValue
}
} else if (_isRef) {
remove(existing, refValue)
} else if (_isString) {
refs[ref] = null
- if (__DEV__ && hasOwn(setupState, ref)) {
+ if (__DEV__ && canSetSetupRef(ref)) {
setupState[ref] = null
}
} else if (_isRef) {