@@ -16,6 +16,7 @@ import { ReactiveFlags } from './constants'
1616import { DebuggerOptions , ReactiveEffect , EffectScheduler } from './effect'
1717import { isShallow , isReactive } from './reactive'
1818import { Ref , isRef } from './ref'
19+ import { getCurrentScope } from './effectScope'
1920
2021// contexts where user provided function may be executed, in addition to
2122// lifecycle hooks.
@@ -130,7 +131,9 @@ export function baseWatch(
130131 )
131132 }
132133
134+ let effect : ReactiveEffect
133135 let getter : ( ) => any
136+ let cleanup : ( ( ) => void ) | undefined
134137 let forceTrigger = false
135138 let isMultiSource = false
136139
@@ -200,10 +203,14 @@ export function baseWatch(
200203
201204 if ( once ) {
202205 if ( ! cb ) {
206+ // onEffectCleanup need use effect as a key
207+ getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
203208 getter ( )
204209 return NOOP
205210 }
206211 if ( immediate ) {
212+ // onEffectCleanup need use effect as a key
213+ getCurrentScope ( ) ?. effects . push ( ( effect = { } as any ) )
207214 callWithAsyncErrorHandling (
208215 cb ,
209216 handleError ,
@@ -280,9 +287,9 @@ export function baseWatch(
280287 isInit : false
281288 } )
282289
283- const effect = new ReactiveEffect ( getter , NOOP , effectScheduler )
290+ effect = new ReactiveEffect ( getter , NOOP , effectScheduler )
284291
285- const cleanup = ( effect . onStop = ( ) => {
292+ cleanup = effect . onStop = ( ) => {
286293 const cleanups = cleanupMap . get ( effect )
287294 if ( cleanups ) {
288295 cleanups . forEach ( cleanup =>
@@ -294,7 +301,7 @@ export function baseWatch(
294301 )
295302 cleanupMap . delete ( effect )
296303 }
297- } )
304+ }
298305
299306 const unwatch : WatchInstance = ( ) => {
300307 effect . stop ( )
0 commit comments