Skip to content

Commit 69add9c

Browse files
committed
feat(reactivity): nested pause and resume for effect scopes
1 parent 329d788 commit 69add9c

4 files changed

Lines changed: 263 additions & 32 deletions

File tree

‎packages/reactivity/__tests__/effectScope.spec.ts‎

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,183 @@ describe('reactivity/effect/scope', () => {
625625
expect(scope.run(() => c.value)).toBe(value)
626626
expect(getter).toHaveBeenCalledTimes(1)
627627
})
628+
629+
describe('pause inheritance', () => {
630+
it('starts an effect created in a paused scope paused', () => {
631+
const count = ref(0)
632+
const log: number[] = []
633+
const scope = effectScope()
634+
scope.pause()
635+
scope.run(() => effect(() => log.push(count.value)))
636+
expect(log).toEqual([0])
637+
638+
count.value++
639+
expect(log).toEqual([0])
640+
641+
scope.resume()
642+
expect(log).toEqual([0, 1])
643+
})
644+
645+
it('starts a scope created in a paused scope paused', () => {
646+
const count = ref(0)
647+
const log: number[] = []
648+
const scope = effectScope()
649+
scope.pause()
650+
scope.run(() => {
651+
effectScope().run(() => effect(() => log.push(count.value)))
652+
})
653+
count.value++
654+
expect(log).toEqual([0])
655+
656+
scope.resume()
657+
expect(log).toEqual([0, 1])
658+
})
659+
660+
it('keeps a child paused by its parent paused when the child resumes', () => {
661+
const count = ref(0)
662+
const log: number[] = []
663+
const parent = effectScope()
664+
const child = parent.run(() => effectScope())!
665+
child.run(() => effect(() => log.push(count.value)))
666+
parent.pause()
667+
child.resume()
668+
count.value++
669+
expect(log).toEqual([0])
670+
671+
parent.resume()
672+
expect(log).toEqual([0, 1])
673+
})
674+
675+
it('keeps a child paused on its own paused when the parent resumes', () => {
676+
const count = ref(0)
677+
const log: number[] = []
678+
const parent = effectScope()
679+
const child = parent.run(() => effectScope())!
680+
child.run(() => effect(() => log.push(count.value)))
681+
child.pause()
682+
parent.pause()
683+
parent.resume()
684+
count.value++
685+
expect(log).toEqual([0])
686+
687+
child.resume()
688+
expect(log).toEqual([0, 1])
689+
})
690+
691+
it('keeps a watcher paused through its handle paused when the scope resumes', () => {
692+
const count = ref(0)
693+
const log: number[] = []
694+
const scope = effectScope()
695+
const handle = scope.run(() =>
696+
watch(count, v => log.push(v), { flush: 'sync' }),
697+
)!
698+
handle.pause()
699+
scope.pause()
700+
scope.resume()
701+
count.value++
702+
expect(log).toEqual([])
703+
704+
handle.resume()
705+
expect(log).toEqual([1])
706+
})
707+
708+
it('honours a pause made by an effect that resume runs', () => {
709+
const count = ref(0)
710+
const log: string[] = []
711+
const parent = effectScope()
712+
parent.run(() => {
713+
effectScope().run(() =>
714+
effect(() => {
715+
if (count.value === 1) parent.pause()
716+
}),
717+
)
718+
effectScope().run(() => effect(() => log.push('second ' + count.value)))
719+
})
720+
parent.pause()
721+
count.value++
722+
parent.resume()
723+
expect(log).toEqual(['second 0'])
724+
725+
parent.resume()
726+
expect(log).toEqual(['second 0', 'second 1'])
727+
})
728+
729+
it('does not notify an effect that an earlier one already ran on resume', () => {
730+
const value = ref(0)
731+
const log: string[] = []
732+
const scope = effectScope()
733+
let second: any
734+
scope.run(() => {
735+
watch(
736+
value,
737+
v => {
738+
log.push(`first:${v}`)
739+
second()
740+
},
741+
{ flush: 'sync' },
742+
)
743+
second = effect(() => log.push(`second:${value.value}`), {
744+
scheduler: () => {
745+
log.push('schedule second')
746+
second()
747+
},
748+
})
749+
})
750+
log.length = 0
751+
scope.pause()
752+
value.value = 1
753+
scope.resume()
754+
expect(log).toEqual(['first:1', 'second:1'])
755+
})
756+
757+
it('keeps an effect paused until the resume reaches it', () => {
758+
const x = ref(0)
759+
const y = ref(0)
760+
const log: string[] = []
761+
const scope = effectScope()
762+
scope.run(() => {
763+
watch(
764+
x,
765+
() => {
766+
log.push('a:start')
767+
y.value = 1
768+
y.value = 2
769+
log.push('a:end')
770+
},
771+
{ flush: 'sync' },
772+
)
773+
watch(y, v => log.push(`b:${v}`), { flush: 'sync' })
774+
})
775+
scope.pause()
776+
x.value = 1
777+
scope.resume()
778+
expect(log).toEqual(['a:start', 'a:end', 'b:2'])
779+
})
780+
781+
it('resumes each effect once when one stops itself and the next', () => {
782+
const count = ref(0)
783+
const log: string[] = []
784+
const scope = effectScope()
785+
let stopNext: () => void
786+
scope.run(() => {
787+
effect(() => count.value, { scheduler: () => log.push('queued') })
788+
const stop = watch(
789+
count,
790+
() => {
791+
stop()
792+
stopNext()
793+
},
794+
{ flush: 'sync' },
795+
)
796+
stopNext = watch(count, () => log.push('next'), { flush: 'sync' })
797+
effect(() => log.push(`last ${count.value}`))
798+
})
799+
scope.pause()
800+
count.value++
801+
scope.resume()
802+
expect(log).toEqual(['last 0', 'queued', 'last 1'])
803+
})
804+
})
628805
})
629806

630807
function getEffectsCount(scope: EffectScope): number {

‎packages/reactivity/src/effect.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,18 @@ export enum EffectFlags {
5050
* ReactiveEffect only
5151
*/
5252
ALLOW_RECURSE = 1 << 7,
53-
PAUSED = 1 << 8,
53+
/**
54+
* paused by `pause()`
55+
* @internal
56+
*/
57+
SELF_PAUSED = 1 << 8,
5458
STOP = 1 << 10,
59+
/**
60+
* paused by an ancestor scope
61+
* @internal
62+
*/
63+
INHERITED_PAUSED = 1 << 11,
64+
PAUSED = EffectFlags.SELF_PAUSED | EffectFlags.INHERITED_PAUSED,
5565
}
5666

5767
export class ReactiveEffect<T = any>
@@ -87,6 +97,9 @@ export class ReactiveEffect<T = any>
8797
}
8898
if (activeEffectScope) {
8999
link(this, activeEffectScope)
100+
if (activeEffectScope.flags & EffectFlags.PAUSED) {
101+
this.flags |= EffectFlags.INHERITED_PAUSED
102+
}
90103
}
91104
}
92105

@@ -100,12 +113,15 @@ export class ReactiveEffect<T = any>
100113
}
101114

102115
pause(): void {
103-
this.flags |= EffectFlags.PAUSED
116+
this.flags |= EffectFlags.SELF_PAUSED
104117
}
105118

106119
resume(): void {
107-
const flags = (this.flags &= ~EffectFlags.PAUSED)
108-
if (flags & (ReactiveFlags.Dirty | ReactiveFlags.Pending)) {
120+
const flags = (this.flags &= ~EffectFlags.SELF_PAUSED)
121+
if (
122+
!(flags & EffectFlags.PAUSED) &&
123+
flags & (ReactiveFlags.Dirty | ReactiveFlags.Pending)
124+
) {
109125
this.notify()
110126
}
111127
}

‎packages/reactivity/src/effectScope.ts‎

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { EffectFlags, cleanup } from './effect'
2-
import { type Link, type ReactiveNode, link, unlink } from './system'
1+
import { EffectFlags, type ReactiveEffect, cleanup } from './effect'
2+
import {
3+
type Link,
4+
ReactiveFlags,
5+
type ReactiveNode,
6+
link,
7+
unlink,
8+
} from './system'
39
import { warn } from './warning'
410

511
export let activeEffectScope: EffectScope | undefined
@@ -24,6 +30,9 @@ export class EffectScope implements ReactiveNode {
2430
constructor(detached = false) {
2531
if (!detached && activeEffectScope) {
2632
link(this, activeEffectScope)
33+
if (activeEffectScope.flags & EffectFlags.PAUSED) {
34+
this.flags = EffectFlags.INHERITED_PAUSED
35+
}
2736
}
2837
}
2938

@@ -32,35 +41,16 @@ export class EffectScope implements ReactiveNode {
3241
}
3342

3443
pause(): void {
35-
if (!(this.flags & EffectFlags.PAUSED)) {
36-
this.flags |= EffectFlags.PAUSED
37-
for (let link = this.deps; link !== undefined; link = link.nextDep) {
38-
const dep = link.dep
39-
if ('pause' in dep) {
40-
dep.pause()
41-
}
42-
}
43-
}
44+
pauseScope(this, EffectFlags.SELF_PAUSED)
4445
}
4546

4647
/**
47-
* Resumes the effect scope, including all child scopes and effects.
48+
* Resumes the effect scope, including all child scopes and effects, unless
49+
* an ancestor scope keeps it paused.
4850
*/
4951
resume(): void {
50-
const flags = this.flags
51-
if (flags & EffectFlags.PAUSED) {
52-
this.flags = flags & ~EffectFlags.PAUSED
53-
// from a snapshot: a resumed effect may run and unlink any dep
54-
const deps: Link['dep'][] = []
55-
for (let link = this.deps; link !== undefined; link = link.nextDep) {
56-
deps.push(link.dep)
57-
}
58-
for (let i = 0; i < deps.length; i++) {
59-
const dep = deps[i]
60-
if ('resume' in dep) {
61-
dep.resume()
62-
}
63-
}
52+
if (this.flags & EffectFlags.SELF_PAUSED) {
53+
resumeScope(this, EffectFlags.SELF_PAUSED)
6454
}
6555
}
6656

@@ -109,6 +99,56 @@ export class EffectScope implements ReactiveNode {
10999
}
110100
}
111101

102+
function pauseScope(scope: EffectScope, by: EffectFlags): void {
103+
const wasPaused = scope.flags & EffectFlags.PAUSED
104+
scope.flags |= by
105+
if (wasPaused) return
106+
for (let link = scope.deps; link !== undefined; link = link.nextDep) {
107+
const node = link.dep
108+
const flags = node.flags
109+
if (flags & ReactiveFlags.Watching) {
110+
// an effect
111+
node.flags = flags | EffectFlags.INHERITED_PAUSED
112+
} else if (!(flags & (ReactiveFlags.Mutable | EffectFlags.STOP))) {
113+
// neither a computed, which has no pause state, nor stopped: a scope
114+
pauseScope(node as EffectScope, EffectFlags.INHERITED_PAUSED)
115+
}
116+
}
117+
}
118+
119+
// Lifts one of the two pauses; the subtree resumes once neither is left. An
120+
// effect run on the way may pause or stop any part of the subtree again.
121+
function resumeScope(scope: EffectScope, by: EffectFlags): void {
122+
if ((scope.flags &= ~by) & EffectFlags.PAUSED) return
123+
let link = scope.deps
124+
while (link !== undefined) {
125+
const node = link.dep
126+
let flags = node.flags
127+
// only the nodes this resume has yet to reach carry the inherited pause
128+
if (flags & EffectFlags.INHERITED_PAUSED) {
129+
if (flags & ReactiveFlags.Watching) {
130+
node.flags = flags &= ~EffectFlags.INHERITED_PAUSED
131+
if (
132+
!(flags & EffectFlags.PAUSED) &&
133+
flags & (ReactiveFlags.Dirty | ReactiveFlags.Pending)
134+
) {
135+
;(node as ReactiveEffect).notify()
136+
}
137+
} else {
138+
resumeScope(node as EffectScope, EffectFlags.INHERITED_PAUSED)
139+
}
140+
if (scope.flags & EffectFlags.PAUSED) return
141+
// a node stopped on the way has left the list, and may have taken its
142+
// neighbours with it: start over
143+
if (node.flags & EffectFlags.STOP) {
144+
link = scope.deps
145+
continue
146+
}
147+
}
148+
link = link.nextDep
149+
}
150+
}
151+
112152
/**
113153
* Creates an effect scope object which can capture the reactive effects (i.e.
114154
* computed and watchers) created within it so that these effects can be

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
} from '@vue/runtime-dom'
3535
import {
3636
Dep,
37-
EffectFlags,
3837
ReactiveFlags,
3938
activeSub,
4039
computed,
@@ -684,7 +683,6 @@ export function initInputs(
684683
instance.hasDynamicProps = false
685684
}
686685
} else {
687-
if (scope.flags & EffectFlags.PAUSED) effect.pause()
688686
effect.notify()
689687
}
690688
} finally {

0 commit comments

Comments
 (0)