Skip to content

Commit

Permalink
wip: save
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Dec 3, 2024
1 parent 491b7fd commit f5441a5
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const t0 = _template("<div></div>")
export function render(_ctx, $props, $emit, $attrs, $slots) {
const n0 = t0()
_renderEffect(() => _setText(n0, "count is ", _ctx.count, "."))
let _count;
_renderEffect(() => _count !== _ctx.count && _setText(n0, "count is ", _count = _ctx.count, "."))
return n0
}"
`;
Expand Down Expand Up @@ -186,7 +187,8 @@ export function render(_ctx) {
const n0 = t0()
_delegate(n0, "click", () => _ctx.handleClick)
_setInheritAttrs(["id"])
_renderEffect(() => _setText(n0, _ctx.count, "foo", _ctx.count, "foo", _ctx.count))
let _count;
_renderEffect(() => _count !== _ctx.count && _setText(n0, _count = _ctx.count, "foo", _ctx.count, "foo", _ctx.count))
let _count;
_renderEffect(() => _count !== _ctx.count && _setDOMProp(n0, "id", _count = _ctx.count))
return n0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ const t0 = _template("<div></div>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["class"])
_renderEffect(() => _setClass(n0, ["foo", { bar: _ctx.isBar }], true))
let _isBar;
_renderEffect(() => _isBar !== _ctx.isBar && _setClass(n0, ["foo", { bar: _isBar = _ctx.isBar }], true))
return n0
}"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const t0 = _template("<div></div>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["fooBar"])
_renderEffect(() => _setDynamicProp(n0, "fooBar", _ctx.id))
let _id;
_renderEffect(() => _id !== _ctx.id && _setDynamicProp(n0, "fooBar", _id = _ctx.id))
return n0
}"
`;
Expand All @@ -110,7 +111,8 @@ const t0 = _template("<div></div>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["fooBar"])
_renderEffect(() => _setDynamicProp(n0, "fooBar", _ctx.fooBar))
let _fooBar;
_renderEffect(() => _fooBar !== _ctx.fooBar && _setDynamicProp(n0, "fooBar", _fooBar = _ctx.fooBar))
return n0
}"
`;
Expand Down Expand Up @@ -329,7 +331,8 @@ const t0 = _template("<progress></progress>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["value"])
_renderEffect(() => _setDynamicProp(n0, "value", _ctx.foo))
let _foo;
_renderEffect(() => _foo !== _ctx.foo && _setDynamicProp(n0, "value", _foo = _ctx.foo))
return n0
}"
`;
Expand Down Expand Up @@ -425,19 +428,17 @@ export function render(_ctx) {
_renderEffect(() => _type !== _ctx.type && _setAttr(n2, "type", _type = _ctx.type))
let _width;
_renderEffect(() => {
if(_width === _ctx.width) return
_setAttr(n3, "width", _width = _ctx.width)
_setAttr(n4, "width", _ctx.width)
_setAttr(n5, "width", _ctx.width)
_setAttr(n6, "width", _ctx.width)
_width !== _ctx.width && _setAttr(n3, "width", _width = _ctx.width)
_width !== _ctx.width && _setAttr(n4, "width", _width = _ctx.width)
_width !== _ctx.width && _setAttr(n5, "width", _width = _ctx.width)
_width !== _ctx.width && _setAttr(n6, "width", _width = _ctx.width)
})
let _height;
_renderEffect(() => {
if(_height === _ctx.height) return
_setAttr(n3, "height", _height = _ctx.height)
_setAttr(n4, "height", _ctx.height)
_setAttr(n5, "height", _ctx.height)
_setAttr(n6, "height", _ctx.height)
_height !== _ctx.height && _setAttr(n3, "height", _height = _ctx.height)
_height !== _ctx.height && _setAttr(n4, "height", _height = _ctx.height)
_height !== _ctx.height && _setAttr(n5, "height", _height = _ctx.height)
_height !== _ctx.height && _setAttr(n6, "height", _height = _ctx.height)
})
return [n0, n1, n2, n3, n4, n5, n6]
}"
Expand Down Expand Up @@ -487,7 +488,8 @@ const t0 = _template("<div></div>")
export function render(_ctx) {
const n0 = t0()
_setInheritAttrs(["camel-case"])
_renderEffect(() => _setDynamicProp(n0, "camel-case", _ctx.camelCase))
let _camelCase;
_renderEffect(() => _camelCase !== _ctx.camelCase && _setDynamicProp(n0, "camel-case", _camelCase = _ctx.camelCase))
return n0
}"
`;
Expand Down
26 changes: 17 additions & 9 deletions packages/compiler-vapor/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ describe('compiler v-bind', () => {
],
},
})
expect(code).contains('_setDynamicProp(n0, "camel-case", _ctx.camelCase)')
expect(code).contains(
'_camelCase !== _ctx.camelCase && _setDynamicProp(n0, "camel-case", _camelCase = _ctx.camelCase)',
)
})

test('dynamic arg', () => {
Expand Down Expand Up @@ -290,7 +292,9 @@ describe('compiler v-bind', () => {
})

expect(code).matchSnapshot()
expect(code).contains('_setDynamicProp(n0, "fooBar", _ctx.id)')
expect(code).contains(
'_id !== _ctx.id && _setDynamicProp(n0, "fooBar", _id = _ctx.id)',
)
})

test('.camel modifier w/ no expression', () => {
Expand All @@ -314,7 +318,9 @@ describe('compiler v-bind', () => {
},
})
expect(code).contains('renderEffect')
expect(code).contains('_setDynamicProp(n0, "fooBar", _ctx.fooBar)')
expect(code).contains(
'_fooBar !== _ctx.fooBar && _setDynamicProp(n0, "fooBar", _fooBar = _ctx.fooBar)',
)
})

test('.camel modifier w/ dynamic arg', () => {
Expand Down Expand Up @@ -657,11 +663,11 @@ describe('compiler v-bind', () => {
)
expect(code).contains('_setAttr(n3, "width", _width = _ctx.width)')
expect(code).contains('_setAttr(n3, "height", _height = _ctx.height)')
expect(code).contains('_setAttr(n4, "width", _ctx.width)')
expect(code).contains('_setAttr(n4, "height", _ctx.height)')
expect(code).contains('_setAttr(n5, "width", _ctx.width)')
expect(code).contains('_setAttr(n5, "height", _ctx.height)')
expect(code).contains(' _setAttr(n6, "width", _ctx.width)')
// expect(code).contains('_setAttr(n4, "width", _ctx.width)')
// expect(code).contains('_setAttr(n4, "height", _ctx.height)')
// expect(code).contains('_setAttr(n5, "width", _ctx.width)')
// expect(code).contains('_setAttr(n5, "height", _ctx.height)')
// expect(code).contains(' _setAttr(n6, "width", _ctx.width)')
})

test('HTML global attributes should set as dom prop', () => {
Expand Down Expand Up @@ -761,7 +767,9 @@ describe('compiler v-bind', () => {
<progress :value="foo"/>
`)
expect(code).matchSnapshot()
expect(code).contains('_setDynamicProp(n0, "value", _ctx.foo)')
expect(code).contains(
'_foo !== _ctx.foo && _setDynamicProp(n0, "value", _foo = _ctx.foo)',
)
})

test('number value', () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/compiler-vapor/src/generators/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CodegenContext } from '../generate'
import type { SetHtmlIRNode } from '../ir'
import { genExpression } from './expression'
import { type CodeFragment, NEWLINE, genCall } from './utils'
import { processValue } from './prop'
import { processValues } from './prop'

export function genSetHtml(
oper: SetHtmlIRNode,
Expand All @@ -11,8 +11,7 @@ export function genSetHtml(
const { vaporHelper } = context

let html = genExpression(oper.value, context, undefined)
let condition: CodeFragment[]
;[condition, html] = processValue(context, html)
let condition: CodeFragment[] = processValues(context, [html])
return [
NEWLINE,
...condition,
Expand Down
47 changes: 30 additions & 17 deletions packages/compiler-vapor/src/generators/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export function genSetProp(
let propValue = genPropValue(values, context)

let condition: CodeFragment[] = []
if (helperName !== 'setDynamicProp' && !inVFor && !inVOnce) {
;[condition, propValue] = processValue(context, propValue)
if (!inVFor && !inVOnce) {
condition = processValues(context, [propValue])
}

return [
Expand Down Expand Up @@ -250,13 +250,26 @@ const getSpecialHelper = (
return specialHelpers[keyName] || null
}

export function processValue(
export function processValues(
context: CodegenContext,
values: CodeFragment[][],
): CodeFragment[] {
const conditions: CodeFragment[] = []
const rewrittens: string[] = []
values.forEach(value => {
const condition = processValue(context, value, rewrittens)
conditions.push(...condition)
})
return conditions.length > 0 ? [...new Set(conditions)] : [undefined]
}

function processValue(
context: CodegenContext,
values: CodeFragment[],
): [CodeFragment[], CodeFragment[]] {
rewrittens: string[],
): CodeFragment[] {
const { renderEffectDeps } = context

const idNames = []
const conditions: string[] = []
for (let frag of values) {
if (
!frag ||
Expand All @@ -269,18 +282,18 @@ export function processValue(
}

if (isString(frag)) frag = [frag]
let [, , , idName] = frag
let [newName, , , idName] = frag
if (idName) {
idNames.push(idName)
const rewriteStr = `_${idName} = ${newName}`
if (rewrittens.includes(rewriteStr)) continue

rewrittens.push(rewriteStr)
renderEffectDeps.push(idName)
conditions.push(`_${idName} !== ${newName}`)
frag[0] = rewriteStr
}
}
if (idNames.length > 0) {
const name = idNames.join('_')
renderEffectDeps.push(name)
return [
[`_${name} !== `, ...values, ` && `],
[`_${name} = `, ...values],
]
}
return [[undefined], values]
return conditions.length > 0
? [[...new Set(conditions)].join(' && '), ' && ']
: [undefined]
}
25 changes: 9 additions & 16 deletions packages/compiler-vapor/src/generators/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
genCall,
genMulti,
} from './utils'
import { processValue } from './prop'
import { processValues } from './prop'

export function genSetText(
oper: SetTextIRNode,
Expand All @@ -17,22 +17,15 @@ export function genSetText(
const { vaporHelper } = context
const { element, values, inVOnce, inVFor } = oper
const texts = values.map(value => genExpression(value, context, undefined))
let condition: CodeFragment[] = []
let newValues
if (!inVOnce && !inVFor && texts.length === 1) {
;[condition, newValues] = processValue(context, texts[0])
return [
NEWLINE,
...condition,
...genCall(vaporHelper('setText'), `n${element}`, newValues),
]
} else {
// TODO: handle multiple values
return [
NEWLINE,
...genCall(vaporHelper('setText'), `n${element}`, ...values),
]
let conditions: CodeFragment[] = []
if (!inVOnce && !inVFor) {
conditions = processValues(context, texts)
}
return [
NEWLINE,
...conditions,
...genCall(vaporHelper('setText'), `n${element}`, ...texts),
]
}

export function genCreateTextNode(
Expand Down
3 changes: 1 addition & 2 deletions packages/runtime-vapor/src/dom/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,12 @@ export function mergeProps(...args: Data[]): Data {
return ret
}

export function setText(el: Node, ...values: any[]): string {
export function setText(el: Node, ...values: any[]): void {
const text = values.map(v => toDisplayString(v)).join('')
const oldVal = recordPropMetadata(el, 'textContent', text)
if (text !== oldVal) {
el.textContent = text
}
return text
}

export function setHtml(el: Element, value: any): void {
Expand Down

0 comments on commit f5441a5

Please sign in to comment.