Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(watch): watchImmediate and watchDeep support overloads #2998

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/shared/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export interface ConfigurableFlushSync {
}

// Internal Types
export type MultiWatchSources = (WatchSource<unknown> | object)[]

export type MapSources<T> = {
[K in keyof T]: T[K] extends WatchSource<infer V> ? V : never;
}
Expand Down
33 changes: 30 additions & 3 deletions packages/shared/watchDeep/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import type { WatchCallback, WatchOptions, WatchSource } from 'vue-demi'
import type { WatchCallback, WatchOptions, WatchSource, WatchStopHandle } from 'vue-demi'
import { watch } from 'vue-demi'

import type { MapOldSources, MapSources, MultiWatchSources } from '../utils/types'

// overloads
export function watchDeep<
T extends Readonly<MultiWatchSources>,
Immediate extends Readonly<boolean> = false,
>(
source: T,
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
options?: Omit<WatchOptions<Immediate>, 'deep'>
): WatchStopHandle

export function watchDeep<T, Immediate extends Readonly<boolean> = false>(
source: WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: Omit<WatchOptions<Immediate>, 'deep'>
): WatchStopHandle

export function watchDeep<
T extends object,
Immediate extends Readonly<boolean> = false,
>(
source: T,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: Omit<WatchOptions<Immediate>, 'deep'>
): WatchStopHandle

/**
* Shorthand for watching value with {deep: true}
*
* @see https://vueuse.org/watchDeep
*/
export function watchDeep<T>(source: WatchSource<T>, cb: WatchCallback<T>, options?: Omit<WatchOptions, 'deep'>) {
export function watchDeep<T = any, Immediate extends Readonly<boolean> = false>(source: T | WatchSource<T>, cb: any, options?: Omit<WatchOptions<Immediate>, 'deep'>) {
return watch(
source,
source as any,
cb,
{
...options,
Expand Down
27 changes: 24 additions & 3 deletions packages/shared/watchImmediate/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import type { WatchCallback, WatchOptions, WatchSource } from 'vue-demi'
import type { WatchCallback, WatchOptions, WatchSource, WatchStopHandle } from 'vue-demi'
import { watch } from 'vue-demi'

import type { MapOldSources, MapSources, MultiWatchSources } from '../utils/types'

// overloads
export function watchImmediate<T extends Readonly<MultiWatchSources>>(
source: T,
cb: WatchCallback<MapSources<T>, MapOldSources<T, true>>,
options?: Omit<WatchOptions<true>, 'deep'>
): WatchStopHandle

export function watchImmediate<T>(
source: WatchSource<T>,
cb: WatchCallback<T, T | undefined>,
options?: Omit<WatchOptions<true>, 'deep'>
): WatchStopHandle

export function watchImmediate<T extends object>(
source: T,
cb: WatchCallback<T, T | undefined>,
options?: Omit<WatchOptions<true>, 'deep'>
): WatchStopHandle

/**
* Shorthand for watching value with {immediate: true}
*
* @see https://vueuse.org/watchImmediate
*/
export function watchImmediate<T>(source: WatchSource<T>, cb: WatchCallback<T>, options?: Omit<WatchOptions, 'immediate'>) {
export function watchImmediate<T = any>(source: T, cb: any, options?: Omit<WatchOptions, 'immediate'>) {
return watch(
source,
source as any,
cb,
{
...options,
Expand Down