NOOP
} from '@vue/shared'
import { computed } from './apiReactivity'
-import { watch, WatchOptions, WatchHandler } from './apiWatch'
+import { watch, WatchOptions, WatchCallback } from './apiWatch'
import { provide, inject } from './apiInject'
import {
onBeforeMount,
type WatchOptionItem =
| string
- | WatchHandler
- | { handler: WatchHandler } & WatchOptions
+ | WatchCallback
+ | { handler: WatchCallback } & WatchOptions
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
if (isString(raw)) {
const handler = renderContext[raw]
if (isFunction(handler)) {
- watch(getter, handler as WatchHandler)
+ watch(getter, handler as WatchCallback)
} else if (__DEV__) {
warn(`Invalid watch handler specified by key "${raw}"`, handler)
}
import { queuePostRenderEffect } from './renderer'
import { warn } from './warning'
-export type WatchHandler<T = any> = (
+export type WatchEffect = (onCleanup: CleanupRegistrator) => void
+
+export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
+
+export type WatchCallback<T = any> = (
value: T,
oldValue: T,
onCleanup: CleanupRegistrator
) => any
+type MapSources<T> = {
+ [K in keyof T]: T[K] extends WatchSource<infer V> ? V : never
+}
+
+export type CleanupRegistrator = (invalidate: () => void) => void
+
export interface WatchOptions {
lazy?: boolean
flush?: 'pre' | 'post' | 'sync'
export type StopHandle = () => void
-export type WatcherSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
-
-type MapSources<T> = {
- [K in keyof T]: T[K] extends WatcherSource<infer V> ? V : never
-}
-
-export type CleanupRegistrator = (invalidate: () => void) => void
-
-export type SimpleEffect = (onCleanup: CleanupRegistrator) => void
-
const invoke = (fn: Function) => fn()
// overload #1: simple effect
-export function watch(effect: SimpleEffect, options?: WatchOptions): StopHandle
+export function watch(effect: WatchEffect, options?: WatchOptions): StopHandle
// overload #2: single source + cb
export function watch<T>(
- source: WatcherSource<T>,
- cb: WatchHandler<T>,
+ source: WatchSource<T>,
+ cb: WatchCallback<T>,
options?: WatchOptions
): StopHandle
// Readonly constraint helps the callback to correctly infer value types based
// on position in the source array. Otherwise the values will get a union type
// of all possible value types.
-export function watch<T extends Readonly<WatcherSource<unknown>[]>>(
+export function watch<T extends Readonly<WatchSource<unknown>[]>>(
sources: T,
- cb: WatchHandler<MapSources<T>>,
+ cb: WatchCallback<MapSources<T>>,
options?: WatchOptions
): StopHandle
// implementation
export function watch<T = any>(
- effectOrSource: WatcherSource<T> | WatcherSource<T>[] | SimpleEffect,
- cbOrOptions?: WatchHandler<T> | WatchOptions,
+ effectOrSource: WatchSource<T> | WatchSource<T>[] | WatchEffect,
+ cbOrOptions?: WatchCallback<T> | WatchOptions,
options?: WatchOptions
): StopHandle {
if (isFunction(cbOrOptions)) {
}
function doWatch(
- source: WatcherSource | WatcherSource[] | SimpleEffect,
- cb: WatchHandler | null,
+ source: WatchSource | WatchSource[] | WatchEffect,
+ cb: WatchCallback | null,
{ lazy, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
): StopHandle {
const instance = currentInstance