|
| 1 | +import { useState, useEffect, useRef } from 'react'; |
| 2 | +import { Time, Validate } from './utils'; |
| 3 | + |
| 4 | +export default function useTimer(settings) { |
| 5 | + const { expiryTimestamp: expiry, onExpire } = settings || {}; |
| 6 | + const [expiryTimestamp, setExpiryTimestamp] = useState(expiry); |
| 7 | + const [seconds, setSeconds] = useState(Time.getSecondsFromExpiry(expiryTimestamp)); |
| 8 | + const [isRunning, setIsRunning] = useState(true); |
| 9 | + const intervalRef = useRef(); |
| 10 | + |
| 11 | + function clearIntervalRef() { |
| 12 | + if (intervalRef.current) { |
| 13 | + setIsRunning(false); |
| 14 | + clearInterval(intervalRef.current); |
| 15 | + intervalRef.current = undefined; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + function handleExpire() { |
| 20 | + clearIntervalRef(); |
| 21 | + Validate.onExpire(onExpire) && onExpire(); |
| 22 | + } |
| 23 | + |
| 24 | + function start() { |
| 25 | + if (!intervalRef.current) { |
| 26 | + setIsRunning(true); |
| 27 | + intervalRef.current = setInterval(() => { |
| 28 | + const secondsValue = Time.getSecondsFromExpiry(expiryTimestamp); |
| 29 | + if (secondsValue <= 0) { |
| 30 | + handleExpire(); |
| 31 | + } |
| 32 | + setSeconds(secondsValue); |
| 33 | + }, 1000); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + function pause() { |
| 38 | + clearIntervalRef(); |
| 39 | + } |
| 40 | + |
| 41 | + function resume() { |
| 42 | + if (!intervalRef.current) { |
| 43 | + setIsRunning(true); |
| 44 | + intervalRef.current = setInterval(() => setSeconds((prevSeconds) => { |
| 45 | + const secondsValue = prevSeconds - 1; |
| 46 | + if (secondsValue <= 0) { |
| 47 | + handleExpire(); |
| 48 | + } |
| 49 | + return secondsValue; |
| 50 | + }), 1000); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function restart(newExpiryTimestamp) { |
| 55 | + clearIntervalRef(); |
| 56 | + setExpiryTimestamp(newExpiryTimestamp); |
| 57 | + } |
| 58 | + |
| 59 | + function handleExtraMilliSeconds(secondsValue, extraMilliSeconds) { |
| 60 | + setIsRunning(true); |
| 61 | + intervalRef.current = setTimeout(() => { |
| 62 | + const currentSeconds = Time.getSecondsFromExpiry(expiryTimestamp); |
| 63 | + setSeconds(currentSeconds); |
| 64 | + if (currentSeconds <= 0) { |
| 65 | + handleExpire(); |
| 66 | + } else { |
| 67 | + intervalRef.current = undefined; |
| 68 | + start(); |
| 69 | + } |
| 70 | + }, extraMilliSeconds); |
| 71 | + } |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + if (Validate.expiryTimestamp(expiryTimestamp)) { |
| 75 | + const secondsValue = Time.getSecondsFromExpiry(expiryTimestamp); |
| 76 | + const extraMilliSeconds = Math.floor((secondsValue - Math.floor(secondsValue)) * 1000); |
| 77 | + setSeconds(secondsValue); |
| 78 | + if (extraMilliSeconds > 0) { |
| 79 | + handleExtraMilliSeconds(secondsValue, extraMilliSeconds); |
| 80 | + } else { |
| 81 | + start(); |
| 82 | + } |
| 83 | + } |
| 84 | + return clearIntervalRef; |
| 85 | + }, [expiryTimestamp]); |
| 86 | + |
| 87 | + |
| 88 | + return { |
| 89 | + ...Time.getTimeFromSeconds(seconds), start, pause, resume, restart, isRunning, |
| 90 | + }; |
| 91 | +} |
0 commit comments