forked from vueuse/vueuse
/
index.ts
53 lines (48 loc) · 1.26 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
42
43
44
45
46
47
48
49
50
51
52
53
import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'
import type { UseTimeoutFnOptions } from '../useTimeoutFn'
import { useTimeoutFn } from '../useTimeoutFn'
import type { Fn, Stoppable } from '../utils'
import { noop } from '../utils'
export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutFnOptions {
/**
* Expose more controls
*
* @default false
*/
controls?: Controls
/**
* Callback on timeout
*/
callback?: Fn
}
/**
* Update value after a given time with controls.
*
* @see {@link https://vueuse.org/useTimeout}
* @param interval
* @param options
*/
export function useTimeout(interval?: number, options?: UseTimeoutOptions<false>): ComputedRef<boolean>
export function useTimeout(interval: number, options: UseTimeoutOptions<true>): { ready: ComputedRef<boolean> } & Stoppable
export function useTimeout(interval = 1000, options: UseTimeoutOptions<boolean> = {}) {
const {
controls: exposeControls = false,
callback,
} = options
const controls = useTimeoutFn(
callback ?? noop,
interval,
options,
)
const ready = computed(() => !controls.isPending.value)
if (exposeControls) {
return {
ready,
...controls,
}
}
else {
return ready
}
}