File tree 2 files changed +40
-22
lines changed
2 files changed +40
-22
lines changed Original file line number Diff line number Diff line change @@ -678,6 +678,28 @@ describe('reactivity/effect', () => {
678
678
expect ( dummy ) . toBe ( 3 )
679
679
} )
680
680
681
+ it ( 'stop with scheduler' , ( ) => {
682
+ let dummy
683
+ const obj = reactive ( { prop : 1 } )
684
+ const queue : ( ( ) => void ) [ ] = [ ]
685
+ const runner = effect (
686
+ ( ) => {
687
+ dummy = obj . prop
688
+ } ,
689
+ {
690
+ scheduler : e => queue . push ( e )
691
+ }
692
+ )
693
+ obj . prop = 2
694
+ expect ( dummy ) . toBe ( 1 )
695
+ expect ( queue . length ) . toBe ( 1 )
696
+ stop ( runner )
697
+
698
+ // a scheduled effect should not execute anymore after stopped
699
+ queue . forEach ( e => e ( ) )
700
+ expect ( dummy ) . toBe ( 1 )
701
+ } )
702
+
681
703
it ( 'events: onStop' , ( ) => {
682
704
const onStop = jest . fn ( )
683
705
const runner = effect ( ( ) => { } , {
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ type KeyToDepMap = Map<any, Dep>
10
10
const targetMap = new WeakMap < any , KeyToDepMap > ( )
11
11
12
12
export interface ReactiveEffect < T = any > {
13
- ( ) : T
13
+ ( ... args : any [ ] ) : T
14
14
_isEffect : true
15
15
active : boolean
16
16
raw : ( ) => T
@@ -75,11 +75,26 @@ export function stop(effect: ReactiveEffect) {
75
75
}
76
76
77
77
function createReactiveEffect < T = any > (
78
- fn : ( ) => T ,
78
+ fn : ( ... args : any [ ] ) => T ,
79
79
options : ReactiveEffectOptions
80
80
) : ReactiveEffect < T > {
81
81
const effect = function reactiveEffect ( ...args : unknown [ ] ) : unknown {
82
- return run ( effect , fn , args )
82
+ if ( ! effect . active ) {
83
+ return options . scheduler ? undefined : fn ( ...args )
84
+ }
85
+ if ( ! effectStack . includes ( effect ) ) {
86
+ cleanup ( effect )
87
+ try {
88
+ enableTracking ( )
89
+ effectStack . push ( effect )
90
+ activeEffect = effect
91
+ return fn ( ...args )
92
+ } finally {
93
+ effectStack . pop ( )
94
+ resetTracking ( )
95
+ activeEffect = effectStack [ effectStack . length - 1 ]
96
+ }
97
+ }
83
98
} as ReactiveEffect
84
99
effect . _isEffect = true
85
100
effect . active = true
@@ -89,25 +104,6 @@ function createReactiveEffect<T = any>(
89
104
return effect
90
105
}
91
106
92
- function run ( effect : ReactiveEffect , fn : Function , args : unknown [ ] ) : unknown {
93
- if ( ! effect . active ) {
94
- return fn ( ...args )
95
- }
96
- if ( ! effectStack . includes ( effect ) ) {
97
- cleanup ( effect )
98
- try {
99
- enableTracking ( )
100
- effectStack . push ( effect )
101
- activeEffect = effect
102
- return fn ( ...args )
103
- } finally {
104
- effectStack . pop ( )
105
- resetTracking ( )
106
- activeEffect = effectStack [ effectStack . length - 1 ]
107
- }
108
- }
109
- }
110
-
111
107
function cleanup ( effect : ReactiveEffect ) {
112
108
const { deps } = effect
113
109
if ( deps . length ) {
You can’t perform that action at this time.
0 commit comments