Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 18, 2024
1 parent bb168c3 commit 78e1cb7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 60 deletions.
87 changes: 51 additions & 36 deletions packages/runtime-vapor/src/apiCreateComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import {
currentInstance,
} from './component'
import { type Block, setupComponent } from './apiRender'
import type { NormalizedRawProps, RawProps } from './componentProps'
import {
type NormalizedRawProps,
type RawProps,
normalizeRawProps,
walkRawProps,
} from './componentProps'
import type { DynamicSlots, Slots } from './componentSlots'
import { walkRawProps, withAttrs } from './componentAttrs'
import { isArray, isString } from '@vue/shared'
import { withAttrs } from './componentAttrs'
import { isString } from '@vue/shared'
import { renderEffect } from './renderEffect'
import { normalizeBlock } from './dom/element'
import { setDynamicProp } from './dom/prop'
Expand All @@ -21,40 +26,9 @@ export function createComponent(
once: boolean = false,
) {
if (isString(comp)) {
// eslint-disable-next-line no-restricted-globals
const el = document.createElement(comp)

if (rawProps) {
if (!isArray(rawProps)) rawProps = [rawProps]
renderEffect(() => {
walkRawProps(rawProps as NormalizedRawProps, (key, value, getter) => {
setDynamicProp(el, key, getter ? value() : value)
})
})
}

if ((dynamicSlots && dynamicSlots.length) || (slots && slots.default)) {
renderEffect(() => {
let block: Block | undefined

if (slots && slots.default) {
block = slots.default()
} else {
for (const slotFn of dynamicSlots!) {
const slot = slotFn()
if (slot.name === 'default') {
block = slot.fn()
break
}
}
}

if (block) el.append(...normalizeBlock(block))
})
}

return el
return fallbackComponent(comp, rawProps, slots, dynamicSlots, singleRoot)
}

const current = currentInstance!
const instance = createComponentInstance(
comp,
Expand All @@ -70,3 +44,44 @@ export function createComponent(

return instance
}

function fallbackComponent(
comp: string,
rawProps: RawProps | null = null,
slots: Slots | null = null,
dynamicSlots: DynamicSlots | null = null,
singleRoot: boolean = false,
) {
// eslint-disable-next-line no-restricted-globals
const el = document.createElement(comp)

if (rawProps) {
rawProps = normalizeRawProps(rawProps)
renderEffect(() => {
walkRawProps(rawProps as NormalizedRawProps, (key, value, getter) => {
setDynamicProp(el, key, getter ? value() : value)
})
})
}

if ((dynamicSlots && dynamicSlots.length) || (slots && slots.default)) {
renderEffect(() => {
let block: Block | undefined

if (slots && slots.default) {
block = slots.default()
} else {
for (const slotFn of dynamicSlots!) {
const slot = slotFn()
if (slot.name === 'default') {
block = slot.fn()
break
}
}
}

if (block) el.append(...normalizeBlock(block))
})
}
return { __return: el, rawProps }
}
22 changes: 2 additions & 20 deletions packages/runtime-vapor/src/componentAttrs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { camelize, isArray, isFunction } from '@vue/shared'
import { camelize, isArray } from '@vue/shared'
import { type ComponentInternalInstance, currentInstance } from './component'
import { isEmitListener } from './componentEmits'
import { setDynamicProps } from './dom/prop'
import type { NormalizedRawProps, RawProps } from './componentProps'
import { type RawProps, walkRawProps } from './componentProps'
import { renderEffect } from './renderEffect'

export function patchAttrs(instance: ComponentInternalInstance) {
Expand Down Expand Up @@ -42,24 +42,6 @@ export function patchAttrs(instance: ComponentInternalInstance) {
}
}

export function walkRawProps(
rawProps: NormalizedRawProps,
cb: (key: string, value: any, getter?: boolean) => void,
) {
for (const props of Array.from(rawProps).reverse()) {
if (isFunction(props)) {
const resolved = props()
for (const rawKey in resolved) {
cb(rawKey, resolved[rawKey])
}
} else {
for (const rawKey in props) {
cb(rawKey, props[rawKey], true)
}
}
}
}

export function withAttrs(props: RawProps): RawProps {
const instance = currentInstance!
if (instance.component.inheritAttrs === false) return props
Expand Down
29 changes: 25 additions & 4 deletions packages/runtime-vapor/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ export function initProps(
isStateful: boolean,
once: boolean,
) {
if (!rawProps) rawProps = []
else if (!isArray(rawProps)) rawProps = [rawProps]
instance.rawProps = rawProps

instance.rawProps = rawProps = normalizeRawProps(rawProps)
const props: Data = {}
const attrs = (instance.attrs = shallowReactive<Data>({}))
const [options] = instance.propsOptions
Expand Down Expand Up @@ -166,6 +163,30 @@ function registerProp(
}
}

export function normalizeRawProps(rawProps: RawProps) {
if (!rawProps) return []
if (!isArray(rawProps)) return [rawProps]
return rawProps
}

export function walkRawProps(
rawProps: NormalizedRawProps,
cb: (key: string, value: any, getter?: boolean) => void,
) {
for (const props of Array.from(rawProps).reverse()) {
if (isFunction(props)) {
const resolved = props()
for (const rawKey in resolved) {
cb(rawKey, resolved[rawKey])
}
} else {
for (const rawKey in props) {
cb(rawKey, props[rawKey], true)
}
}
}
}

function getRawKey(obj: Data, key: string) {
return Object.keys(obj).find(k => camelize(k) === key)
}
Expand Down

0 comments on commit 78e1cb7

Please sign in to comment.