forked from vueuse/vueuse
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
48 lines (43 loc) · 1.18 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
import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'
import type { UseTimeoutFnOptions } from '../useTimeoutFn'
import { useTimeoutFn } from '../useTimeoutFn'
import type { Stoppable } from '../utils'
import { noop } from '../utils'
export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutFnOptions {
/**
* Expose more controls
*
* @default false
*/
controls?: Controls
}
/**
* Update value after a given time with controls.
*
* @see {@link https://vueuse.org/useTimeout}
* @param interval
* @param immediate
*/
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,
} = options
const controls = useTimeoutFn(
noop,
interval,
options,
)
const ready = computed(() => !controls.isPending.value)
if (exposeControls) {
return {
ready,
...controls,
}
}
else {
return ready
}
}