types: massive refactor

This commit is contained in:
Evan You
2019-10-22 11:26:48 -04:00
parent 522beaa766
commit b5886189ba
21 changed files with 308 additions and 258 deletions

View File

@@ -21,7 +21,12 @@ import {
} from './errorHandling'
import { onBeforeUnmount } from './apiLifecycle'
import { queuePostRenderEffect } from './createRenderer'
import { WatchHandler } from './apiOptions'
export type WatchHandler<T = any> = (
value: T,
oldValue: T,
onCleanup: CleanupRegistrator
) => any
export interface WatchOptions {
lazy?: boolean
@@ -58,11 +63,7 @@ export function watch<T>(
// overload #3: array of multiple sources + cb
export function watch<T extends readonly WatcherSource<unknown>[]>(
sources: T,
cb: (
newValues: MapSources<T>,
oldValues: MapSources<T>,
onCleanup: CleanupRegistrator
) => any,
cb: WatchHandler<MapSources<T>>,
options?: WatchOptions
): StopHandle
@@ -84,9 +85,7 @@ export function watch<T = any>(
function doWatch(
source: WatcherSource | WatcherSource[] | SimpleEffect,
cb:
| ((newValue: any, oldValue: any, onCleanup: CleanupRegistrator) => any)
| null,
cb: WatchHandler | null,
{ lazy, deep, flush, onTrack, onTrigger }: WatchOptions = EMPTY_OBJ
): StopHandle {
const instance = currentInstance
@@ -95,11 +94,10 @@ function doWatch(
let getter: () => any
if (isArray(source)) {
getter = () =>
source.map(
s =>
isRef(s)
? s.value
: callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
source.map(s =>
isRef(s)
? s.value
: callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
)
} else if (isRef(source)) {
getter = () => source.value
@@ -218,7 +216,7 @@ export function instanceWatch(
return stop
}
function traverse(value: any, seen: Set<any> = new Set()) {
function traverse(value: unknown, seen: Set<unknown> = new Set()) {
if (!isObject(value) || seen.has(value)) {
return
}