2021-02-04 19:15:23 +01:00
|
|
|
import { useState, useEffect, useRef, useCallback } from 'react';
|
2020-07-24 16:17:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* useTimeout
|
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Function} cb - callback function inside setTimeout
|
|
|
|
* @param {number} delay - delay in ms
|
|
|
|
* @param {boolean} [immediate] - determines whether the timeout is invoked immediately
|
2020-07-24 16:17:03 +02:00
|
|
|
*
|
2020-08-12 21:06:57 +02:00
|
|
|
* @return {Function|undefined}
|
2020-07-24 16:17:03 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useTimeout(cb, delay, immediate = true) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const saveCb = useRef();
|
|
|
|
const [timeoutId, setTimeoutId] = useState(null);
|
2020-07-24 16:17:03 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
saveCb.current = cb;
|
|
|
|
}, [cb]);
|
2020-07-24 16:17:03 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (timeoutId !== 'start') {
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
2020-07-24 16:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const id = setTimeout(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
saveCb.current();
|
|
|
|
}, delay);
|
2020-07-24 16:17:03 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
setTimeoutId(id);
|
2020-07-24 16:17:03 +02:00
|
|
|
|
|
|
|
return () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
};
|
|
|
|
}, [delay, timeoutId]);
|
2020-07-24 16:17:03 +02:00
|
|
|
|
|
|
|
const startTimeout = useCallback(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
setTimeoutId('start');
|
|
|
|
}, [timeoutId]);
|
2020-07-24 16:17:03 +02:00
|
|
|
|
|
|
|
if (immediate) {
|
2021-02-04 19:15:23 +01:00
|
|
|
startTimeout();
|
2020-07-24 16:17:03 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return startTimeout;
|
2020-07-24 16:17:03 +02:00
|
|
|
}
|