File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,19 @@ describe('useNow', () => {
9
9
expect ( + now . value ) . toBeLessThanOrEqual ( + new Date ( ) )
10
10
} )
11
11
12
+ it ( 'starts lazily if immediate is false' , ( ) => {
13
+ const initial = + new Date ( )
14
+ const { now, resume } = useNow ( { controls : true , immediate : false } )
15
+
16
+ expect ( + now . value ) . toBe ( initial )
17
+ vi . advanceTimersByTime ( 50 )
18
+ expect ( + now . value ) . toBe ( initial )
19
+
20
+ resume ( )
21
+ vi . advanceTimersByTime ( 50 )
22
+ expect ( + now . value ) . toBeGreaterThan ( initial )
23
+ } )
24
+
12
25
function testControl ( interval : any ) {
13
26
it ( `should control now timestamp by ${ interval } ` , async ( ) => {
14
27
let initial = + new Date ( )
Original file line number Diff line number Diff line change @@ -12,6 +12,13 @@ export interface UseNowOptions<Controls extends boolean> {
12
12
*/
13
13
controls ?: Controls
14
14
15
+ /**
16
+ * Start the clock immediately
17
+ *
18
+ * @default true
19
+ */
20
+ immediate ?: boolean
21
+
15
22
/**
16
23
* Update interval in milliseconds, or use requestAnimationFrame
17
24
*
@@ -32,15 +39,16 @@ export function useNow(options: UseNowOptions<boolean> = {}) {
32
39
const {
33
40
controls : exposeControls = false ,
34
41
interval = 'requestAnimationFrame' ,
42
+ immediate = true ,
35
43
} = options
36
44
37
45
const now = deepRef ( new Date ( ) )
38
46
39
47
const update = ( ) => now . value = new Date ( )
40
48
41
49
const controls : Pausable = interval === 'requestAnimationFrame'
42
- ? useRafFn ( update , { immediate : true } )
43
- : useIntervalFn ( update , interval , { immediate : true } )
50
+ ? useRafFn ( update , { immediate } )
51
+ : useIntervalFn ( update , interval , { immediate } )
44
52
45
53
if ( exposeControls ) {
46
54
return {
You can’t perform that action at this time.
0 commit comments