Skip to content
Closed
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
24 changes: 24 additions & 0 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,30 @@ describe('reactivity/effect/scope', () => {
expect(fnSpy).toHaveBeenCalledTimes(3)
})

it('should pause effects created in a paused scope', () => {
const count = ref(0)
const effectSpy = vi.fn(() => count.value)
const nestedEffectSpy = vi.fn(() => count.value)
const scope = effectScope()

scope.pause()
scope.run(() => {
effect(effectSpy)
effectScope().run(() => effect(nestedEffectSpy))
})

expect(effectSpy).toHaveBeenCalledOnce()
expect(nestedEffectSpy).toHaveBeenCalledOnce()

count.value++
expect(effectSpy).toHaveBeenCalledOnce()
expect(nestedEffectSpy).toHaveBeenCalledOnce()

scope.resume()
expect(effectSpy).toHaveBeenCalledTimes(2)
expect(nestedEffectSpy).toHaveBeenCalledTimes(2)
})

test('removing a watcher while stopping its effectScope', async () => {
const count = ref(0)
const scope = effectScope()
Expand Down
2 changes: 2 additions & 0 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export class ReactiveEffect<T = any>
}
if (activeEffectScope) {
link(this, activeEffectScope)
// pause() only visits existing effects, so inherit the current state.
this.flags |= activeEffectScope.flags & EffectFlags.PAUSED
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class EffectScope implements ReactiveNode {
constructor(detached = false) {
if (!detached && activeEffectScope) {
link(this, activeEffectScope)
// pause() only visits existing child scopes, so inherit the current state.
this.flags |= activeEffectScope.flags & EffectFlags.PAUSED
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class RenderWatcherEffect extends WatcherEffect {
super(source, cb, options)

const job: SchedulerJob = () => {
if (this.dirty) {
// The job may have been queued before its scope was paused.
if (!(this.flags & EffectFlags.PAUSED) && this.dirty) {
this.run()
}
}
Expand Down
219 changes: 219 additions & 0 deletions packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ref,
shallowRef,
vModelText,
watch,
withDirectives,
} from 'vue'
import {
Expand Down Expand Up @@ -1699,6 +1700,224 @@ describe('VaporKeepAlive', () => {
})
})

test('should not update a cached child while deactivating it', async () => {
const Child = compile(
`<script setup vapor>
defineProps({ item: { type: Object, required: true } })
</script>
<template><p>child: {{ item.id }}</p></template>`,
ref(),
)
const selected = ref<{ id: string } | undefined>({ id: 'A' })
const App = compile(
`<script setup vapor>
const selected = _data
const Child = _components.Child
</script>
<template>
<KeepAlive>
<Child v-if="selected" :item="selected" />
</KeepAlive>
</template>`,
selected,
{ Child },
)
const { host } = define(App).render()

expect(host.textContent).toBe('child: A')

selected.value = undefined
await nextTick()
expect(host.innerHTML).toBe('<!--if-->')

selected.value = { id: 'B' }
await nextTick()
expect(host.textContent).toBe('child: B')
expect('type check failed for prop "item"').not.toHaveBeenWarned()
})

test('should not update a nested KeepAlive slot while its owner is cached', async () => {
const Child = compile(
`<script setup vapor>
defineProps({ item: { type: Object, required: true } })
</script>
<template><p>child: {{ item.id }}</p></template>`,
ref(),
)
const Nested = compile(
`<script setup vapor>
defineProps({ item: { type: Object, required: true } })
const Child = _components.Child
</script>
<template>
<KeepAlive>
<Child v-if="item.visible" :item="item" />
</KeepAlive>
</template>`,
ref(),
{ Child },
)
const selected = ref<{ id: string; visible: boolean } | undefined>({
id: 'A',
visible: true,
})
const App = compile(
`<script setup vapor>
const selected = _data
const Nested = _components.Nested
</script>
<template>
<KeepAlive>
<Nested v-if="selected" :item="selected" />
</KeepAlive>
</template>`,
selected,
{ Nested },
)
const { host } = define(App).render()

expect(host.textContent).toBe('child: A')

selected.value = undefined
await nextTick()
expect(host.innerHTML).toBe('<!--if-->')

selected.value = { id: 'B', visible: true }
await nextTick()
expect(host.textContent).toBe('child: B')
expect('type check failed for prop "item"').not.toHaveBeenWarned()
})

test('should defer resolving a cached async component until activation', async () => {
let resolve: (comp: VaporComponent) => void
const setups = ref(0)
const Child = compile(
`<script setup vapor>
const setups = _data
setups.value++
defineProps({ item: { type: Object, required: true } })
</script>
<template><p>child: {{ item.id }}</p></template>`,
setups,
)
const AsyncChild = defineVaporAsyncComponent(
() =>
new Promise(r => {
resolve = r as any
}),
)
const selected = ref<{ id: string } | undefined>({ id: 'A' })
const App = compile(
`<script setup vapor>
const selected = _data
const AsyncChild = _components.AsyncChild
</script>
<template>
<KeepAlive>
<AsyncChild v-if="selected" :item="selected" />
</KeepAlive>
</template>`,
selected,
{ AsyncChild },
)
const { host } = define(App).render()

selected.value = undefined
await nextTick()
resolve!(Child)
await timeout()

expect(setups.value).toBe(0)

selected.value = { id: 'B' }
await nextTick()
expect(setups.value).toBe(1)
expect(host.textContent).toBe('child: B')
expect('type check failed for prop "item"').not.toHaveBeenWarned()
})

test('should pause effects under nested KeepAlive boundaries', async () => {
const toggle = ref(true)
const innerToggle = ref(true)
const count = ref(0)
const render = vi.fn()
const watcher = vi.fn()
const providerWatcher = vi.fn()
const NestedChild = defineVaporComponent({
setup() {
watch(count, watcher)
const n0 = template(`<div> </div>`)() as any
const x0 = child(n0) as any
renderEffect(() => {
render()
setText(x0, String(count.value))
})
return n0
},
})
const CachedRoot = defineVaporComponent({
setup() {
watch(
count,
value => {
providerWatcher()
if (value === 2) innerToggle.value = false
},
{ flush: 'sync' },
)
return createComponent(VaporKeepAlive, null, {
default: () =>
createIf(
() => innerToggle.value,
() => createComponent(NestedChild),
),
})
},
})
const { html } = define({
setup() {
return createComponent(VaporKeepAlive, null, {
default: () =>
createIf(
() => toggle.value,
() => createComponent(CachedRoot),
),
})
},
}).render()

expect(render).toHaveBeenCalledTimes(1)

count.value = 1
toggle.value = false
await nextTick()
count.value = 2
await nextTick()
expect(render).toHaveBeenCalledTimes(1)
expect(providerWatcher).toHaveBeenCalledOnce()
expect(watcher).not.toHaveBeenCalled()

innerToggle.value = false
await nextTick()
innerToggle.value = true
await nextTick()
expect(render).toHaveBeenCalledTimes(1)
expect(watcher).not.toHaveBeenCalled()

toggle.value = true
await nextTick()
expect(html()).toBe(`<!--if--><!--if-->`)
expect(render).toHaveBeenCalledTimes(1)
expect(providerWatcher).toHaveBeenCalledTimes(2)
expect(watcher).not.toHaveBeenCalled()

innerToggle.value = true
await nextTick()
expect(html()).toBe(`<div>2</div><!--if--><!--if-->`)
expect(render).toHaveBeenCalledTimes(2)
expect(watcher).toHaveBeenCalledOnce()
})

test('should work with async component', async () => {
let resolve: (comp: VaporComponent) => void
const AsyncComp = defineVaporAsyncComponent(
Expand Down
14 changes: 14 additions & 0 deletions packages/runtime-vapor/__tests__/renderEffect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ describe('renderEffect', () => {
expect(dummy).toBe(3)
})

test('defers the initial run in a paused scope', async () => {
const scope = new EffectScope()
const render = vi.fn()
scope.pause()

scope.run(() => renderEffect(render))
expect(render).not.toHaveBeenCalled()

scope.resume()
await nextTick()
expect(render).toHaveBeenCalledOnce()
scope.stop()
})

test('should run with the scheduling order', async () => {
const calls: string[] = []

Expand Down
Loading
Loading