|
|
@@ -79,11 +79,17 @@ export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
|
|
|
|
|
|
export type WatchStopHandle = () => void
|
|
|
|
|
|
+export interface WatchHandle extends WatchStopHandle {
|
|
|
+ pause: () => void
|
|
|
+ resume: () => void
|
|
|
+ stop: () => void
|
|
|
+}
|
|
|
+
|
|
|
// Simple effect.
|
|
|
export function watchEffect(
|
|
|
effect: WatchEffect,
|
|
|
options?: WatchOptionsBase,
|
|
|
-): WatchStopHandle {
|
|
|
+): WatchHandle {
|
|
|
return doWatch(effect, null, options)
|
|
|
}
|
|
|
|
|
|
@@ -119,7 +125,7 @@ export function watch<T, Immediate extends Readonly<boolean> = false>(
|
|
|
source: WatchSource<T>,
|
|
|
cb: WatchCallback<T, MaybeUndefined<T, Immediate>>,
|
|
|
options?: WatchOptions<Immediate>,
|
|
|
-): WatchStopHandle
|
|
|
+): WatchHandle
|
|
|
|
|
|
// overload: reactive array or tuple of multiple sources + cb
|
|
|
export function watch<
|
|
|
@@ -131,7 +137,7 @@ export function watch<
|
|
|
? WatchCallback<T, MaybeUndefined<T, Immediate>>
|
|
|
: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
|
|
|
options?: WatchOptions<Immediate>,
|
|
|
-): WatchStopHandle
|
|
|
+): WatchHandle
|
|
|
|
|
|
// overload: array of multiple sources + cb
|
|
|
export function watch<
|
|
|
@@ -141,7 +147,7 @@ export function watch<
|
|
|
sources: [...T],
|
|
|
cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
|
|
|
options?: WatchOptions<Immediate>,
|
|
|
-): WatchStopHandle
|
|
|
+): WatchHandle
|
|
|
|
|
|
// overload: watching reactive object w/ cb
|
|
|
export function watch<
|
|
|
@@ -151,14 +157,14 @@ export function watch<
|
|
|
source: T,
|
|
|
cb: WatchCallback<T, MaybeUndefined<T, Immediate>>,
|
|
|
options?: WatchOptions<Immediate>,
|
|
|
-): WatchStopHandle
|
|
|
+): WatchHandle
|
|
|
|
|
|
// implementation
|
|
|
export function watch<T = any, Immediate extends Readonly<boolean> = false>(
|
|
|
source: T | WatchSource<T>,
|
|
|
cb: any,
|
|
|
options?: WatchOptions<Immediate>,
|
|
|
-): WatchStopHandle {
|
|
|
+): WatchHandle {
|
|
|
if (__DEV__ && !isFunction(cb)) {
|
|
|
warn(
|
|
|
`\`watch(fn, options?)\` signature has been moved to a separate API. ` +
|
|
|
@@ -180,12 +186,12 @@ function doWatch(
|
|
|
onTrack,
|
|
|
onTrigger,
|
|
|
}: WatchOptions = EMPTY_OBJ,
|
|
|
-): WatchStopHandle {
|
|
|
+): WatchHandle {
|
|
|
if (cb && once) {
|
|
|
const _cb = cb
|
|
|
cb = (...args) => {
|
|
|
_cb(...args)
|
|
|
- unwatch()
|
|
|
+ watchHandle()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -327,7 +333,11 @@ function doWatch(
|
|
|
const ctx = useSSRContext()!
|
|
|
ssrCleanup = ctx.__watcherHandles || (ctx.__watcherHandles = [])
|
|
|
} else {
|
|
|
- return NOOP
|
|
|
+ const watchHandle: WatchHandle = () => {}
|
|
|
+ watchHandle.stop = NOOP
|
|
|
+ watchHandle.resume = NOOP
|
|
|
+ watchHandle.pause = NOOP
|
|
|
+ return watchHandle
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -397,13 +407,17 @@ function doWatch(
|
|
|
effect.scheduler = scheduler
|
|
|
|
|
|
const scope = getCurrentScope()
|
|
|
- const unwatch = () => {
|
|
|
+ const watchHandle: WatchHandle = () => {
|
|
|
effect.stop()
|
|
|
if (scope) {
|
|
|
remove(scope.effects, effect)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ watchHandle.pause = effect.pause.bind(effect)
|
|
|
+ watchHandle.resume = effect.resume.bind(effect)
|
|
|
+ watchHandle.stop = watchHandle
|
|
|
+
|
|
|
if (__DEV__) {
|
|
|
effect.onTrack = onTrack
|
|
|
effect.onTrigger = onTrigger
|
|
|
@@ -425,8 +439,8 @@ function doWatch(
|
|
|
effect.run()
|
|
|
}
|
|
|
|
|
|
- if (__SSR__ && ssrCleanup) ssrCleanup.push(unwatch)
|
|
|
- return unwatch
|
|
|
+ if (__SSR__ && ssrCleanup) ssrCleanup.push(watchHandle)
|
|
|
+ return watchHandle
|
|
|
}
|
|
|
|
|
|
// this.$watch
|
|
|
@@ -435,7 +449,7 @@ export function instanceWatch(
|
|
|
source: string | Function,
|
|
|
value: WatchCallback | ObjectWatchOptionItem,
|
|
|
options?: WatchOptions,
|
|
|
-): WatchStopHandle {
|
|
|
+): WatchHandle {
|
|
|
const publicThis = this.proxy as any
|
|
|
const getter = isString(source)
|
|
|
? source.includes('.')
|