-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathuseInterval.ts
More file actions
90 lines (77 loc) · 2.69 KB
/
useInterval.ts
File metadata and controls
90 lines (77 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { useEffect, useRef, useState } from 'react';
/** The use interval options */
export interface UseIntervalOptions {
/** Start the interval immediately */
immediately?: boolean;
}
/** The use interval return type */
export interface UseIntervalReturn {
/** Is the interval active */
active: boolean;
/** Pause the interval */
pause: () => void;
/** Resume the interval */
resume: () => void;
/** Toggle the interval */
toggle: () => void;
}
interface UseInterval {
(callback: () => void, interval?: number, options?: UseIntervalOptions): UseIntervalReturn;
(callback: () => void, options?: UseIntervalOptions & { interval?: number }): UseIntervalReturn;
}
/**
* @name useInterval
* @description - Hook that makes and interval and returns controlling functions
* @category Time
* @usage high
*
* @overload
* @param {() => void} callback Any callback function
* @param {number} [interval=1000] Time in milliseconds
* @param {boolean} [options.immediately=true] Start the interval immediately
* @returns {UseIntervalReturn}
*
* @example
* const { active, pause, resume, toggle } = useInterval(() => console.log('inside interval'), 2500);
*
* @overload
* @param {() => void} callback Any callback function
* @param {number} [options.interval=1000] Time in milliseconds
* @param {boolean} [options.immediately=true] Start the interval immediately
*
* @example
* const { active, pause, resume, toggle } = useInterval(() => console.log('inside interval'), { interval: 2500 });
*/
export const useInterval = ((...params: any[]): UseIntervalReturn => {
const callback = params[0] as () => void;
const interval =
((typeof params[1] === 'number'
? params[1]
: (params[1] as (UseIntervalOptions & { interval?: number }) | undefined)
?.interval) as number) ?? 1000;
const options =
typeof params[1] === 'object'
? (params[1] as (UseIntervalOptions & { interval?: number }) | undefined)
: (params[2] as UseIntervalOptions | undefined);
const immediately = options?.immediately ?? true;
const [active, setActive] = useState<boolean>(immediately);
const intervalIdRef = useRef<ReturnType<typeof setInterval>>(undefined);
const internalCallbackRef = useRef(callback);
internalCallbackRef.current = callback;
useEffect(() => {
if (!active) return;
intervalIdRef.current = setInterval(() => internalCallbackRef.current(), interval);
return () => {
clearInterval(intervalIdRef.current);
};
}, [active, interval]);
const pause = () => setActive(false);
const resume = () => setActive(true);
const toggle = () => setActive((prev) => !prev);
return {
active,
pause,
resume,
toggle
};
}) as UseInterval;