File size: 1,369 Bytes
82d1e90 1805613 82d1e90 0ce53fb 82d1e90 1805613 82d1e90 1805613 82d1e90 |
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 |
import { useStore } from "@/app/store"
import { useEffect, useRef } from "react"
import { useIsBusy } from "./useIsBusy"
export function useProgressTimer() {
const runningRef = useRef(false)
const timerRef = useRef<NodeJS.Timeout>()
const progress = useStore(s => s.progress)
const stage = useStore(s => s.stage)
const { isBusy, busyRef } = useIsBusy()
const timerFn = async () => {
const { progress, stage } = useStore.getState()
let isBusy = busyRef.current
clearTimeout(timerRef.current)
if (!isBusy || stage === "idle") {
return
}
/*
console.log("progress function:", {
stage,
delay: progressDelayInMsPerStage[stage],
progress,
})
*/
useStore.setState({
// progress: Math.min(maxProgressPerStage[stage], progress + 1)
progress: Math.min(100, progress + 1)
})
// timerRef.current = setTimeout(timerFn, progressDelayInMsPerStage[stage])
timerRef.current = setTimeout(timerFn, 1600)
}
// const running = useRef(false)
useEffect(() => {
timerFn()
clearTimeout(timerRef.current)
let isBusy = busyRef.current
if (!isBusy) {
return
}/* else if (running.current) {
return
}
*/
// running.current = true
timerRef.current = setTimeout(timerFn, 0)
}, [isBusy])
return { isBusy, busyRef, progress, stage }
} |