UNPKG

react-compound-timer

Version:

React hooks for timers, countdowns, and stopwatches.

91 lines (85 loc) 2.64 kB
type Unit = 'ms' | 's' | 'm' | 'h' | 'd'; interface Checkpoint { time: number; callback: () => void; } type Checkpoints = Checkpoint[]; interface TimeParts { ms: number; s: number; m: number; h: number; d: number; } type TimeModelStateValues = 'INITED' | 'PLAYING' | 'PAUSED' | 'STOPPED'; type Direction = 'forward' | 'backward'; type TimerValue = TimeParts & { state: TimeModelStateValues; }; declare function getTimeParts(time: number, lastUnit: Unit, roundUnit?: Unit): TimeParts; interface TimeModelOptions { initialTime: number; startImmediately: boolean; direction: "forward" | "backward"; timeToUpdate: number; lastUnit: Unit; roundUnit: Unit; checkpoints: Checkpoints; } interface TimeModelEvents { onChange?: (timerValue: TimerValue) => void; onStart?: () => void; onResume?: () => void; onPause?: () => void; onStop?: () => void; onReset?: () => void; } declare class TimeModel { private options; events: TimeModelEvents; private internalTime; private time; private innerState; private timerId; constructor(options: TimeModelOptions, events?: TimeModelEvents); get value(): TimerValue; get currentOptions(): TimeModelOptions; /** * Change options methods **/ changeTime: (time: number) => void; changeLastUnit: (lastUnit: Unit) => void; changeRoundUnit: (roundUnit: Unit) => void; changeTimeToUpdate: (interval: number) => void; changeDirection: (direction: Direction) => void; changeCheckpoints: (checkpoints: Checkpoints) => void; /** * Timer controls methods **/ start: () => void; resume: (callImmediately?: boolean) => void; pause: () => void; stop: () => void; reset: () => void; /** * Private methods **/ private setTimerInterval; private getTimerValue; private computeTime; } declare const useTimeModel: (timer: TimeModel) => { value: TimerValue; changeTime: (time: number) => void; changeLastUnit: (lastUnit: Unit) => void; changeTimeToUpdate: (interval: number) => void; changeDirection: (direction: Direction) => void; changeCheckpoints: (checkpoints: Checkpoints) => void; start: () => void; pause: () => void; resume: (callImmediately?: boolean) => void; stop: () => void; reset: () => void; }; declare const createTimeModel: (options?: Partial<TimeModelOptions>, events?: TimeModelEvents) => TimeModel; export { Checkpoint, Checkpoints, Direction, TimeModelStateValues, TimeParts, TimerValue, Unit, createTimeModel, getTimeParts, useTimeModel };