From c1a4724e992e9a5bdedb8c7dd060c85ee6fe8996 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Sat, 30 Jan 2021 19:13:04 +0000 Subject: [PATCH] Fix PFL VU meter trying to update after it's killed. --- src/optionsMenu/helpers/VUMeter.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/optionsMenu/helpers/VUMeter.tsx b/src/optionsMenu/helpers/VUMeter.tsx index 0e7791b..c20bc13 100644 --- a/src/optionsMenu/helpers/VUMeter.tsx +++ b/src/optionsMenu/helpers/VUMeter.tsx @@ -30,18 +30,19 @@ export function VUMeter(props: VUMeterProps) { const FPS = 30; // Limit the FPS so that lower spec machines have a better time juggling CPU. useEffect(() => { + let isMounted = true; // This VU exists as we're calling useEffect const animate = () => { - if (!isMic || isMicOpen) { + if ((!isMic || isMicOpen) && isMounted) { const result = audioEngine.getLevels( props.source, props.stereo ? props.stereo : false ); setPeakL(result[0]); - if (props.stereo) { + if (props.stereo && isMounted) { setPeakR(result[1]); } setTimeout((current = rafRef.current, a = animate) => { - current = requestAnimationFrame(a); + if (isMounted) current = requestAnimationFrame(a); }, 1000 / FPS); } }; @@ -49,6 +50,7 @@ export function VUMeter(props: VUMeterProps) { rafRef.current = requestAnimationFrame(animate); } return () => { + isMounted = false; // Tell the async stuff above to not bother if the VU meter has gone away. if (rafRef.current !== null) { cancelAnimationFrame(rafRef.current); rafRef.current = null;