1- import type { Pausable } from '@vueuse/shared'
1+ import type { AnyFn , Pausable } from '@vueuse/shared'
22import type { MaybeRefOrGetter , ShallowRef } from 'vue'
3+ import type { ConfigurableScheduler } from '../_configurable'
34import { useIntervalFn } from '@vueuse/shared'
45import { shallowRef , toValue } from 'vue'
56
6- export interface UseCountdownOptions {
7+ function getDefaultScheduler ( options : UseCountdownOptions ) {
8+ if ( 'interval' in options || 'immediate' in options ) {
9+ const {
10+ interval = 1000 ,
11+ immediate = false ,
12+ } = options
13+
14+ return ( cb : AnyFn ) => useIntervalFn ( cb , interval , { immediate } )
15+ }
16+
17+ return ( cb : AnyFn ) => useIntervalFn ( cb , 1000 , { immediate : false } )
18+ }
19+
20+ export interface UseCountdownOptions extends ConfigurableScheduler {
721 /**
822 * Interval for the countdown in milliseconds. Default is 1000ms.
23+ *
24+ * @deprecated Please use `scheduler` option instead
925 */
1026 interval ?: MaybeRefOrGetter < number >
1127 /**
@@ -19,6 +35,7 @@ export interface UseCountdownOptions {
1935 /**
2036 * Start the countdown immediately
2137 *
38+ * @deprecated Please use `scheduler` option instead
2239 * @default false
2340 */
2441 immediate ?: boolean
@@ -44,55 +61,61 @@ export interface UseCountdownReturn extends Pausable {
4461}
4562
4663/**
47- * Wrapper for `useIntervalFn` that provides a countdown timer in seconds.
64+ * Reactive countdown timer in seconds.
4865 *
4966 * @param initialCountdown
5067 * @param options
5168 *
5269 * @see https://vueuse.org/useCountdown
5370 */
54- export function useCountdown ( initialCountdown : MaybeRefOrGetter < number > , options ? : UseCountdownOptions ) : UseCountdownReturn {
71+ export function useCountdown ( initialCountdown : MaybeRefOrGetter < number > , options : UseCountdownOptions = { } ) : UseCountdownReturn {
5572 const remaining = shallowRef ( toValue ( initialCountdown ) )
5673
57- const intervalController = useIntervalFn ( ( ) => {
74+ const {
75+ scheduler = getDefaultScheduler ( options ) ,
76+ onTick,
77+ onComplete,
78+ } = options
79+
80+ const controls = scheduler ( ( ) => {
5881 const value = remaining . value - 1
5982 remaining . value = value < 0 ? 0 : value
60- options ?. onTick ?.( )
83+ onTick ?.( )
6184 if ( remaining . value <= 0 ) {
62- intervalController . pause ( )
63- options ?. onComplete ?.( )
85+ controls . pause ( )
86+ onComplete ?.( )
6487 }
65- } , options ?. interval ?? 1000 , { immediate : options ?. immediate ?? false } )
88+ } )
6689
6790 const reset = ( countdown ?: MaybeRefOrGetter < number > ) => {
6891 remaining . value = toValue ( countdown ) ?? toValue ( initialCountdown )
6992 }
7093
7194 const stop = ( ) => {
72- intervalController . pause ( )
95+ controls . pause ( )
7396 reset ( )
7497 }
7598
7699 const resume = ( ) => {
77- if ( ! intervalController . isActive . value ) {
100+ if ( ! controls . isActive . value ) {
78101 if ( remaining . value > 0 ) {
79- intervalController . resume ( )
102+ controls . resume ( )
80103 }
81104 }
82105 }
83106
84107 const start = ( countdown ?: MaybeRefOrGetter < number > ) => {
85108 reset ( countdown )
86- intervalController . resume ( )
109+ controls . resume ( )
87110 }
88111
89112 return {
90113 remaining,
91114 reset,
92115 stop,
93116 start,
94- pause : intervalController . pause ,
117+ pause : controls . pause ,
95118 resume,
96- isActive : intervalController . isActive ,
119+ isActive : controls . isActive ,
97120 }
98121}
0 commit comments