Skip to content
Open
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
8 changes: 8 additions & 0 deletions packages-private/dts-test/reactivity.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
EffectScope,
type Ref,
markRaw,
reactive,
Expand Down Expand Up @@ -146,3 +147,10 @@ describe('shallowReactive type should accept plain object assignment', () => {
let values: typeof shallow
values = { a: 1, b: 2 }
})

describe('EffectScope can be constructed', () => {
new EffectScope()
new EffectScope(true)
// @ts-expect-error the parent is internal
new EffectScope(true, new EffectScope())
})
132 changes: 132 additions & 0 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,138 @@ describe('reactivity/effect/scope', () => {
scope.resume()
expect(log).toEqual(['last 0', 'queued', 'last 1'])
})

it('a scope linked to a parent for pausing follows it but is not stopped with it', () => {
const count = ref(0)
const log: number[] = []
const parent = effectScope()
const child = new EffectScope(true, parent)
child.run(() => effect(() => log.push(count.value)))
parent.pause()
count.value++
expect(log).toEqual([0])

parent.resume()
expect(log).toEqual([0, 1])

parent.stop()
expect(child.prevDetached).toBeUndefined()
parent.pause()
count.value++
expect(log).toEqual([0, 1, 2])
})

it('a scope linked to a paused parent starts paused', () => {
const count = ref(0)
const log: number[] = []
const parent = effectScope()
parent.pause()
const child = new EffectScope(true, parent)
child.run(() => effect(() => log.push(count.value)))
count.value++
expect(log).toEqual([0])

parent.resume()
expect(log).toEqual([0, 1])
})

it('a scope linked for pausing leaves its parent when it stops', () => {
const count = ref(0)
const log: string[] = []
const parent = effectScope()
const children = ['a', 'b', 'c', 'd'].map(name => {
const child = new EffectScope(true, parent)
child.run(() => effect(() => log.push(name + count.value)))
return child
})
children[0].stop()
children[2].stop()
children[3].stop()
parent.pause()
count.value++
expect(log).toEqual(['a0', 'b0', 'c0', 'd0'])

parent.resume()
expect(log).toEqual(['a0', 'b0', 'c0', 'd0', 'b1'])
expect(parent.detachedScopes).toBe(children[1])
expect(children[1].prevDetached).toBe(parent)
expect(children[1].nextDetached).toBeUndefined()

children[1].stop()
expect(parent.detachedScopes).toBeUndefined()
expect(children[1].prevDetached).toBeUndefined()
})

it('resumes the effects of scopes linked for pausing in creation order', () => {
const count = ref(0)
const log: string[] = []
const parent = effectScope()
for (const name of ['a', 'b', 'c']) {
new EffectScope(true, parent).run(() =>
effect(() => log.push(name + count.value)),
)
}
parent.pause()
count.value++
log.length = 0

parent.resume()
expect(log).toEqual(['a1', 'b1', 'c1'])
})

it('a scope linked to a parent that stops while paused stays paused', () => {
const count = ref(0)
const log: number[] = []
const parent = effectScope()
const child = new EffectScope(true, parent)
child.run(() => effect(() => log.push(count.value)))
parent.pause()
count.value++
parent.stop()
expect(log).toEqual([0])
expect(child.active).toBe(true)
})

it('resumes the scopes linked after one that stops itself and the next', () => {
const count = ref(0)
const log: number[] = []
const parent = effectScope()
const first = new EffectScope(true, parent)
const second = new EffectScope(true, parent)
first.run(() =>
watch(
count,
() => {
first.stop()
second.stop()
},
{ flush: 'sync' },
),
)
new EffectScope(true, parent).run(() =>
effect(() => log.push(count.value)),
)
parent.pause()
count.value++
parent.resume()
expect(log).toEqual([0, 1])
})

it('stops resuming the scopes linked to a parent stopped on the way', () => {
const count = ref(0)
const log: number[] = []
const parent = effectScope()
new EffectScope(true, parent).run(() =>
watch(count, () => parent.stop(), { flush: 'sync' }),
)
new EffectScope(true, parent).run(() =>
effect(() => log.push(count.value)),
)
parent.pause()
count.value++
parent.resume()
expect(log).toEqual([0])
})
})
})

Expand Down
96 changes: 92 additions & 4 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,45 @@ export class EffectScope implements ReactiveNode {
*/
cleanupsLength = 0

constructor(detached = false) {
if (!detached && activeEffectScope) {
link(this, activeEffectScope)
if (activeEffectScope.flags & EffectFlags.PAUSED) {
/**
* The detached scopes given this one as their parent, which follow its
* pause until they stop. An attached scope is linked through `deps` instead.
* @internal
*/
detachedScopes: EffectScope | undefined = undefined
/**
* The previous scope in its parent's `detachedScopes`; for the first one,
* the parent itself.
* @internal
*/
prevDetached: EffectScope | undefined = undefined
/**
* @internal
*/
nextDetached: EffectScope | undefined = undefined

constructor(detached?: boolean)
/**
* @param parent - a detached scope given a parent still follows its pause,
* but is not stopped with it
* @internal
*/
constructor(detached: true, parent?: EffectScope)
constructor(detached = false, parent?: EffectScope) {
const scope = detached ? parent : activeEffectScope
if (scope) {
if (detached) {
const head = scope.detachedScopes
if (head !== undefined) {
head.prevDetached = this
this.nextDetached = head
}
this.prevDetached = scope
scope.detachedScopes = this
} else {
link(this, scope)
}
if (scope.flags & EffectFlags.PAUSED) {
this.flags = EffectFlags.INHERITED_PAUSED
}
}
Expand Down Expand Up @@ -74,6 +109,28 @@ export class EffectScope implements ReactiveNode {
if (sub !== undefined) {
unlink(sub)
}
const prev = this.prevDetached
if (prev !== undefined) {
const next = this.nextDetached
if (next !== undefined) {
next.prevDetached = prev
}
if (prev.detachedScopes === this) {
prev.detachedScopes = next
} else {
prev.nextDetached = next
}
this.prevDetached = this.nextDetached = undefined
}
// the scopes linked to this one keep their pause: they are expected to
// stop right after it
let child = this.detachedScopes
this.detachedScopes = undefined
while (child !== undefined) {
const next: EffectScope | undefined = child.nextDetached
child.prevDetached = child.nextDetached = undefined
child = next
}
}

/**
Expand Down Expand Up @@ -114,6 +171,13 @@ function pauseScope(scope: EffectScope, by: EffectFlags): void {
pauseScope(node as EffectScope, EffectFlags.INHERITED_PAUSED)
}
}
for (
let child = scope.detachedScopes;
child !== undefined;
child = child.nextDetached
) {
pauseScope(child, EffectFlags.INHERITED_PAUSED)
}
}

// Lifts one of the two pauses; the subtree resumes once neither is left. An
Expand Down Expand Up @@ -147,6 +211,30 @@ function resumeScope(scope: EffectScope, by: EffectFlags): void {
}
link = link.nextDep
}
// The list starts with the newest: walk back from the oldest, to resume them
// in creation order.
let child = oldestDetached(scope)
while (child !== scope) {
if (child.flags & EffectFlags.INHERITED_PAUSED) {
resumeScope(child, EffectFlags.INHERITED_PAUSED)
// a stopped parent leaves the rest of its scopes paused
if (scope.flags & (EffectFlags.PAUSED | EffectFlags.STOP)) return
if (child.flags & EffectFlags.STOP) {
child = oldestDetached(scope)
continue
}
}
child = child.prevDetached!
}
}

function oldestDetached(scope: EffectScope): EffectScope {
let child = scope.detachedScopes
if (child === undefined) return scope
while (child.nextDetached !== undefined) {
child = child.nextDetached
}
return child
}

/**
Expand Down
63 changes: 63 additions & 0 deletions packages/runtime-core/__tests__/component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {
type ComponentInternalInstance,
type EffectScope,
getCurrentInstance,
getCurrentScope,
h,
nextTick,
nodeOps,
ref,
render,
serializeInner,
watch,
} from '@vue/runtime-test'
import { type GenericComponent, formatComponentName } from '../src/component'

Expand Down Expand Up @@ -161,3 +167,60 @@ describe('formatComponentName', () => {
expect(formatComponentName(instance, Comp)).toBe('CompF')
})
})

describe('component scope pause', () => {
test('pausing a component scope pauses its descendants until it resumes', async () => {
const count = ref(0)
const log: string[] = []
let scope!: EffectScope
const Leaf = {
setup: () => () => (log.push('leaf'), h('i', count.value)),
}
const Parent = {
setup() {
scope = getCurrentScope()!
return () => h('div', [h(Leaf)])
},
}
const root = nodeOps.createElement('div')
render(h(Parent), root)
log.length = 0

scope.pause()
count.value++
await nextTick()
expect(log).toEqual([])
expect(serializeInner(root)).toBe('<div><i>0</i></div>')

scope.resume()
await nextTick()
expect(log).toEqual(['leaf'])
expect(serializeInner(root)).toBe('<div><i>1</i></div>')
})

test('a render queued before its scope pauses waits for the resume', async () => {
const count = ref(0)
const log: string[] = []
let scope!: EffectScope
const Comp = {
setup() {
scope = getCurrentScope()!
watch(count, () => log.push('watcher'))
return () => (log.push('render'), h('div', count.value))
},
}
const root = nodeOps.createElement('div')
render(h(Comp), root)
log.length = 0

count.value++
scope.pause()
await nextTick()
expect(log).toEqual([])

scope.resume()
await nextTick()
expect(log).toEqual(['watcher', 'render'])
expect(serializeInner(root)).toBe('<div>1</div>')
})
})
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 already be queued when its scope is paused
if (!(this.flags & EffectFlags.PAUSED) && this.dirty) {
this.run()
}
}
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 @@ -768,7 +768,7 @@ export function createComponentInstance(
effect: null!,
update: null!, // will be set synchronously right after creation
job: null!,
scope: new EffectScope(true /* detached */),
scope: new EffectScope(true, parent ? parent.scope : undefined),
render: null,
proxy: null,
exposed: null,
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,8 @@ function baseCreateRenderer(
setCurrentScope(prevScope)

this.job = instance.job = () => {
if (this.dirty) {
// the job may already be queued when its scope is paused
if (!(this.flags & EffectFlags.PAUSED) && this.dirty) {
this.run()
}
}
Expand Down
Loading
Loading