/
index.ts
41 lines (36 loc) · 1.61 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
39
40
41
import type { WatchCallback, WatchOptions, WatchSource, WatchStopHandle } from 'vue-demi'
import type { MapOldSources, MapSources, MaybeRef } from '../utils'
import { throttleFilter } from '../utils'
import { watchWithFilter } from '../watchWithFilter'
export interface WatchThrottledOptions<Immediate> extends WatchOptions<Immediate> {
throttle?: MaybeRef<number>
trailing?: boolean
leading?: boolean
}
// overloads
export function watchThrottled<T extends Readonly<WatchSource<unknown>[]>, Immediate extends Readonly<boolean> = false>(sources: [...T], cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, options?: WatchThrottledOptions<Immediate>): WatchStopHandle
export function watchThrottled<T, Immediate extends Readonly<boolean> = false>(source: WatchSource<T>, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchThrottledOptions<Immediate>): WatchStopHandle
export function watchThrottled<T extends object, Immediate extends Readonly<boolean> = false>(source: T, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchThrottledOptions<Immediate>): WatchStopHandle
// implementation
export function watchThrottled<Immediate extends Readonly<boolean> = false>(
source: any,
cb: any,
options: WatchThrottledOptions<Immediate> = {},
): WatchStopHandle {
const {
throttle = 0,
trailing = true,
leading = true,
...watchOptions
} = options
return watchWithFilter(
source,
cb,
{
...watchOptions,
eventFilter: throttleFilter(throttle, trailing, leading),
},
)
}
// alias
export { watchThrottled as throttledWatch }