rsmpark / tempo_tester

0 stars 0 forks source link

Synchronize CountdownPrompt and Metronome setIntervals #23

Open rsmpark opened 9 months ago

rsmpark commented 9 months ago

The countdown should be in sync with the click of the metronome.

rsmpark commented 9 months ago
const CountdownPrompt = () => {
  const [count, setCount] = useState(7);
  const { dispatch } = useAppCtx();
  const { state } = useMetronomeCtx();
  const metronomeCnt = state.count;
  const didMount = useRef(false);

  useEffect(() => {
    if (didMount.current) {
      setCount((prevCount) => prevCount - 1);
    }
    didMount.current = true;
  }, [metronomeCnt]);

  if (count === 0) {
    dispatch({ type: "NEXT_STAGE" });
  }

  return <div>{<p>{count}</p>}</div>;
};

export default CountdownPrompt;

Countdown count is synchronized with the metronome count state. -> if metronome count changes, decrement countdown

However, this causes issues as useEffect is run twice causing the countdown to be decremented even if metronome count is not updated

rsmpark commented 9 months ago

Need to explore alternative approaches. Possible solutions: