Skip to content

feat: v-once for component / v-once with v-for #201

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

Merged
merged 3 commits into from
May 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ export function render(_ctx) {
}"
`;

exports[`compiler: v-once > on component 1`] = `
"import { resolveComponent as _resolveComponent, createComponent as _createComponent, insert as _insert, template as _template } from 'vue/vapor';
const t0 = _template("<div></div>")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n1 = t0()
const n0 = _createComponent(_component_Comp, [
{ id: () => (_ctx.foo) }
], null, null, null, true)
_insert(n0, n1)
return n1
}"
`;

exports[`compiler: v-once > on nested plain element 1`] = `
"import { setDynamicProp as _setDynamicProp, template as _template } from 'vue/vapor';
const t0 = _template("<div><div></div></div>")
Expand All @@ -47,6 +62,19 @@ export function render(_ctx) {
}"
`;

exports[`compiler: v-once > with v-for 1`] = `
"import { createFor as _createFor, template as _template } from 'vue/vapor';
const t0 = _template("<div></div>")

export function render(_ctx) {
const n0 = _createFor(() => (_ctx.list), (_block) => {
const n2 = t0()
return [n2, () => {}]
}, null, null, null, true)
return n0
}"
`;

exports[`compiler: v-once > with v-if 1`] = `
"import { createIf as _createIf, template as _template } from 'vue/vapor';
const t0 = _template("<div></div>")
Expand Down
33 changes: 31 additions & 2 deletions packages/compiler-vapor/__tests__/transforms/vOnce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,25 @@ describe('compiler: v-once', () => {
])
})

test.todo('on component')
test('on component', () => {
const { ir, code } = compileWithOnce(`<div><Comp :id="foo" v-once /></div>`)
expect(code).toMatchSnapshot()
expect(ir.block.effect).lengthOf(0)
expect(ir.block.operation).toMatchObject([
{
type: IRNodeTypes.CREATE_COMPONENT_NODE,
id: 0,
tag: 'Comp',
once: true,
},
{
type: IRNodeTypes.INSERT_NODE,
elements: [0],
parent: 1,
},
])
})

test.todo('on slot outlet')

test('inside v-once', () => {
Expand Down Expand Up @@ -205,5 +223,16 @@ describe('compiler: v-once', () => {
])
})

test.todo('with v-for')
test('with v-for', () => {
const { ir, code } = compileWithOnce(`<div v-for="i in list" v-once />`)
expect(code).toMatchSnapshot()
expect(ir.block.effect).lengthOf(0)
expect(ir.block.operation).toMatchObject([
{
type: IRNodeTypes.FOR,
id: 0,
once: true,
},
])
})
})
5 changes: 3 additions & 2 deletions packages/compiler-vapor/src/generators/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function genCreateComponent(
const { vaporHelper } = context

const tag = genTag()
const { root, slots, dynamicSlots } = oper
const { root, slots, dynamicSlots, once } = oper
const rawProps = genRawProps(oper.props, context)

return [
Expand All @@ -46,7 +46,8 @@ export function genCreateComponent(
rawProps,
slots && genSlots(slots, context),
dynamicSlots && genDynamicSlots(dynamicSlots, context),
root && 'true',
root ? 'true' : false,
once && 'true',
),
...genDirectivesForElement(oper.id, context),
]
Expand Down
13 changes: 10 additions & 3 deletions packages/compiler-vapor/src/generators/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function genFor(
context: CodegenContext,
): CodeFragment[] {
const { vaporHelper, genEffects } = context
const { source, value, key, index, render, keyProp } = oper
const { source, value, key, index, render, keyProp, once } = oper

const rawValue = value && value.content
const rawKey = key && key.content
Expand Down Expand Up @@ -67,11 +67,18 @@ export function genFor(
}

genEffects.pop()

return [
NEWLINE,
`const n${oper.id} = `,
...genCall(vaporHelper('createFor'), sourceExpr, blockFn, getKeyFn),
...genCall(
vaporHelper('createFor'),
sourceExpr,
blockFn,
getKeyFn,
false, // todo: getMemo
false, // todo: hydrationNode
once && 'true',
),
]

function genEffectInFor(effects: IREffect[]): CodeFragment[] {
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-vapor/src/ir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface ForIRNode extends BaseIRNode {
index?: SimpleExpressionNode
keyProp?: SimpleExpressionNode
render: BlockIRNode
once: boolean
}

export interface IRProp extends Omit<DirectiveTransformResult, 'value'> {
Expand Down Expand Up @@ -224,6 +225,7 @@ export interface CreateComponentIRNode extends BaseIRNode {

asset: boolean
root: boolean
once: boolean
}

export interface DeclareOldRefIRNode extends BaseIRNode {
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-vapor/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function transformComponentElement(
root,
slots: context.slots,
dynamicSlots: context.dynamicSlots,
once: context.inVOnce,
})
context.slots = undefined
context.dynamicSlots = undefined
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-vapor/src/transforms/vFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function processFor(
index: index as SimpleExpressionNode | undefined,
keyProp: keyProperty,
render,
once: context.inVOnce,
})
}
}
2 changes: 2 additions & 0 deletions packages/runtime-vapor/src/apiCreateComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export function createComponent(
slots: Slots | null = null,
dynamicSlots: DynamicSlots | null = null,
singleRoot: boolean = false,
once: boolean = false,
) {
const current = currentInstance!
const instance = createComponentInstance(
comp,
singleRoot ? withAttrs(rawProps) : rawProps,
slots,
dynamicSlots,
once,
)
setupComponent(instance, singleRoot)

Expand Down
12 changes: 7 additions & 5 deletions packages/runtime-vapor/src/apiCreateFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const createFor = (
getKey?: (item: any, key: any, index?: number) => any,
getMemo?: (item: any, key: any, index?: number) => any[],
hydrationNode?: Node,
once?: boolean,
): Fragment => {
let isMounted = false
let oldBlocks: ForBlock[] = []
Expand All @@ -32,9 +33,12 @@ export const createFor = (
nodes: oldBlocks,
[fragmentKey]: true,
}

const update = getMemo ? updateWithMemo : updateWithoutMemo
renderEffect(() => {
once ? renderList() : renderEffect(renderList)

return ref

function renderList() {
const source = src()
const newLength = getLength(source)
const oldLength = oldBlocks.length
Expand Down Expand Up @@ -213,9 +217,7 @@ export const createFor = (
}

ref.nodes = [(oldBlocks = newBlocks), parentAnchor]
})

return ref
}

function mount(
source: any,
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-vapor/src/apiCreateVaporApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function createVaporApp(
rootProps,
null,
null,
false,
context,
)
setupComponent(instance)
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-vapor/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export function createComponentInstance(
rawProps: RawProps | null,
slots: Slots | null,
dynamicSlots: DynamicSlots | null,
once: boolean = false,
// application root node only
appContext?: AppContext,
): ComponentInternalInstance {
Expand Down Expand Up @@ -354,7 +355,7 @@ export function createComponentInstance(
*/
// [VaporLifecycleHooks.SERVER_PREFETCH]: null,
}
initProps(instance, rawProps, !isFunction(component))
initProps(instance, rawProps, !isFunction(component), once)
initSlots(instance, slots, dynamicSlots)
instance.emit = emit.bind(null, instance)

Expand Down
15 changes: 8 additions & 7 deletions packages/runtime-vapor/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function initProps(
instance: ComponentInternalInstance,
rawProps: RawProps,
isStateful: boolean,
once: boolean,
) {
if (!rawProps) rawProps = []
else if (!isArray(rawProps)) rawProps = [rawProps]
Expand All @@ -97,16 +98,16 @@ export function initProps(
for (const key in options) {
const getter = () =>
getDynamicPropValue(rawProps as NormalizedRawProps, key)
registerProp(instance, props, key, getter, true)
registerProp(instance, once, props, key, getter, true)
}
} else {
const staticProps = rawProps[0] as StaticProps
for (const key in options) {
const rawKey = staticProps && getRawKey(staticProps, key)
if (rawKey) {
registerProp(instance, props, key, staticProps[rawKey])
registerProp(instance, once, props, key, staticProps[rawKey])
} else {
registerProp(instance, props, key, undefined, false, true)
registerProp(instance, once, props, key, undefined, false, true)
}
}
}
Expand All @@ -133,6 +134,7 @@ export function initProps(

function registerProp(
instance: ComponentInternalInstance,
once: boolean,
props: Data,
rawKey: string,
getter?: (() => unknown) | (() => DynamicPropResult),
Expand All @@ -158,10 +160,9 @@ function registerProp(
? () => withCast(getter!())
: getter!

Object.defineProperty(props, key, {
get,
enumerable: true,
})
const descriptor: PropertyDescriptor = once ? { value: get() } : { get }
descriptor.enumerable = true
Object.defineProperty(props, key, descriptor)
}
}

Expand Down