Skip to content

refactor(runtime-vapor): allow non-instance for setRef #13675

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: minor
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
4 changes: 2 additions & 2 deletions packages/runtime-vapor/__tests__/apiExpose.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('api: expose', () => {
define({
setup: () => {
const n0 = (i = createComponent(Child))
setRef(currentInstance as VaporComponentInstance, n0, childRef)
setRef(n0, childRef)
return n0
},
}).render()
Expand All @@ -46,7 +46,7 @@ describe('api: expose', () => {
define({
setup: () => {
const n0 = createComponent(Child)
setRef(currentInstance as VaporComponentInstance, n0, childRef)
setRef(n0, childRef)
return n0
},
}).render()
Expand Down
19 changes: 12 additions & 7 deletions packages/runtime-vapor/src/apiTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,32 @@ export type setRefFn = (

export function createTemplateRefSetter(): setRefFn {
const instance = currentInstance as VaporComponentInstance
return (...args) => setRef(instance, ...args)
return (el, ref, oldRef, refFor) => setRef(el, ref, oldRef, refFor, instance)
}

/**
* Function for handling a template ref
*/
export function setRef(
instance: VaporComponentInstance,
el: RefEl,
ref: NodeRef,
oldRef?: NodeRef,
refFor = false,
instance?: VaporComponentInstance,
): NodeRef | undefined {
if (!instance || instance.isUnmounted) return
const _isString = isString(ref)
if (_isString && (!instance || instance.isUnmounted)) return

const setupState: any = __DEV__ ? instance.setupState || {} : null
const setupState: any = __DEV__
? (instance && instance.setupState) || {}
: null
const refValue = getRefValue(el)

const refs =
instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs
const refs = instance
? instance.refs === EMPTY_OBJ
? (instance.refs = {})
: instance.refs
: {}

// dynamic ref changed. unset old ref
if (oldRef != null && oldRef !== ref) {
Expand All @@ -79,7 +85,6 @@ export function setRef(
// TODO this gets called repeatedly in renderEffect when it's dynamic ref?
onScopeDispose(() => invokeRefSetter())
} else {
const _isString = isString(ref)
const _isRef = isRef(ref)
let existing: unknown

Expand Down