forked from vueuse/vueuse
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
72 lines (62 loc) · 1.6 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { Pausable } from '@vueuse/shared'
import { timestamp, useIntervalFn } from '@vueuse/shared'
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import { useRafFn } from '../useRafFn'
export interface UseTimestampOptions<Controls extends boolean> {
/**
* Expose more controls
*
* @default false
*/
controls?: Controls
/**
* Offset value adding to the value
*
* @default 0
*/
offset?: number
/**
* Update the timestamp immediately
*
* @default true
*/
immediate?: boolean
/**
* Update interval, or use requestAnimationFrame
*
* @default requestAnimationFrame
*/
interval?: 'requestAnimationFrame' | number
}
/**
* Reactive current timestamp.
*
* @see https://vueuse.org/useTimestamp
* @param options
*/
export function useTimestamp(options?: UseTimestampOptions<false>): Ref<number>
export function useTimestamp(options: UseTimestampOptions<true>): { timestamp: Ref<number> } & Pausable
export function useTimestamp(options: UseTimestampOptions<boolean> = {}) {
const {
controls: exposeControls = false,
offset = 0,
immediate = true,
interval = 'requestAnimationFrame',
} = options
const ts = ref(timestamp() + offset)
const update = () => ts.value = timestamp() + offset
const controls: Pausable = interval === 'requestAnimationFrame'
? useRafFn(update, { immediate })
: useIntervalFn(update, interval, { immediate })
if (exposeControls) {
return {
timestamp: ts,
...controls,
}
}
else {
return ts
}
}
export type UseTimestampReturn = ReturnType<typeof useTimestamp>