diff --git a/src/apis/watch.ts b/src/apis/watch.ts index 8fe0c5af..77efc70f 100644 --- a/src/apis/watch.ts +++ b/src/apis/watch.ts @@ -11,7 +11,11 @@ type SimpleEffect = (onCleanup: CleanupRegistrator) => void; type StopHandle = () => void; -type WatcherCallBack = (newVal: T, oldVal: T, onCleanup: CleanupRegistrator) => void; +type WatcherCallBack = ( + newVal: V, + oldVal: OV, + onCleanup: CleanupRegistrator +) => void; type WatcherSource = Ref | (() => T); @@ -19,10 +23,18 @@ type MapSources = { [K in keyof T]: T[K] extends WatcherSource ? V : never; }; +type MapOldSources = { + [K in keyof T]: T[K] extends WatcherSource + ? Lazy extends true + ? V + : (V | undefined) + : never; +}; + type FlushMode = 'pre' | 'post' | 'sync'; -interface WatcherOption { - lazy: boolean; // whether or not to delay callcack invoking +interface WatcherOption { + lazy: Lazy; // whether or not to delay callback invoking deep: boolean; flush: FlushMode; } @@ -221,11 +233,11 @@ function createWatcher( }; let callback = createScheduler(applyCb); if (!options.lazy) { - const originalCallbck = callback; + const originalCallback = callback; // `shiftCallback` is used to handle the first sync effect run. // The subsequent callbacks will redirect to `callback`. let shiftCallback = (n: any, o: any) => { - shiftCallback = originalCallbck; + shiftCallback = originalCallback; applyCb(n, o); }; callback = (n: any, o: any) => { @@ -259,16 +271,26 @@ export function watch( source: SimpleEffect, options?: Omit, 'lazy'> ): StopHandle; -export function watch( + +export function watch = false>( source: WatcherSource, - cb: WatcherCallBack, - options?: Partial + cb: WatcherCallBack, + options?: Partial> ): StopHandle; -export function watch[]>( + +export function watch< + T extends Readonly[]>, + Lazy extends Readonly = false +>( sources: T, - cb: (newValues: MapSources, oldValues: MapSources, onCleanup: CleanupRegistrator) => any, - options?: Partial + cb: ( + newValues: MapSources, + oldValues: MapOldSources, + onCleanup: CleanupRegistrator + ) => any, + options?: Partial> ): StopHandle; + export function watch( source: WatcherSource | WatcherSource[] | SimpleEffect, cb?: Partial | WatcherCallBack,