use-interval.ts
ink/hooks/use-interval.ts
68
Lines
1796
Bytes
2
Exports
2
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with ui-flow. It contains 68 lines, 2 detected imports, and 2 detected exports.
Important relationships
Detected exports
useAnimationTimeruseInterval
Keywords
clockintervalmslastupdateclockcontextonchangevoidusecontextuseeffecttimeshared
Detected imports
react../components/ClockContext.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { useContext, useEffect, useRef, useState } from 'react'
import { ClockContext } from '../components/ClockContext.js'
/**
* Returns the clock time, updating at the given interval.
* Subscribes as non-keepAlive — won't keep the clock alive on its own,
* but updates whenever a keepAlive subscriber (e.g. the spinner)
* is driving the clock.
*
* Use this to drive pure time-based computations (shimmer position,
* frame index) from the shared clock.
*/
export function useAnimationTimer(intervalMs: number): number {
const clock = useContext(ClockContext)
const [time, setTime] = useState(() => clock?.now() ?? 0)
useEffect(() => {
if (!clock) return
let lastUpdate = clock.now()
const onChange = (): void => {
const now = clock.now()
if (now - lastUpdate >= intervalMs) {
lastUpdate = now
setTime(now)
}
}
return clock.subscribe(onChange, false)
}, [clock, intervalMs])
return time
}
/**
* Interval hook backed by the shared Clock.
*
* Unlike `useInterval` from `usehooks-ts` (which creates its own setInterval),
* this piggybacks on the single shared clock so all timers consolidate into
* one wake-up. Pass `null` for intervalMs to pause.
*/
export function useInterval(
callback: () => void,
intervalMs: number | null,
): void {
const callbackRef = useRef(callback)
callbackRef.current = callback
const clock = useContext(ClockContext)
useEffect(() => {
if (!clock || intervalMs === null) return
let lastUpdate = clock.now()
const onChange = (): void => {
const now = clock.now()
if (now - lastUpdate >= intervalMs) {
lastUpdate = now
callbackRef.current()
}
}
return clock.subscribe(onChange, false)
}, [clock, intervalMs])
}