@@ -79,11 +79,17 @@ export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
79
79
80
80
export type WatchStopHandle = ( ) => void
81
81
82
+ export interface WatchHandle extends WatchStopHandle {
83
+ pause : ( ) => void
84
+ resume : ( ) => void
85
+ stop : ( ) => void
86
+ }
87
+
82
88
// Simple effect.
83
89
export function watchEffect (
84
90
effect : WatchEffect ,
85
91
options ?: WatchOptionsBase ,
86
- ) : WatchStopHandle {
92
+ ) : WatchHandle {
87
93
return doWatch ( effect , null , options )
88
94
}
89
95
@@ -119,7 +125,7 @@ export function watch<T, Immediate extends Readonly<boolean> = false>(
119
125
source : WatchSource < T > ,
120
126
cb : WatchCallback < T , MaybeUndefined < T , Immediate > > ,
121
127
options ?: WatchOptions < Immediate > ,
122
- ) : WatchStopHandle
128
+ ) : WatchHandle
123
129
124
130
// overload: reactive array or tuple of multiple sources + cb
125
131
export function watch <
@@ -131,7 +137,7 @@ export function watch<
131
137
? WatchCallback < T , MaybeUndefined < T , Immediate > >
132
138
: WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
133
139
options ?: WatchOptions < Immediate > ,
134
- ) : WatchStopHandle
140
+ ) : WatchHandle
135
141
136
142
// overload: array of multiple sources + cb
137
143
export function watch <
@@ -141,7 +147,7 @@ export function watch<
141
147
sources : [ ...T ] ,
142
148
cb : WatchCallback < MapSources < T , false > , MapSources < T , Immediate > > ,
143
149
options ?: WatchOptions < Immediate > ,
144
- ) : WatchStopHandle
150
+ ) : WatchHandle
145
151
146
152
// overload: watching reactive object w/ cb
147
153
export function watch <
@@ -151,14 +157,14 @@ export function watch<
151
157
source : T ,
152
158
cb : WatchCallback < T , MaybeUndefined < T , Immediate > > ,
153
159
options ?: WatchOptions < Immediate > ,
154
- ) : WatchStopHandle
160
+ ) : WatchHandle
155
161
156
162
// implementation
157
163
export function watch < T = any , Immediate extends Readonly < boolean > = false > (
158
164
source : T | WatchSource < T > ,
159
165
cb : any ,
160
166
options ?: WatchOptions < Immediate > ,
161
- ) : WatchStopHandle {
167
+ ) : WatchHandle {
162
168
if ( __DEV__ && ! isFunction ( cb ) ) {
163
169
warn (
164
170
`\`watch(fn, options?)\` signature has been moved to a separate API. ` +
@@ -180,12 +186,12 @@ function doWatch(
180
186
onTrack,
181
187
onTrigger,
182
188
} : WatchOptions = EMPTY_OBJ ,
183
- ) : WatchStopHandle {
189
+ ) : WatchHandle {
184
190
if ( cb && once ) {
185
191
const _cb = cb
186
192
cb = ( ...args ) => {
187
193
_cb ( ...args )
188
- unwatch ( )
194
+ watchHandle ( )
189
195
}
190
196
}
191
197
@@ -327,7 +333,11 @@ function doWatch(
327
333
const ctx = useSSRContext ( ) !
328
334
ssrCleanup = ctx . __watcherHandles || ( ctx . __watcherHandles = [ ] )
329
335
} else {
330
- return NOOP
336
+ const watchHandle : WatchHandle = ( ) => { }
337
+ watchHandle . stop = NOOP
338
+ watchHandle . resume = NOOP
339
+ watchHandle . pause = NOOP
340
+ return watchHandle
331
341
}
332
342
}
333
343
@@ -397,13 +407,17 @@ function doWatch(
397
407
effect . scheduler = scheduler
398
408
399
409
const scope = getCurrentScope ( )
400
- const unwatch = ( ) => {
410
+ const watchHandle : WatchHandle = ( ) => {
401
411
effect . stop ( )
402
412
if ( scope ) {
403
413
remove ( scope . effects , effect )
404
414
}
405
415
}
406
416
417
+ watchHandle . pause = effect . pause . bind ( effect )
418
+ watchHandle . resume = effect . resume . bind ( effect )
419
+ watchHandle . stop = watchHandle
420
+
407
421
if ( __DEV__ ) {
408
422
effect . onTrack = onTrack
409
423
effect . onTrigger = onTrigger
@@ -425,8 +439,8 @@ function doWatch(
425
439
effect . run ( )
426
440
}
427
441
428
- if ( __SSR__ && ssrCleanup ) ssrCleanup . push ( unwatch )
429
- return unwatch
442
+ if ( __SSR__ && ssrCleanup ) ssrCleanup . push ( watchHandle )
443
+ return watchHandle
430
444
}
431
445
432
446
// this.$watch
@@ -435,7 +449,7 @@ export function instanceWatch(
435
449
source : string | Function ,
436
450
value : WatchCallback | ObjectWatchOptionItem ,
437
451
options ?: WatchOptions ,
438
- ) : WatchStopHandle {
452
+ ) : WatchHandle {
439
453
const publicThis = this . proxy as any
440
454
const getter = isString ( source )
441
455
? source . includes ( '.' )
0 commit comments