|
@@ -54,26 +54,26 @@ type MapOldSources<T, Immediate> = {
|
|
|
|
|
|
|
|
type InvalidateCbRegistrator = (cb: () => void) => void
|
|
type InvalidateCbRegistrator = (cb: () => void) => void
|
|
|
|
|
|
|
|
-export interface BaseWatchOptions {
|
|
|
|
|
|
|
+export interface WatchOptionsBase {
|
|
|
flush?: 'pre' | 'post' | 'sync'
|
|
flush?: 'pre' | 'post' | 'sync'
|
|
|
onTrack?: ReactiveEffectOptions['onTrack']
|
|
onTrack?: ReactiveEffectOptions['onTrack']
|
|
|
onTrigger?: ReactiveEffectOptions['onTrigger']
|
|
onTrigger?: ReactiveEffectOptions['onTrigger']
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export interface WatchOptions<Immediate = boolean> extends BaseWatchOptions {
|
|
|
|
|
|
|
+export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
|
|
|
immediate?: Immediate
|
|
immediate?: Immediate
|
|
|
deep?: boolean
|
|
deep?: boolean
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export type StopHandle = () => void
|
|
|
|
|
|
|
+export type WatchStopHandle = () => void
|
|
|
|
|
|
|
|
const invoke = (fn: Function) => fn()
|
|
const invoke = (fn: Function) => fn()
|
|
|
|
|
|
|
|
// Simple effect.
|
|
// Simple effect.
|
|
|
export function watchEffect(
|
|
export function watchEffect(
|
|
|
effect: WatchEffect,
|
|
effect: WatchEffect,
|
|
|
- options?: BaseWatchOptions
|
|
|
|
|
-): StopHandle {
|
|
|
|
|
|
|
+ options?: WatchOptionsBase
|
|
|
|
|
+): WatchStopHandle {
|
|
|
return doWatch(effect, null, options)
|
|
return doWatch(effect, null, options)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -85,7 +85,7 @@ export function watch<T, Immediate extends Readonly<boolean> = false>(
|
|
|
source: WatchSource<T>,
|
|
source: WatchSource<T>,
|
|
|
cb: WatchCallback<T, Immediate extends true ? (T | undefined) : T>,
|
|
cb: WatchCallback<T, Immediate extends true ? (T | undefined) : T>,
|
|
|
options?: WatchOptions<Immediate>
|
|
options?: WatchOptions<Immediate>
|
|
|
-): StopHandle
|
|
|
|
|
|
|
+): WatchStopHandle
|
|
|
|
|
|
|
|
// overload #2: array of multiple sources + cb
|
|
// overload #2: array of multiple sources + cb
|
|
|
// Readonly constraint helps the callback to correctly infer value types based
|
|
// Readonly constraint helps the callback to correctly infer value types based
|
|
@@ -98,14 +98,14 @@ export function watch<
|
|
|
sources: T,
|
|
sources: T,
|
|
|
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
|
|
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
|
|
|
options?: WatchOptions<Immediate>
|
|
options?: WatchOptions<Immediate>
|
|
|
-): StopHandle
|
|
|
|
|
|
|
+): WatchStopHandle
|
|
|
|
|
|
|
|
// implementation
|
|
// implementation
|
|
|
export function watch<T = any>(
|
|
export function watch<T = any>(
|
|
|
source: WatchSource<T> | WatchSource<T>[],
|
|
source: WatchSource<T> | WatchSource<T>[],
|
|
|
cb: WatchCallback<T>,
|
|
cb: WatchCallback<T>,
|
|
|
options?: WatchOptions
|
|
options?: WatchOptions
|
|
|
-): StopHandle {
|
|
|
|
|
|
|
+): WatchStopHandle {
|
|
|
if (__DEV__ && !isFunction(cb)) {
|
|
if (__DEV__ && !isFunction(cb)) {
|
|
|
warn(
|
|
warn(
|
|
|
`\`watch(fn, options?)\` signature has been moved to a separate API. ` +
|
|
`\`watch(fn, options?)\` signature has been moved to a separate API. ` +
|
|
@@ -120,7 +120,7 @@ function doWatch(
|
|
|
source: WatchSource | WatchSource[] | WatchEffect,
|
|
source: WatchSource | WatchSource[] | WatchEffect,
|
|
|
cb: WatchCallback | null,
|
|
cb: WatchCallback | null,
|
|
|
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
|
|
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
|
|
|
-): StopHandle {
|
|
|
|
|
|
|
+): WatchStopHandle {
|
|
|
if (__DEV__ && !cb) {
|
|
if (__DEV__ && !cb) {
|
|
|
if (immediate !== undefined) {
|
|
if (immediate !== undefined) {
|
|
|
warn(
|
|
warn(
|
|
@@ -274,7 +274,7 @@ export function instanceWatch(
|
|
|
source: string | Function,
|
|
source: string | Function,
|
|
|
cb: Function,
|
|
cb: Function,
|
|
|
options?: WatchOptions
|
|
options?: WatchOptions
|
|
|
-): StopHandle {
|
|
|
|
|
|
|
+): WatchStopHandle {
|
|
|
const publicThis = this.proxy as any
|
|
const publicThis = this.proxy as any
|
|
|
const getter = isString(source)
|
|
const getter = isString(source)
|
|
|
? () => publicThis[source]
|
|
? () => publicThis[source]
|