Skip to content

Commit

Permalink
refactor: get metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Feb 20, 2024
1 parent d58c266 commit fe4744e
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions packages/runtime-vapor/src/dom/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,33 @@ import {
normalizeStyle,
toDisplayString,
} from '@vue/shared'
import { currentInstance } from '../component'
import { type ElementMetadata, currentInstance } from '../component'
import { warn } from '../warning'
import { setStyle } from './style'

export function recordPropMetadata(el: Node, key: string, value: any): any {
function getMetadata(el: Node): ElementMetadata {
const EMPTY_METADATA = { props: {} }

if (!currentInstance) {
// TODO implement error handling
if (__DEV__) throw new Error('cannot be used out of component')
return
return EMPTY_METADATA
}

let metadata = currentInstance.metadata.get(el)
if (!metadata) {
currentInstance.metadata.set(el, (metadata = { props: {} }))
currentInstance.metadata.set(el, (metadata = EMPTY_METADATA))
}
return metadata
}

export function recordPropMetadata(el: Node, key: string, value: any): any {
const metadata = getMetadata(el)
const prev = metadata.props[key]
metadata.props[key] = value
return prev
}

function getPropsMetadata(el: Node): Data | undefined {
if (!currentInstance) {
// TODO implement error handling
if (__DEV__) throw new Error('cannot be used out of component')
return
}

return currentInstance.metadata.get(el)?.props
}

export function setClass(el: Element, value: any) {
const prev = recordPropMetadata(el, 'class', (value = normalizeClass(value)))
if (value !== prev && (value || prev)) {
Expand Down Expand Up @@ -150,7 +148,7 @@ export function setDynamicProp(el: Element, key: string, value: any) {
}

export function setDynamicProps(el: Element, ...args: any) {
const oldProps = getPropsMetadata(el)
const oldProps = getMetadata(el).props
const props = args.length > 1 ? mergeProps(...args) : args[0]

for (const key in oldProps) {
Expand Down

0 comments on commit fe4744e

Please sign in to comment.