Skip to content

Commit

Permalink
fix(vMemo/vFor): add cacheIndex to v-for items for clean renderCache …
Browse files Browse the repository at this point in the history
…during unmounting
  • Loading branch information
edison1105 committed Jan 14, 2025
1 parent 2e6ec39 commit cf88048
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ exports[`compiler: v-memo transform > element v-for key expression prefixing + v
export function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock("div", null, [
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(_ctx.tableData, (data, __, ___, _cached) => {
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(_ctx.tableData, (data, _key, ___, _cached) => {
const _memo = (_ctx.getLetter(data))
if (_cached && _cached.key === _ctx.getId(data) && _isMemoSame(_cached, _memo)) return _cached
const _item = (_openBlock(), _createElementBlock("span", {
key: _ctx.getId(data)
}))
_item.memo = _memo
_item.cacheIndex = _key
return _item
}, _cache, 0), 128 /* KEYED_FRAGMENT */))
]))
Expand Down Expand Up @@ -53,11 +54,12 @@ exports[`compiler: v-memo transform > on template v-for 1`] = `
export function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock("div", null, [
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(_ctx.list, ({ x, y }, __, ___, _cached) => {
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(_ctx.list, ({ x, y }, _key, ___, _cached) => {
const _memo = ([x, y === _ctx.z])
if (_cached && _cached.key === x && _isMemoSame(_cached, _memo)) return _cached
const _item = (_openBlock(), _createElementBlock("span", { key: x }, "foobar"))
_item.memo = _memo
_item.cacheIndex = _key
return _item
}, _cache, 0), 128 /* KEYED_FRAGMENT */))
]))
Expand All @@ -69,13 +71,14 @@ exports[`compiler: v-memo transform > on v-for 1`] = `
export function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock("div", null, [
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(_ctx.list, ({ x, y }, __, ___, _cached) => {
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(_ctx.list, ({ x, y }, _key, ___, _cached) => {
const _memo = ([x, y === _ctx.z])
if (_cached && _cached.key === x && _isMemoSame(_cached, _memo)) return _cached
const _item = (_openBlock(), _createElementBlock("div", { key: x }, [
_createElementVNode("span", null, "foobar")
]))
_item.memo = _memo
_item.cacheIndex = _key
return _item
}, _cache, 0), 128 /* KEYED_FRAGMENT */))
]))
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-core/src/transforms/vFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export const transformFor: NodeTransform = createStructuralDirectiveTransform(
}

if (memo) {
forNode.parseResult.key = createSimpleExpression('_key')
const loop = createFunctionExpression(
createForLoopParams(forNode.parseResult, [
createSimpleExpression(`_cached`),
Expand All @@ -234,6 +235,7 @@ export const transformFor: NodeTransform = createStructuralDirectiveTransform(
]),
createCompoundExpression([`const _item = `, childBlock as any]),
createSimpleExpression(`_item.memo = _memo`),
createSimpleExpression(`_item.cacheIndex = _key`),
createSimpleExpression(`return _item`),
])
renderExp.arguments.push(
Expand Down

0 comments on commit cf88048

Please sign in to comment.