Try and guard against play/pause function being called after player is destroyed (#9364)

* try and guard play/pause against unmount

* add try/catch to avoid fatal crashes
This commit is contained in:
Samuel Newman
2025-11-11 02:02:13 +02:00
committed by GitHub
parent bb365856c9
commit 835a716eaf
+22 -8
View File
@@ -57,6 +57,7 @@ import {
import {sanitizeDisplayName} from '#/lib/strings/display-names' import {sanitizeDisplayName} from '#/lib/strings/display-names'
import {cleanError} from '#/lib/strings/errors' import {cleanError} from '#/lib/strings/errors'
import {sanitizeHandle} from '#/lib/strings/handles' import {sanitizeHandle} from '#/lib/strings/handles'
import {logger} from '#/logger'
import {isAndroid} from '#/platform/detection' import {isAndroid} from '#/platform/detection'
import {useA11y} from '#/state/a11y' import {useA11y} from '#/state/a11y'
import { import {
@@ -1044,16 +1045,29 @@ function PlayPauseTapArea({
const {isPlaying} = useEvent(player, 'playingChange', { const {isPlaying} = useEvent(player, 'playingChange', {
isPlaying: player.playing, isPlaying: player.playing,
}) })
const isMounted = useRef(false)
const togglePlayPause = () => { useEffect(() => {
if (!player) return isMounted.current = true
doubleTapRef.current = null return () => {
if (player.playing) { isMounted.current = false
player.pause()
} else {
player.play()
} }
} }, [])
const togglePlayPause = useNonReactiveCallback(() => {
// gets called after a timeout, so guard against being called after unmount -sfn
if (!player || !isMounted.current) return
doubleTapRef.current = null
try {
if (player.playing) {
player.pause()
} else {
player.play()
}
} catch (err) {
logger.error('Could not toggle play/pause', {safeMessage: err})
}
})
const onPress = () => { const onPress = () => {
if (doubleTapRef.current) { if (doubleTapRef.current) {