Fix PFL VU meter trying to update after it's killed.

This commit is contained in:
Matthew Stratford 2021-01-30 19:13:04 +00:00
parent a82f68e89f
commit c1a4724e99

View file

@ -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. const FPS = 30; // Limit the FPS so that lower spec machines have a better time juggling CPU.
useEffect(() => { useEffect(() => {
let isMounted = true; // This VU exists as we're calling useEffect
const animate = () => { const animate = () => {
if (!isMic || isMicOpen) { if ((!isMic || isMicOpen) && isMounted) {
const result = audioEngine.getLevels( const result = audioEngine.getLevels(
props.source, props.source,
props.stereo ? props.stereo : false props.stereo ? props.stereo : false
); );
setPeakL(result[0]); setPeakL(result[0]);
if (props.stereo) { if (props.stereo && isMounted) {
setPeakR(result[1]); setPeakR(result[1]);
} }
setTimeout((current = rafRef.current, a = animate) => { setTimeout((current = rafRef.current, a = animate) => {
current = requestAnimationFrame(a); if (isMounted) current = requestAnimationFrame(a);
}, 1000 / FPS); }, 1000 / FPS);
} }
}; };
@ -49,6 +50,7 @@ export function VUMeter(props: VUMeterProps) {
rafRef.current = requestAnimationFrame(animate); rafRef.current = requestAnimationFrame(animate);
} }
return () => { return () => {
isMounted = false; // Tell the async stuff above to not bother if the VU meter has gone away.
if (rafRef.current !== null) { if (rafRef.current !== null) {
cancelAnimationFrame(rafRef.current); cancelAnimationFrame(rafRef.current);
rafRef.current = null; rafRef.current = null;