refactor: more readable type names for watch cleanup function

This commit is contained in:
Evan You 2022-01-12 22:07:47 +08:00
parent 3007d5b4ca
commit ce49fdf999

View File

@ -40,14 +40,14 @@ import { DeprecationTypes } from './compat/compatConfig'
import { checkCompatEnabled, isCompatEnabled } from './compat/compatConfig' import { checkCompatEnabled, isCompatEnabled } from './compat/compatConfig'
import { ObjectWatchOptionItem } from './componentOptions' import { ObjectWatchOptionItem } from './componentOptions'
export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void export type WatchEffect = (onCleanup: OnCleanup) => void
export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T) export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
export type WatchCallback<V = any, OV = any> = ( export type WatchCallback<V = any, OV = any> = (
value: V, value: V,
oldValue: OV, oldValue: OV,
onInvalidate: InvalidateCbRegistrator onCleanup: OnCleanup
) => any ) => any
type MapSources<T, Immediate> = { type MapSources<T, Immediate> = {
@ -62,7 +62,7 @@ type MapSources<T, Immediate> = {
: never : never
} }
type InvalidateCbRegistrator = (cb: () => void) => void type OnCleanup = (cleanupFn: () => void) => void
export interface WatchOptionsBase extends DebuggerOptions { export interface WatchOptionsBase extends DebuggerOptions {
flush?: 'pre' | 'post' | 'sync' flush?: 'pre' | 'post' | 'sync'
@ -242,7 +242,7 @@ function doWatch(
source, source,
instance, instance,
ErrorCodes.WATCH_CALLBACK, ErrorCodes.WATCH_CALLBACK,
[onInvalidate] [onCleanup]
) )
} }
} }
@ -272,7 +272,7 @@ function doWatch(
} }
let cleanup: () => void let cleanup: () => void
let onInvalidate: InvalidateCbRegistrator = (fn: () => void) => { let onCleanup: OnCleanup = (fn: () => void) => {
cleanup = effect.onStop = () => { cleanup = effect.onStop = () => {
callWithErrorHandling(fn, instance, ErrorCodes.WATCH_CLEANUP) callWithErrorHandling(fn, instance, ErrorCodes.WATCH_CLEANUP)
} }
@ -282,14 +282,14 @@ function doWatch(
// unless it's eager // unless it's eager
if (__SSR__ && isInSSRComponentSetup) { if (__SSR__ && isInSSRComponentSetup) {
// we will also not call the invalidate callback (+ runner is not set up) // we will also not call the invalidate callback (+ runner is not set up)
onInvalidate = NOOP onCleanup = NOOP
if (!cb) { if (!cb) {
getter() getter()
} else if (immediate) { } else if (immediate) {
callWithAsyncErrorHandling(cb, instance, ErrorCodes.WATCH_CALLBACK, [ callWithAsyncErrorHandling(cb, instance, ErrorCodes.WATCH_CALLBACK, [
getter(), getter(),
isMultiSource ? [] : undefined, isMultiSource ? [] : undefined,
onInvalidate onCleanup
]) ])
} }
return NOOP return NOOP
@ -323,7 +323,7 @@ function doWatch(
newValue, newValue,
// pass undefined as the old value when it's changed for the first time // pass undefined as the old value when it's changed for the first time
oldValue === INITIAL_WATCHER_VALUE ? undefined : oldValue, oldValue === INITIAL_WATCHER_VALUE ? undefined : oldValue,
onInvalidate onCleanup
]) ])
oldValue = newValue oldValue = newValue
} }