expect(html()).toBe('<baz></baz><!--dynamic-component-->')
})
+ test('with v-once', async () => {
+ const val = shallowRef<any>(A)
+
+ const { html } = define({
+ setup() {
+ return createDynamicComponent(() => val.value, null, null, true, true)
+ },
+ }).render()
+
+ expect(html()).toBe('AAA<!--dynamic-component-->')
+
+ val.value = B
+ await nextTick()
+ expect(html()).toBe('AAA<!--dynamic-component-->') // still AAA
+ })
+
+ test('fallback with v-once', async () => {
+ const val = shallowRef<any>('button')
+ const id = ref(0)
+ const { html } = define({
+ setup() {
+ return createDynamicComponent(
+ () => val.value,
+ { id: () => id.value },
+ null,
+ true,
+ true,
+ )
+ },
+ }).render()
+
+ expect(html()).toBe('<button id="0"></button><!--dynamic-component-->')
+
+ id.value++
+ await nextTick()
+ expect(html()).toBe('<button id="0"></button><!--dynamic-component-->')
+ })
+
test('render fallback with insertionState', async () => {
const { html, mount } = define({
setup() {
onUpdated,
provide,
ref,
+ useAttrs,
watch,
watchEffect,
} from '@vue/runtime-dom'
createComponent,
createIf,
createTextNode,
+ defineVaporComponent,
renderEffect,
setInsertionState,
template,
expect(getEffectsCount(i.scope)).toBe(0)
})
+ it('work with v-once + props', () => {
+ const Child = defineVaporComponent({
+ props: {
+ count: Number,
+ },
+ setup(props) {
+ const n0 = template(' ')() as any
+ renderEffect(() => setText(n0, props.count))
+ return n0
+ },
+ })
+
+ const count = ref(0)
+ const { html } = define({
+ setup() {
+ return createComponent(
+ Child,
+ { count: () => count.value },
+ null,
+ true,
+ true, // v-once
+ )
+ },
+ }).render()
+
+ expect(html()).toBe('0')
+
+ count.value++
+ expect(html()).toBe('0')
+ })
+
+ it('work with v-once + attrs', () => {
+ const Child = defineVaporComponent({
+ setup() {
+ const attrs = useAttrs()
+ const n0 = template(' ')() as any
+ renderEffect(() => setText(n0, attrs.count as string))
+ return n0
+ },
+ })
+
+ const count = ref(0)
+ const { html } = define({
+ setup() {
+ return createComponent(
+ Child,
+ { count: () => count.value },
+ null,
+ true,
+ true, // v-once
+ )
+ },
+ }).render()
+
+ expect(html()).toBe('0')
+
+ count.value++
+ expect(html()).toBe('0')
+ })
+
test('should mount component only with template in production mode', () => {
__DEV__ = false
const { component: Child } = define({
app._props as RawProps,
null,
false,
+ false,
app._context,
)
mountComponent(instance, container)
app._props as RawProps,
null,
false,
+ false,
app._context,
)
mountComponent(instance, container)
rawProps?: RawProps | null,
rawSlots?: RawSlots | null,
isSingleRoot?: boolean,
+ once?: boolean,
): VaporFragment {
const _insertionParent = insertionParent
const _insertionAnchor = insertionAnchor
? new DynamicFragment('dynamic-component')
: new DynamicFragment()
- renderEffect(() => {
+ const renderFn = () => {
const value = getter()
const appContext =
(currentInstance && currentInstance.appContext) || emptyContext
rawProps,
rawSlots,
isSingleRoot,
+ once,
appContext,
),
value,
)
- })
+ }
+
+ if (once) renderFn()
+ else renderEffect(renderFn)
if (!isHydrating) {
if (_insertionParent) insert(frag, _insertionParent, _insertionAnchor)
rawProps,
rawSlots,
isSingleRoot,
+ undefined,
appContext,
)
getPropsProxyHandlers,
hasFallthroughAttrs,
normalizePropsOptions,
+ resolveDynamicProps,
setupPropsValidation,
} from './componentProps'
import { type RenderEffect, renderEffect } from './renderEffect'
rawProps?: LooseRawProps | null,
rawSlots?: LooseRawSlots | null,
isSingleRoot?: boolean,
+ once?: boolean,
appContext: GenericAppContext = (currentInstance &&
currentInstance.appContext) ||
emptyContext,
rawProps as RawProps,
rawSlots as RawSlots,
appContext,
+ once,
)
// HMR
rawProps?: RawProps | null,
rawSlots?: RawSlots | null,
appContext?: GenericAppContext,
+ once?: boolean,
) {
this.vapor = true
this.uid = nextUid()
this.rawProps = rawProps || EMPTY_OBJ
this.hasFallthrough = hasFallthroughAttrs(comp, rawProps)
if (rawProps || comp.props) {
- const [propsHandlers, attrsHandlers] = getPropsProxyHandlers(comp)
+ const [propsHandlers, attrsHandlers] = getPropsProxyHandlers(comp, once)
this.attrs = new Proxy(this, attrsHandlers)
this.props = comp.props
? new Proxy(this, propsHandlers!)
rawProps?: LooseRawProps | null,
rawSlots?: LooseRawSlots | null,
isSingleRoot?: boolean,
+ once?: boolean,
appContext?: GenericAppContext,
): HTMLElement | VaporComponentInstance {
if (!isString(comp)) {
- return createComponent(comp, rawProps, rawSlots, isSingleRoot, appContext)
+ return createComponent(
+ comp,
+ rawProps,
+ rawSlots,
+ isSingleRoot,
+ once,
+ appContext,
+ )
}
const _insertionParent = insertionParent
// mark single root
;(el as any).$root = isSingleRoot
+ if (rawProps) {
+ const setFn = () =>
+ setDynamicProps(el, [resolveDynamicProps(rawProps as RawProps)])
+ if (once) setFn()
+ else renderEffect(setFn)
+ }
+
if (rawSlots) {
let nextNode: Node | null = null
if (isHydrating) {
import { ReactiveFlags } from '@vue/reactivity'
import { normalizeEmitsOptions } from './componentEmits'
import { renderEffect } from './renderEffect'
+import { pauseTracking, resetTracking } from '@vue/reactivity'
import type { interopKey } from './vdomInterop'
export type RawProps = Record<string, () => unknown> & {
export function getPropsProxyHandlers(
comp: VaporComponent,
+ once?: boolean,
): [
ProxyHandler<VaporComponentInstance> | null,
ProxyHandler<VaporComponentInstance>,
)
}
+ const getPropValue = once
+ ? (...args: Parameters<typeof getProp>) => {
+ pauseTracking()
+ const value = getProp(...args)
+ resetTracking()
+ return value
+ }
+ : getProp
+
const propsHandlers = propsOptions
? ({
- get: (target, key) => getProp(target, key),
+ get: (target, key) => getPropValue(target, key),
has: (_, key) => isProp(key),
ownKeys: () => Object.keys(propsOptions),
getOwnPropertyDescriptor(target, key) {
return {
configurable: true,
enumerable: true,
- get: () => getProp(target, key),
+ get: () => getPropValue(target, key),
}
}
},
}
}
+ const getAttrValue = once
+ ? (...args: Parameters<typeof getAttr>) => {
+ pauseTracking()
+ const value = getAttr(...args)
+ resetTracking()
+ return value
+ }
+ : getAttr
+
const attrsHandlers = {
- get: (target, key: string) => getAttr(target.rawProps, key),
+ get: (target, key: string) => getAttrValue(target.rawProps, key),
has: (target, key: string) => hasAttr(target.rawProps, key),
ownKeys: target => getKeysFromRawProps(target.rawProps).filter(isAttr),
getOwnPropertyDescriptor(target, key: string) {
return {
configurable: true,
enumerable: true,
- get: () => getAttr(target.rawProps, key),
+ get: () => getAttrValue(target.rawProps, key),
}
}
},
_: slotsRef, // pass the slots ref
} as any as RawSlots,
undefined,
+ undefined,
(parentComponent ? parentComponent.appContext : vnode.appContext) as any,
))
instance.rawPropsRef = propsRef