Skip to content

fix(types): ensure correct oldValue typing based on lazy option #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/apis/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@ type SimpleEffect = (onCleanup: CleanupRegistrator) => void;

type StopHandle = () => void;

type WatcherCallBack<T> = (newVal: T, oldVal: T, onCleanup: CleanupRegistrator) => void;
type WatcherCallBack<V = any, OV = any> = (
newVal: V,
oldVal: OV,
onCleanup: CleanupRegistrator
) => void;

type WatcherSource<T> = Ref<T> | (() => T);

type MapSources<T> = {
[K in keyof T]: T[K] extends WatcherSource<infer V> ? V : never;
};

type MapOldSources<T, Lazy> = {
[K in keyof T]: T[K] extends WatcherSource<infer V>
? 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 = boolean> {
lazy: Lazy; // whether or not to delay callback invoking
deep: boolean;
flush: FlushMode;
}
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -259,16 +271,26 @@ export function watch<T = any>(
source: SimpleEffect,
options?: Omit<Partial<WatcherOption>, 'lazy'>
): StopHandle;
export function watch<T = any>(

export function watch<T, Lazy extends Readonly<boolean> = false>(
source: WatcherSource<T>,
cb: WatcherCallBack<T>,
options?: Partial<WatcherOption>
cb: WatcherCallBack<T, Lazy extends true ? T : (T | undefined)>,
options?: Partial<WatcherOption<Lazy>>
): StopHandle;
export function watch<T extends WatcherSource<unknown>[]>(

export function watch<
T extends Readonly<WatcherSource<unknown>[]>,
Lazy extends Readonly<boolean> = false
>(
sources: T,
cb: (newValues: MapSources<T>, oldValues: MapSources<T>, onCleanup: CleanupRegistrator) => any,
options?: Partial<WatcherOption>
cb: (
newValues: MapSources<T>,
oldValues: MapOldSources<T, Lazy>,
onCleanup: CleanupRegistrator
) => any,
options?: Partial<WatcherOption<Lazy>>
): StopHandle;

export function watch(
source: WatcherSource<unknown> | WatcherSource<unknown>[] | SimpleEffect,
cb?: Partial<WatcherOption> | WatcherCallBack<any>,
Expand Down