types: refactor watcher types naming

This commit is contained in:
Evan You 2019-12-30 11:30:12 -05:00
parent 1cf1ad5e94
commit bfb0ad5a5e
2 changed files with 24 additions and 24 deletions

View File

@ -16,7 +16,7 @@ import {
NOOP NOOP
} from '@vue/shared' } from '@vue/shared'
import { computed } from './apiReactivity' import { computed } from './apiReactivity'
import { watch, WatchOptions, WatchHandler } from './apiWatch' import { watch, WatchOptions, WatchCallback } from './apiWatch'
import { provide, inject } from './apiInject' import { provide, inject } from './apiInject'
import { import {
onBeforeMount, onBeforeMount,
@ -133,8 +133,8 @@ export type ExtractComputedReturns<T extends any> = {
type WatchOptionItem = type WatchOptionItem =
| string | string
| WatchHandler | WatchCallback
| { handler: WatchHandler } & WatchOptions | { handler: WatchCallback } & WatchOptions
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[] type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
@ -463,7 +463,7 @@ function createWatcher(
if (isString(raw)) { if (isString(raw)) {
const handler = renderContext[raw] const handler = renderContext[raw]
if (isFunction(handler)) { if (isFunction(handler)) {
watch(getter, handler as WatchHandler) watch(getter, handler as WatchCallback)
} else if (__DEV__) { } else if (__DEV__) {
warn(`Invalid watch handler specified by key "${raw}"`, handler) warn(`Invalid watch handler specified by key "${raw}"`, handler)
} }

View File

@ -31,12 +31,22 @@ import { onBeforeUnmount } from './apiLifecycle'
import { queuePostRenderEffect } from './renderer' import { queuePostRenderEffect } from './renderer'
import { warn } from './warning' 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, value: T,
oldValue: T, oldValue: T,
onCleanup: CleanupRegistrator onCleanup: CleanupRegistrator
) => any ) => 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 { export interface WatchOptions {
lazy?: boolean lazy?: boolean
flush?: 'pre' | 'post' | 'sync' flush?: 'pre' | 'post' | 'sync'
@ -47,25 +57,15 @@ export interface WatchOptions {
export type StopHandle = () => void 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() const invoke = (fn: Function) => fn()
// overload #1: simple effect // 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 // overload #2: single source + cb
export function watch<T>( export function watch<T>(
source: WatcherSource<T>, source: WatchSource<T>,
cb: WatchHandler<T>, cb: WatchCallback<T>,
options?: WatchOptions options?: WatchOptions
): StopHandle ): StopHandle
@ -73,16 +73,16 @@ export function watch<T>(
// Readonly constraint helps the callback to correctly infer value types based // 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 // on position in the source array. Otherwise the values will get a union type
// of all possible value types. // of all possible value types.
export function watch<T extends Readonly<WatcherSource<unknown>[]>>( export function watch<T extends Readonly<WatchSource<unknown>[]>>(
sources: T, sources: T,
cb: WatchHandler<MapSources<T>>, cb: WatchCallback<MapSources<T>>,
options?: WatchOptions options?: WatchOptions
): StopHandle ): StopHandle
// implementation // implementation
export function watch<T = any>( export function watch<T = any>(
effectOrSource: WatcherSource<T> | WatcherSource<T>[] | SimpleEffect, effectOrSource: WatchSource<T> | WatchSource<T>[] | WatchEffect,
cbOrOptions?: WatchHandler<T> | WatchOptions, cbOrOptions?: WatchCallback<T> | WatchOptions,
options?: WatchOptions options?: WatchOptions
): StopHandle { ): StopHandle {
if (isFunction(cbOrOptions)) { if (isFunction(cbOrOptions)) {
@ -96,8 +96,8 @@ export function watch<T = any>(
} }
function doWatch( function doWatch(
source: WatcherSource | WatcherSource[] | SimpleEffect, source: WatchSource | WatchSource[] | WatchEffect,
cb: WatchHandler | null, cb: WatchCallback | null,
{ lazy, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ { lazy, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
): StopHandle { ): StopHandle {
const instance = currentInstance const instance = currentInstance