Skip to content
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

fix(runtime-core): improve v-memo work with v-if #6606

Merged
merged 4 commits into from
Jun 7, 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
11 changes: 11 additions & 0 deletions packages/runtime-core/__tests__/helpers/withMemo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ describe('v-memo', () => {
// should update
await nextTick()
expect(el.innerHTML).toBe(`<div>3 3</div>`)

vm.ok = true
await nextTick()
vm.ok = false
await nextTick()
expect(el.innerHTML).toBe(`<div>3 3</div>`)

vm.y++
// should update
await nextTick()
expect(el.innerHTML).toBe(`<div>4 3</div>`)
})

test('on v-for', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export interface ComponentInternalInstance {
* after initialized (e.g. inline handlers)
* @internal
*/
renderCache: (Function | VNode)[]
renderCache: (Function | VNode | undefined)[]

/**
* Resolved component registry, only for components with mixins or extends
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/helpers/withMemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export function withMemo(

// shallow clone
ret.memo = memo.slice()
ret.memoIndex = index

return (cache[index] = ret)
}

Expand Down
6 changes: 6 additions & 0 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2110,12 +2110,18 @@ function baseCreateRenderer(
shapeFlag,
patchFlag,
dirs,
memoIndex,
} = vnode
// unset ref
if (ref != null) {
setRef(ref, null, parentSuspense, vnode, true)
}

// #6593 should clean memo cache when unmount
if (memoIndex != null) {
parentComponent!.renderCache[memoIndex] = undefined
}

if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
return
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ export interface VNode<
* @internal attached by v-memo
*/
memo?: any[]
/**
* @internal index for cleaning v-memo cache
*/
memoIndex?: number
/**
* @internal __COMPAT__ only
*/
Expand Down