fix(call): clear soundboard safety timer + detach permission onchange
- CallSoundboard: the 30s safety timeout (which unsticks the one-at-a-time playingKey guard if audio never signals end) was never cleared, so it fired ~30s after every clip. It's now stored in a per-play token that done() clears by identity — a natural 'ended' cancels it, and a stale done() from a prior clip can't disarm a newer clip's timer (which matters because a rejected audio.play() fires neither ended nor error, leaving the timer as the only guard-reset). The unmount effect also clears any pending timer, and the timer is armed only when there's an audio element. - PrescreenControls: useMediaPermissions set PermissionStatus.onchange but never removed it → a permission change after unmount setState'd a dead component and retained the callback. Now guards all setState with a cancelled flag and detaches onchange in the effect cleanup. Bug-hunt findings from LOTUS_TODO. Three review passes (the last prescribed the per-play token to close a shared-ref cross-play edge). Gate-green (tsc, eslint, prettier, 922 tests, build). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -67,9 +67,11 @@ export function CallSoundboard({ callEmbed }: CallSoundboardProps) {
|
||||
// C-L6: the play() flow schedules a 30s safety timeout that clears playingKey;
|
||||
// guard those setState calls against the component unmounting first.
|
||||
const mountedRef = useRef(true);
|
||||
const safetyTimerRef = useRef<number | undefined>(undefined);
|
||||
useEffect(
|
||||
() => () => {
|
||||
mountedRef.current = false;
|
||||
if (safetyTimerRef.current !== undefined) window.clearTimeout(safetyTimerRef.current);
|
||||
},
|
||||
[],
|
||||
);
|
||||
@@ -96,7 +98,17 @@ export function CallSoundboard({ callEmbed }: CallSoundboardProps) {
|
||||
if (playingKey) return; // one at a time (fork also enforces this)
|
||||
setPlayingKey(flat.key);
|
||||
setError(undefined);
|
||||
// Per-play timer token: `done` clears its OWN timer by identity, so a
|
||||
// stale done() from a prior clip can't disarm a newer clip's safety timer
|
||||
// (which — since a rejected audio.play() fires neither ended nor error —
|
||||
// is sometimes the only thing that unsticks the playingKey guard).
|
||||
let myTimer: number | undefined;
|
||||
const done = () => {
|
||||
if (myTimer !== undefined) {
|
||||
window.clearTimeout(myTimer);
|
||||
if (safetyTimerRef.current === myTimer) safetyTimerRef.current = undefined;
|
||||
myTimer = undefined;
|
||||
}
|
||||
if (!mountedRef.current) return;
|
||||
setPlayingKey((k) => (k === flat.key ? undefined : k));
|
||||
};
|
||||
@@ -108,11 +120,12 @@ export function CallSoundboard({ callEmbed }: CallSoundboardProps) {
|
||||
if (audio) {
|
||||
audio.addEventListener('ended', done, { once: true });
|
||||
audio.addEventListener('error', done, { once: true });
|
||||
// Safety: clear the guard even if the audio never signals end.
|
||||
myTimer = window.setTimeout(done, 30_000);
|
||||
safetyTimerRef.current = myTimer;
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
// Safety: clear the guard even if the audio never signals end.
|
||||
window.setTimeout(done, 30_000);
|
||||
} catch {
|
||||
setError('Could not play that clip.');
|
||||
done();
|
||||
|
||||
@@ -17,15 +17,29 @@ function useMediaPermissions(): MediaPermState {
|
||||
useEffect(() => {
|
||||
if (!navigator.permissions) {
|
||||
setState('unknown');
|
||||
return;
|
||||
return undefined;
|
||||
}
|
||||
let cancelled = false;
|
||||
let permStatus: PermissionStatus | undefined;
|
||||
navigator.permissions
|
||||
.query({ name: 'microphone' as unknown as PermissionDescriptor['name'] })
|
||||
.then((result) => {
|
||||
if (cancelled) return;
|
||||
permStatus = result;
|
||||
setState(result.state as MediaPermState);
|
||||
result.onchange = () => setState(result.state as MediaPermState);
|
||||
result.onchange = () => {
|
||||
if (!cancelled) setState(result.state as MediaPermState);
|
||||
};
|
||||
})
|
||||
.catch(() => setState('unknown'));
|
||||
.catch(() => {
|
||||
if (!cancelled) setState('unknown');
|
||||
});
|
||||
// Detach the onchange handler on unmount so it can't setState afterward (and
|
||||
// so the PermissionStatus doesn't retain the callback).
|
||||
return () => {
|
||||
cancelled = true;
|
||||
if (permStatus) permStatus.onchange = null;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return state;
|
||||
|
||||
Reference in New Issue
Block a user