]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: refactor watcher types naming
authorEvan You <yyx990803@gmail.com>
Mon, 30 Dec 2019 16:30:12 +0000 (11:30 -0500)
committerEvan You <yyx990803@gmail.com>
Mon, 30 Dec 2019 16:30:12 +0000 (11:30 -0500)
packages/runtime-core/src/apiOptions.ts
packages/runtime-core/src/apiWatch.ts

index 8af62b75595337913652e67519b507d3acf11647..b306943f855c178bbde55fb412176db993b5369a 100644 (file)
@@ -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)
     }
index 7f3016e9812c65ffb7cb32ed2479bb52fc5d4a02..0bcfbde147eaf628bc9c16176eb25b2a9cd0453b 100644 (file)
@@ -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