onUpdated,
provide,
ref,
+ useAttrs,
watch,
watchEffect,
} from '@vue/runtime-dom'
createComponent,
createIf,
createTextNode,
+ defineVaporComponent,
renderEffect,
template,
} from '../src'
expect(i.scope.effects.length).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 frag = __DEV__
? new DynamicFragment('dynamic-component')
rawProps,
rawSlots,
isSingleRoot,
+ once,
),
value,
)
rawProps?: LooseRawProps | null,
rawSlots?: LooseRawSlots | null,
isSingleRoot?: boolean,
+ once?: boolean,
appContext: GenericAppContext = (currentInstance &&
currentInstance.appContext) ||
emptyContext,
rawProps as RawProps,
rawSlots as RawSlots,
appContext,
+ once,
)
if (__DEV__) {
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,
): HTMLElement | VaporComponentInstance {
if (!isString(comp)) {
- return createComponent(comp, rawProps, rawSlots, isSingleRoot)
+ return createComponent(comp, rawProps, rawSlots, isSingleRoot, once)
}
const el = document.createElement(comp)
} from '@vue/runtime-dom'
import { normalizeEmitsOptions } from './componentEmits'
import { renderEffect } from './renderEffect'
+import { pauseTracking, resetTracking } from '@vue/reactivity'
export type RawProps = Record<string, () => unknown> & {
// generated by compiler for :[key]="x" or v-bind="x"
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),
}
}
},
if (dynamicSources) {
let i = dynamicSources.length
while (i--) {
- if (hasOwn(resolveSource(dynamicSources[i]), key)) {
+ const source = resolveSource(dynamicSources[i])
+ if (source && hasOwn(source, key)) {
return true
}
}