fix(mobile): harden recorder, queue, context, and screen async lifecycles
This commit is contained in:
parent
3f9e412257
commit
bd3c1a98d9
25 changed files with 2279 additions and 68 deletions
|
|
@ -89,6 +89,7 @@ export default function HistoryDetailScreen({
|
|||
playerRef.current = player
|
||||
const mountedRef = useRef(true)
|
||||
const loadGenerationRef = useRef(0)
|
||||
const playbackGenerationRef = useRef(0)
|
||||
const [detail, setDetail] = useState<HistoryDetail | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
|
@ -109,6 +110,7 @@ export default function HistoryDetailScreen({
|
|||
return () => {
|
||||
mountedRef.current = false
|
||||
loadGenerationRef.current += 1
|
||||
playbackGenerationRef.current += 1
|
||||
void Promise.resolve(playerRef.current.stopPlayer()).catch(() => undefined)
|
||||
}
|
||||
}, [])
|
||||
|
|
@ -306,53 +308,62 @@ export default function HistoryDetailScreen({
|
|||
|
||||
const controlAudio = async (audioFile: AudioFile): Promise<void> => {
|
||||
if (user === null || audioBusy) return
|
||||
const playbackGeneration = ++playbackGenerationRef.current
|
||||
setAudioBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
if (activeAudioId === audioFile.id && player.state.isPlaying) {
|
||||
await player.pausePlayer()
|
||||
if (mountedRef.current) setPaused(true)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) setPaused(true)
|
||||
return
|
||||
}
|
||||
if (activeAudioId === audioFile.id && paused) {
|
||||
await player.resumePlayer()
|
||||
if (mountedRef.current) setPaused(false)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) setPaused(false)
|
||||
return
|
||||
}
|
||||
if (activeAudioId !== null) {
|
||||
await player.stopPlayer()
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
}
|
||||
const signedUrl = await createAudioPlaybackUrl(user.id, audioFile)
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
await player.startPlayer(signedUrl)
|
||||
if (!mountedRef.current) {
|
||||
void Promise.resolve(player.stopPlayer()).catch(() => undefined)
|
||||
return
|
||||
}
|
||||
if (playbackGeneration !== playbackGenerationRef.current) return
|
||||
setActiveAudioId(audioFile.id)
|
||||
setPaused(false)
|
||||
} catch {
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
setActiveAudioId(null)
|
||||
setPaused(false)
|
||||
setError(t('mobile.hist.audioError'))
|
||||
} finally {
|
||||
if (mountedRef.current) setAudioBusy(false)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) {
|
||||
setAudioBusy(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const stopAudio = async (): Promise<void> => {
|
||||
const playbackGeneration = ++playbackGenerationRef.current
|
||||
setAudioBusy(true)
|
||||
try {
|
||||
await player.stopPlayer()
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
setActiveAudioId(null)
|
||||
setPaused(false)
|
||||
} catch {
|
||||
if (mountedRef.current) setError(t('mobile.hist.audioError'))
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) {
|
||||
setError(t('mobile.hist.audioError'))
|
||||
}
|
||||
} finally {
|
||||
if (mountedRef.current) setAudioBusy(false)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) {
|
||||
setAudioBusy(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue