Skip to content

Commit ff5abf0

Browse files
committed
feat(runtime-vapor): pause interop and KeepAlive input scopes with the owner
1 parent 1724aab commit ff5abf0

5 files changed

Lines changed: 201 additions & 10 deletions

File tree

‎packages/runtime-vapor/__tests__/component.spec.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,51 @@ describe('component scope pause', () => {
13371337
await nextTick()
13381338
expect(host.innerHTML).toContain('display: none')
13391339
})
1340+
1341+
test('pausing a component scope pauses the inputs of a KeepAlive child', async () => {
1342+
const data = ref<any>({ count: 0 })
1343+
const log: number[] = []
1344+
const components: any = { read: (n: number) => (log.push(n), n) }
1345+
let scope: any
1346+
components.capture = (s: any) => (scope = s)
1347+
components.Child = compile(
1348+
`<script setup vapor>
1349+
defineProps(['n'])
1350+
</script>
1351+
<template>
1352+
<i>{{ n }}</i>
1353+
</template>`,
1354+
data,
1355+
components,
1356+
)
1357+
const Parent = compile(
1358+
`<script setup vapor>
1359+
import { getCurrentScope } from 'vue'
1360+
const data = _data
1361+
const components = _components
1362+
components.capture(getCurrentScope())
1363+
</script>
1364+
<template>
1365+
<KeepAlive>
1366+
<components.Child :n="components.read(data.count)" />
1367+
</KeepAlive>
1368+
</template>`,
1369+
data,
1370+
components,
1371+
)
1372+
const { host } = define(Parent).render()
1373+
log.length = 0
1374+
1375+
scope.pause()
1376+
data.value.count++
1377+
await nextTick()
1378+
expect(log).toEqual([])
1379+
1380+
scope.resume()
1381+
await nextTick()
1382+
expect(log).toEqual([1])
1383+
expect(host.textContent).toBe('1')
1384+
})
13401385
})
13411386

13421387
function getEffectsCount(scope: EffectScope): number {

‎packages/runtime-vapor/__tests__/vdomInterop.spec.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20327,4 +20327,127 @@ describe('component scope pause across the interop boundary', () => {
2032720327
expect(log).toEqual(['fallback'])
2032820328
expect(host.innerHTML).toContain('<u>1')
2032920329
})
20330+
20331+
test('pausing a vapor parent holds a slot update queued before the pause', async () => {
20332+
const data = ref<any>({ count: 0 })
20333+
const log: string[] = []
20334+
const components: any = { log: (s: string) => void log.push(s) }
20335+
let scope: any
20336+
components.capture = (s: any) => (scope = s)
20337+
components.VdomChild = compile(
20338+
`<script setup>
20339+
import { onBeforeUpdate } from 'vue'
20340+
const components = _components
20341+
onBeforeUpdate(() => components.log('child update'))
20342+
</script>
20343+
<template>
20344+
<div><slot /></div>
20345+
</template>`,
20346+
data,
20347+
components,
20348+
{ vapor: false },
20349+
)
20350+
const Parent = compile(
20351+
`<script setup vapor>
20352+
import { getCurrentScope } from 'vue'
20353+
const data = _data
20354+
const components = _components
20355+
components.capture(getCurrentScope())
20356+
</script>
20357+
<template>
20358+
<components.VdomChild>
20359+
<span>{{ data.count }}</span>
20360+
</components.VdomChild>
20361+
</template>`,
20362+
data,
20363+
components,
20364+
)
20365+
const { html } = define(Parent).render()
20366+
20367+
data.value.count++
20368+
scope.pause()
20369+
await nextTick()
20370+
expect(log).toEqual([])
20371+
expect(html()).toBe('<div><span>0</span></div>')
20372+
20373+
scope.resume()
20374+
await nextTick()
20375+
expect(html()).toBe('<div><span>1</span></div>')
20376+
expect(log).toEqual(['child update'])
20377+
})
20378+
20379+
test('pausing a vapor parent pauses the props of a resolved async vdom child', async () => {
20380+
const data = ref<any>({ count: 0 })
20381+
const components: any = {}
20382+
let scope: any
20383+
components.capture = (s: any) => (scope = s)
20384+
const VdomChild = compile(
20385+
`<script setup>
20386+
defineProps(['n'])
20387+
</script>
20388+
<template>
20389+
<span>{{ n }}</span>
20390+
</template>`,
20391+
data,
20392+
{},
20393+
{ vapor: false },
20394+
)
20395+
components.Child = defineAsyncComponent(() => Promise.resolve(VdomChild))
20396+
const Parent = compile(
20397+
`<script setup vapor>
20398+
import { getCurrentScope } from 'vue'
20399+
const data = _data
20400+
const components = _components
20401+
components.capture(getCurrentScope())
20402+
</script>
20403+
<template>
20404+
<components.Child :n="data.count" />
20405+
</template>`,
20406+
data,
20407+
components,
20408+
)
20409+
const { html } = define(Parent).render()
20410+
await new Promise(r => setTimeout(r))
20411+
await nextTick()
20412+
expect(html()).toContain('<span>0</span>')
20413+
20414+
scope.pause()
20415+
data.value.count++
20416+
await nextTick()
20417+
expect(html()).toContain('<span>0</span>')
20418+
20419+
scope.resume()
20420+
await nextTick()
20421+
expect(html()).toContain('<span>1</span>')
20422+
})
20423+
20424+
test('pausing a vapor parent pauses the props it adds to a vnode it renders', async () => {
20425+
const data = ref<any>({ count: 0 })
20426+
const components: any = { vnode: h('b', 'x') }
20427+
let scope: any
20428+
components.capture = (s: any) => (scope = s)
20429+
const Parent = compile(
20430+
`<script setup vapor>
20431+
import { getCurrentScope } from 'vue'
20432+
const data = _data
20433+
const components = _components
20434+
components.capture(getCurrentScope())
20435+
</script>
20436+
<template>
20437+
<div><component :is="components.vnode" :title="data.count" /></div>
20438+
</template>`,
20439+
data,
20440+
components,
20441+
)
20442+
const { html } = define(Parent).render()
20443+
20444+
scope.pause()
20445+
data.value.count++
20446+
await nextTick()
20447+
expect(html()).toContain('title="0"')
20448+
20449+
scope.resume()
20450+
await nextTick()
20451+
expect(html()).toContain('title="1"')
20452+
})
2033020453
})

‎packages/runtime-vapor/src/component.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ export function createComponent(
564564

565565
let inputScope: EffectScope | undefined
566566
if (keepAliveCtx && !once && !managedMount) {
567-
inputScope = new EffectScope(true)
567+
inputScope = new EffectScope(true, currentInstance!.scope)
568568
}
569569

570570
// The VDOM bridge delivers inputs through the same cache-owned scope.

‎packages/runtime-vapor/src/vdomInterop.ts‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import {
9090
withCtx,
9191
} from '@vue/runtime-dom'
9292
import {
93-
type EffectScope,
93+
EffectScope,
9494
effectScope,
9595
endBatch,
9696
setActiveSub,
@@ -2025,7 +2025,10 @@ function mountVNode(
20252025
// happens before the mount and only establishes the dependency.
20262026
// A KeepAlive hit reuses this fragment without rebinding, so the effect
20272027
// is detached from the branch render; unmount() stops it once mounted.
2028-
const inputScope = (frag.inputScope = effectScope(true))
2028+
const inputScope = (frag.inputScope = new EffectScope(
2029+
true,
2030+
parentComponent ? parentComponent.scope : undefined,
2031+
))
20292032
inputScope.run(() => renderEffect(update))
20302033
onScopeDispose(() => {
20312034
if (!isMounted) inputScope.stop()
@@ -2103,7 +2106,12 @@ function createVDOMComponent(
21032106
let hasDynamicProps =
21042107
!once &&
21052108
(!!cells || hasDynamicPropsSource((rawProps || EMPTY_OBJ) as RawProps))
2106-
if (hasDynamicProps) inputScope ||= effectScope(true)
2109+
if (hasDynamicProps) {
2110+
inputScope ||= new EffectScope(
2111+
true,
2112+
parentComponent ? parentComponent.scope : undefined,
2113+
)
2114+
}
21072115
const prevInstance = setCurrentInstance(parentComponent, inputScope)
21082116
let vnode: VNode
21092117
try {
@@ -4518,7 +4526,9 @@ function renderVaporSlot(
45184526
const renderContent = (): void => {
45194527
content.nodes =
45204528
withRenderContext(frag.ctx, () =>
4521-
withSlotBoundary(rootBoundary, () => invokeVaporSlot(vnode)),
4529+
withSlotBoundary(rootBoundary, () =>
4530+
invokeVaporSlot(vnode, parentComponent),
4531+
),
45224532
) || EMPTY_BLOCK
45234533
if (fallthrough) {
45244534
const scope = vnode.vs!.scope
@@ -4606,11 +4616,18 @@ function stopVaporSlotScope(vnode: VNode): void {
46064616
* Slot functions can create renderEffects while evaluating their block.
46074617
* Those effects live in this detached scope, which only the vnode's re-mount
46084618
* and unmount stop: after the vapor component passing the slot unmounted, not
4609-
* with the scope that happens to render the vdom outlet.
4619+
* with the scope that happens to render the vdom outlet, though it pauses with
4620+
* the outlet's instance.
46104621
*/
4611-
function invokeVaporSlot(vnode: VNode): Block {
4622+
function invokeVaporSlot(
4623+
vnode: VNode,
4624+
parentComponent: ComponentInternalInstance | null,
4625+
): Block {
46124626
const propsRef = (vnode.vs!.ref = shallowRef(vnode.vs!.props || vnode.props))
4613-
const scope = effectScope(true)
4627+
const scope = new EffectScope(
4628+
true,
4629+
parentComponent ? parentComponent.scope : undefined,
4630+
)
46144631
vnode.vs!.scope = scope
46154632
try {
46164633
const invoke = () =>

‎packages/runtime-vapor/src/vdomSlotOwner.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ export function initVdomOwner(owner: ComponentInternalInstance): void {
3838
updating: false,
3939
update: () => {
4040
// the owner may have been removed in this flush: its effect stops at
41-
// once, while `isUnmounted` waits for the post flush
42-
if (state.stale && owner.effect.active) owner.update()
41+
// once, while `isUnmounted` waits for the post flush. A paused owner
42+
// is queued again by the content's effects when it resumes
43+
if (
44+
state.stale &&
45+
!(owner.effect.flags & (EffectFlags.STOP | EffectFlags.PAUSED))
46+
) {
47+
owner.update()
48+
}
4349
},
4450
}
4551
state.update.i = owner

0 commit comments

Comments
 (0)