@@ -96,11 +96,11 @@ export function getCurrentWatcher(): ReactiveEffect<any> | undefined {
96
96
export function onWatcherCleanup (
97
97
cleanupFn : ( ) => void ,
98
98
failSilently = false ,
99
+ owner : ReactiveEffect | undefined = activeWatcher ,
99
100
) : void {
100
- if ( activeWatcher ) {
101
- const cleanups =
102
- cleanupMap . get ( activeWatcher ) ||
103
- cleanupMap . set ( activeWatcher , [ ] ) . get ( activeWatcher ) !
101
+ if ( owner ) {
102
+ let cleanups = cleanupMap . get ( owner )
103
+ if ( ! cleanups ) cleanupMap . set ( owner , ( cleanups = [ ] ) )
104
104
cleanups . push ( cleanupFn )
105
105
} else if ( __DEV__ && ! failSilently ) {
106
106
warn (
@@ -137,6 +137,7 @@ export function watch(
137
137
}
138
138
139
139
let effect : ReactiveEffect
140
+ let boundCleanup : typeof onWatcherCleanup
140
141
let getter : ( ) => any
141
142
let cleanup : ( ( ) => void ) | undefined
142
143
let forceTrigger = false
@@ -184,8 +185,8 @@ export function watch(
184
185
activeWatcher = effect
185
186
try {
186
187
return call
187
- ? call ( source , WatchErrorCodes . WATCH_CALLBACK , [ onWatcherCleanup ] )
188
- : source ( onWatcherCleanup )
188
+ ? call ( source , WatchErrorCodes . WATCH_CALLBACK , [ boundCleanup ] )
189
+ : source ( boundCleanup )
189
190
} finally {
190
191
activeWatcher = currentEffect
191
192
}
@@ -254,7 +255,7 @@ export function watch(
254
255
: isMultiSource && oldValue [ 0 ] === INITIAL_WATCHER_VALUE
255
256
? [ ]
256
257
: oldValue ,
257
- onWatcherCleanup ,
258
+ boundCleanup ,
258
259
]
259
260
call
260
261
? call ( cb ! , WatchErrorCodes . WATCH_CALLBACK , args )
@@ -276,6 +277,7 @@ export function watch(
276
277
}
277
278
278
279
effect = new ReactiveEffect ( getter )
280
+ boundCleanup = fn => onWatcherCleanup ( fn , false , effect )
279
281
if ( scheduler ) {
280
282
effect . scheduler = ( ) => scheduler ( job , false )
281
283
} else {
0 commit comments