@@ -235,6 +235,25 @@ export function useTimerGroup(options: UseTimerGroupOptions = {}): TimerGroupRes
235235 [ callOnEnd , emit , evaluateItemSchedules ] ,
236236 ) ;
237237
238+ const getNextDelay = useCallback ( ( clock = readClock ( ) ) => {
239+ const updateIntervalMs = optionsRef . current . updateIntervalMs ?? 1000 ;
240+ let nextDelay = updateIntervalMs ;
241+
242+ for ( const item of itemsRef . current . values ( ) ) {
243+ if ( item . state . status !== 'running' ) continue ;
244+
245+ const schedules = item . definition . schedules ?? [ ] ;
246+ schedules . forEach ( ( schedule , index ) => {
247+ const key = schedule . id ?? String ( index ) ;
248+ const scheduleState = item . schedules . get ( key ) ;
249+ const lastRunAt = scheduleState ?. lastRunAt ?? item . state . startedAt ?? clock . wallNow ;
250+ nextDelay = Math . min ( nextDelay , Math . max ( 1 , lastRunAt + schedule . everyMs - clock . wallNow ) ) ;
251+ } ) ;
252+ }
253+
254+ return nextDelay ;
255+ } , [ ] ) ;
256+
238257 const ensureItem = useCallback ( ( definition : TimerGroupItem ) : { item : InternalGroupItem ; added : boolean } => {
239258 const existing = itemsRef . current . get ( definition . id ) ;
240259 if ( existing ) {
@@ -401,7 +420,7 @@ export function useTimerGroup(options: UseTimerGroupOptions = {}): TimerGroupRes
401420 }
402421
403422 rerender ( ) ;
404- } , optionsRef . current . updateIntervalMs ?? 1000 ) ;
423+ } , getNextDelay ( ) ) ;
405424
406425 return ( ) => {
407426 if ( timeoutRef . current !== null ) {
@@ -410,7 +429,7 @@ export function useTimerGroup(options: UseTimerGroupOptions = {}): TimerGroupRes
410429 clearScheduledTick ( ) ;
411430 mountedRef . current = false ;
412431 } ;
413- } , [ activeSignature , clearScheduledTick , emit , processItem ] ) ;
432+ } , [ activeSignature , clearScheduledTick , emit , getNextDelay , processItem ] ) ;
414433
415434 const get = useCallback (
416435 ( id : string ) => {
@@ -422,6 +441,12 @@ export function useTimerGroup(options: UseTimerGroupOptions = {}): TimerGroupRes
422441 ) ;
423442
424443 const now = readClock ( ) . wallNow ;
444+ const startAll = useCallback ( ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( start ) , [ start ] ) ;
445+ const pauseAll = useCallback ( ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( pause ) , [ pause ] ) ;
446+ const resumeAll = useCallback ( ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( resume ) , [ resume ] ) ;
447+ const resetAll = useCallback ( ( resetOptions ?: { autoStart ?: boolean } ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( id => reset ( id , resetOptions ) ) , [ reset ] ) ;
448+ const restartAll = useCallback ( ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( restart ) , [ restart ] ) ;
449+ const cancelAll = useCallback ( ( reason ?: string ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( id => cancel ( id , reason ) ) , [ cancel ] ) ;
425450
426451 return useMemo (
427452 ( ) => ( {
@@ -439,14 +464,14 @@ export function useTimerGroup(options: UseTimerGroupOptions = {}): TimerGroupRes
439464 reset,
440465 restart,
441466 cancel,
442- startAll : ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( start ) ,
443- pauseAll : ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( pause ) ,
444- resumeAll : ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( resume ) ,
445- resetAll : ( resetOptions ?: { autoStart ?: boolean } ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( id => reset ( id , resetOptions ) ) ,
446- restartAll : ( ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( restart ) ,
447- cancelAll : ( reason ?: string ) => Array . from ( itemsRef . current . keys ( ) ) . forEach ( id => cancel ( id , reason ) ) ,
467+ startAll,
468+ pauseAll,
469+ resumeAll,
470+ resetAll,
471+ restartAll,
472+ cancelAll,
448473 } ) ,
449- [ add , cancel , clear , get , now , pause , remove , reset , restart , resume , start , update ] ,
474+ [ add , cancel , cancelAll , clear , get , now , pause , pauseAll , remove , reset , resetAll , restart , restartAll , resume , resumeAll , start , startAll , update ] ,
450475 ) ;
451476}
452477
0 commit comments