refactor(runtime-core): refactor watch typing (#2191)

This commit is contained in:
Pick 2020-10-14 03:45:17 +08:00 committed by GitHub
parent 408a8cad48
commit e4a5387435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,13 +44,7 @@ export type WatchCallback<V = any, OV = any> = (
onInvalidate: InvalidateCbRegistrator onInvalidate: InvalidateCbRegistrator
) => any ) => any
type MapSources<T> = { type MapSources<T, Immediate> = {
[K in keyof T]: T[K] extends WatchSource<infer V>
? V
: T[K] extends object ? T[K] : never
}
type MapOldSources<T, Immediate> = {
[K in keyof T]: T[K] extends WatchSource<infer V> [K in keyof T]: T[K] extends WatchSource<infer V>
? Immediate extends true ? (V | undefined) : V ? Immediate extends true ? (V | undefined) : V
: T[K] extends object : T[K] extends object
@ -93,7 +87,7 @@ export function watch<
Immediate extends Readonly<boolean> = false Immediate extends Readonly<boolean> = false
>( >(
sources: T, sources: T,
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
options?: WatchOptions<Immediate> options?: WatchOptions<Immediate>
): WatchStopHandle ): WatchStopHandle
@ -115,10 +109,10 @@ export function watch<
): WatchStopHandle ): WatchStopHandle
// implementation // implementation
export function watch<T = any>( export function watch<T = any, Immediate extends Readonly<boolean> = false>(
source: WatchSource<T> | WatchSource<T>[], source: T | WatchSource<T>,
cb: WatchCallback<T>, cb: any,
options?: WatchOptions options?: WatchOptions<Immediate>
): WatchStopHandle { ): WatchStopHandle {
if (__DEV__ && !isFunction(cb)) { if (__DEV__ && !isFunction(cb)) {
warn( warn(
@ -127,11 +121,11 @@ export function watch<T = any>(
`supports \`watch(source, cb, options?) signature.` `supports \`watch(source, cb, options?) signature.`
) )
} }
return doWatch(source, cb, options) return doWatch(source as any, cb, options)
} }
function doWatch( function doWatch(
source: WatchSource | WatchSource[] | WatchEffect, source: WatchSource | WatchSource[] | WatchEffect | object,
cb: WatchCallback | null, cb: WatchCallback | null,
{ immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ, { immediate, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ,
instance = currentInstance instance = currentInstance
@ -262,11 +256,11 @@ function doWatch(
} }
} }
// important: mark the job as a watcher callback so that scheduler knows it // important: mark the job as a watcher callback so that scheduler knows
// it is allowed to self-trigger (#1727) // it is allowed to self-trigger (#1727)
job.allowRecurse = !!cb job.allowRecurse = !!cb
let scheduler: (job: () => any) => void let scheduler: ReactiveEffectOptions['scheduler']
if (flush === 'sync') { if (flush === 'sync') {
scheduler = job scheduler = job
} else if (flush === 'post') { } else if (flush === 'post') {
@ -318,7 +312,7 @@ function doWatch(
export function instanceWatch( export function instanceWatch(
this: ComponentInternalInstance, this: ComponentInternalInstance,
source: string | Function, source: string | Function,
cb: Function, cb: WatchCallback,
options?: WatchOptions options?: WatchOptions
): WatchStopHandle { ): WatchStopHandle {
const publicThis = this.proxy as any const publicThis = this.proxy as any