types: refactor watcher types naming
This commit is contained in:
parent
1cf1ad5e94
commit
bfb0ad5a5e
@ -16,7 +16,7 @@ import {
|
||||
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,
|
||||
@ -133,8 +133,8 @@ export type ExtractComputedReturns<T extends any> = {
|
||||
|
||||
type WatchOptionItem =
|
||||
| string
|
||||
| WatchHandler
|
||||
| { handler: WatchHandler } & WatchOptions
|
||||
| WatchCallback
|
||||
| { handler: WatchCallback } & WatchOptions
|
||||
|
||||
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
|
||||
|
||||
@ -463,7 +463,7 @@ function createWatcher(
|
||||
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)
|
||||
}
|
||||
|
@ -31,12 +31,22 @@ import { onBeforeUnmount } from './apiLifecycle'
|
||||
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'
|
||||
@ -47,25 +57,15 @@ export interface WatchOptions {
|
||||
|
||||
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
|
||||
|
||||
@ -73,16 +73,16 @@ export function watch<T>(
|
||||
// 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)) {
|
||||
@ -96,8 +96,8 @@ export function watch<T = any>(
|
||||
}
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user