/
index.ts
38 lines (33 loc) · 1.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { WatchCallback, WatchOptions, WatchSource, WatchStopHandle } from 'vue-demi'
import type { DebounceFilterOptions, MapOldSources, MapSources, MaybeRef } from '../utils'
import { debounceFilter } from '../utils'
import { watchWithFilter } from '../watchWithFilter'
export interface WatchDebouncedOptions<Immediate> extends WatchOptions<Immediate>, DebounceFilterOptions {
debounce?: MaybeRef<number>
}
// overlads
export function watchDebounced<T extends Readonly<WatchSource<unknown>[]>, Immediate extends Readonly<boolean> = false>(sources: [...T], cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, options?: WatchDebouncedOptions<Immediate>): WatchStopHandle
export function watchDebounced<T, Immediate extends Readonly<boolean> = false>(source: WatchSource<T>, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchDebouncedOptions<Immediate>): WatchStopHandle
export function watchDebounced<T extends object, Immediate extends Readonly<boolean> = false>(source: T, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchDebouncedOptions<Immediate>): WatchStopHandle
// implementation
export function watchDebounced<Immediate extends Readonly<boolean> = false>(
source: any,
cb: any,
options: WatchDebouncedOptions<Immediate> = {},
): WatchStopHandle {
const {
debounce = 0,
maxWait = undefined,
...watchOptions
} = options
return watchWithFilter(
source,
cb,
{
...watchOptions,
eventFilter: debounceFilter(debounce, { maxWait }),
},
)
}
// alias
export { watchDebounced as debouncedWatch }