@@ -16,6 +16,7 @@ import { ReactiveFlags } from './constants'
16
16
import { DebuggerOptions , ReactiveEffect , EffectScheduler } from './effect'
17
17
import { isShallow , isReactive } from './reactive'
18
18
import { Ref , isRef } from './ref'
19
+ import { getCurrentScope } from './effectScope'
19
20
20
21
// contexts where user provided function may be executed, in addition to
21
22
// lifecycle hooks.
@@ -130,7 +131,9 @@ export function baseWatch(
130
131
)
131
132
}
132
133
134
+ let effect : ReactiveEffect
133
135
let getter : ( ) => any
136
+ let cleanup : ( ( ) => void ) | undefined
134
137
let forceTrigger = false
135
138
let isMultiSource = false
136
139
@@ -200,10 +203,14 @@ export function baseWatch(
200
203
201
204
if ( once ) {
202
205
if ( ! cb ) {
206
+ // onEffectCleanup need use effect as a key
207
+ getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
203
208
getter ( )
204
209
return NOOP
205
210
}
206
211
if ( immediate ) {
212
+ // onEffectCleanup need use effect as a key
213
+ getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
207
214
callWithAsyncErrorHandling (
208
215
cb ,
209
216
handleError ,
@@ -280,9 +287,9 @@ export function baseWatch(
280
287
isInit : false
281
288
} )
282
289
283
- const effect = new ReactiveEffect ( getter , NOOP , effectScheduler )
290
+ effect = new ReactiveEffect ( getter , NOOP , effectScheduler )
284
291
285
- const cleanup = ( effect . onStop = ( ) => {
292
+ cleanup = effect . onStop = ( ) => {
286
293
const cleanups = cleanupMap . get ( effect )
287
294
if ( cleanups ) {
288
295
cleanups . forEach ( cleanup =>
@@ -294,7 +301,7 @@ export function baseWatch(
294
301
)
295
302
cleanupMap . delete ( effect )
296
303
}
297
- } )
304
+ }
298
305
299
306
const unwatch : WatchInstance = ( ) => {
300
307
effect . stop ( )
0 commit comments