File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 1
1
import {
2
+ ref ,
2
3
reactive ,
3
4
effect ,
4
5
stop ,
@@ -801,6 +802,26 @@ describe('reactivity/effect', () => {
801
802
expect ( dummy ) . toBe ( 3 )
802
803
} )
803
804
805
+ // #5707
806
+ // when an effect completes its run, it should clear the tracking bits of
807
+ // its tracked deps. However, if the effect stops itself, the deps list is
808
+ // emptied so their bits are never cleared.
809
+ it ( 'edge case: self-stopping effect tracking ref' , ( ) => {
810
+ const c = ref ( true )
811
+ const runner = effect ( ( ) => {
812
+ // reference ref
813
+ if ( ! c . value ) {
814
+ // stop itself while running
815
+ stop ( runner )
816
+ }
817
+ } )
818
+ // trigger run
819
+ c . value = ! c . value
820
+ // should clear bits
821
+ expect ( ( c as any ) . dep . w ) . toBe ( 0 )
822
+ expect ( ( c as any ) . dep . n ) . toBe ( 0 )
823
+ } )
824
+
804
825
it ( 'events: onStop' , ( ) => {
805
826
const onStop = jest . fn ( )
806
827
const runner = effect ( ( ) => { } , {
Original file line number Diff line number Diff line change @@ -64,6 +64,10 @@ export class ReactiveEffect<T = any> {
64
64
* @internal
65
65
*/
66
66
allowRecurse ?: boolean
67
+ /**
68
+ * @internal
69
+ */
70
+ private deferStop ?: boolean
67
71
68
72
onStop ?: ( ) => void
69
73
// dev only
@@ -114,11 +118,18 @@ export class ReactiveEffect<T = any> {
114
118
activeEffect = this . parent
115
119
shouldTrack = lastShouldTrack
116
120
this . parent = undefined
121
+
122
+ if ( this . deferStop ) {
123
+ this . stop ( )
124
+ }
117
125
}
118
126
}
119
127
120
128
stop ( ) {
121
- if ( this . active ) {
129
+ // stopped while running itself - defer the cleanup
130
+ if ( activeEffect === this ) {
131
+ this . deferStop = true
132
+ } else if ( this . active ) {
122
133
cleanupEffect ( this )
123
134
if ( this . onStop ) {
124
135
this . onStop ( )
You can’t perform that action at this time.
0 commit comments