fix(memory): утечки ObjectURL в 4 компонентах (22.09.2026)

This commit is contained in:
2026-09-22 13:09:34 +03:00
parent eabe823d7f
commit 45628fdaf8
4 changed files with 64 additions and 34 deletions
+13 -8
View File
@@ -209,16 +209,21 @@ const FeedPostComposer: React.FC<FeedPostComposerProps> = ({
setSuccess(false); setSuccess(false);
}, [isOpen, editingPost, language]); }, [isOpen, editingPost, language]);
// 🔧 ФИКС: revoke ObjectURL при размонтировании // 🔧 ФИКС (22.09.2026): утечка ObjectURL при размонтировании.
// Было: setSlots в cleanup — setState в unmounted компоненте НЕ выполняется,
// поэтому URL.revokeObjectURL никогда не вызывался.
// Стало: ref с актуальными слотами + чтение из ref в cleanup.
const slotsRef = useRef<(ImageSlot | null)[]>([null, null, null]);
useEffect(() => {
slotsRef.current = slots;
}, [slots]);
useEffect(() => { useEffect(() => {
return () => { return () => {
setSlots(prev => { slotsRef.current.forEach(slot => {
prev.forEach(slot => { if (slot?.previewUrl) {
if (slot?.previewUrl) { try { URL.revokeObjectURL(slot.previewUrl); } catch { /* ignore */ }
try { URL.revokeObjectURL(slot.previewUrl); } catch { /* ignore */ } }
}
});
return prev;
}); });
}; };
}, []); }, []);
+10
View File
@@ -45,11 +45,20 @@ const PronunciationModal: React.FC<PronunciationModalProps> = ({ isOpen, onClose
...extra, ...extra,
}); });
// 🔧 ФИКС (22.09.2026): утечка ObjectURL. Каждый playWord создавал blob-URL
// без revoke. Добавили audioUrlRef — хранит текущий URL для очистки.
const audioUrlRef = useRef<string | null>(null);
const stopAudio = () => { const stopAudio = () => {
if (audioRef.current) { if (audioRef.current) {
audioRef.current.pause(); audioRef.current.pause();
audioRef.current = null; audioRef.current = null;
} }
// 🔧 Освобождаем предыдущий blob-URL
if (audioUrlRef.current) {
try { URL.revokeObjectURL(audioUrlRef.current); } catch { /* ignore */ }
audioUrlRef.current = null;
}
if ('speechSynthesis' in window) { if ('speechSynthesis' in window) {
window.speechSynthesis.cancel(); window.speechSynthesis.cancel();
} }
@@ -61,6 +70,7 @@ const PronunciationModal: React.FC<PronunciationModalProps> = ({ isOpen, onClose
try { try {
const audioBlob = await getTTS(word, 'male'); const audioBlob = await getTTS(word, 'male');
const audioUrl = URL.createObjectURL(audioBlob); const audioUrl = URL.createObjectURL(audioBlob);
audioUrlRef.current = audioUrl;
const audio = new Audio(audioUrl); const audio = new Audio(audioUrl);
audioRef.current = audio; audioRef.current = audio;
audio.onended = () => setIsPlaying(false); audio.onended = () => setIsPlaying(false);
+32 -25
View File
@@ -64,6 +64,25 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
const [isPhraseLoading, setIsPhraseLoading] = useState(false); const [isPhraseLoading, setIsPhraseLoading] = useState(false);
const [isPlayingPhrase, setIsPlayingPhrase] = useState(false); const [isPlayingPhrase, setIsPlayingPhrase] = useState(false);
const audioRef = useRef<HTMLAudioElement | null>(null); const audioRef = useRef<HTMLAudioElement | null>(null);
// 🔧 ФИКС (22.09.2026): утечка ObjectURL в handlePlayPhrase, handlePlayTranslate, handlePlayLetter.
// Каждый play создавал blob-URL без revoke. Добавили audioUrlRef + stopAudioHelper.
const audioUrlRef = useRef<string | null>(null);
const stopAudioHelper = () => {
if (audioRef.current) {
audioRef.current.pause();
audioRef.current = null;
}
if (audioUrlRef.current) {
try { URL.revokeObjectURL(audioUrlRef.current); } catch { /* ignore */ }
audioUrlRef.current = null;
}
if ('speechSynthesis' in window) {
window.speechSynthesis.cancel();
}
};
const scrollContainerRef = useRef<HTMLDivElement>(null); const scrollContainerRef = useRef<HTMLDivElement>(null);
const menuTrackedRef = useRef(false); const menuTrackedRef = useRef(false);
@@ -132,6 +151,13 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
onClose(); onClose();
}; };
// 🔧 ФИКС (22.09.2026): очистка audio при размонтировании
useEffect(() => {
return () => {
stopAudioHelper();
};
}, []);
const handleOpenLetter = (index: number) => { const handleOpenLetter = (index: number) => {
setSelectedLetter(index); setSelectedLetter(index);
scrollToTop(); scrollToTop();
@@ -250,20 +276,13 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
const handlePlayPhrase = async () => { const handlePlayPhrase = async () => {
if (!phraseOfDay || isPlayingPhrase) return; if (!phraseOfDay || isPlayingPhrase) return;
if (audioRef.current) { stopAudioHelper();
audioRef.current.pause();
audioRef.current = null;
}
if ('speechSynthesis' in window) {
window.speechSynthesis.cancel();
}
setIsPlayingPhrase(true); setIsPlayingPhrase(true);
try { try {
const audioBlob = await getTTS(phraseOfDay.phrase_pl, 'male'); const audioBlob = await getTTS(phraseOfDay.phrase_pl, 'male');
const audioUrl = URL.createObjectURL(audioBlob); const audioUrl = URL.createObjectURL(audioBlob);
audioUrlRef.current = audioUrl;
const audio = new Audio(audioUrl); const audio = new Audio(audioUrl);
audioRef.current = audio; audioRef.current = audio;
audio.onended = () => setIsPlayingPhrase(false); audio.onended = () => setIsPlayingPhrase(false);
@@ -296,20 +315,13 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
const handlePlayTranslate = async () => { const handlePlayTranslate = async () => {
if (!translateResult || isPlaying) return; if (!translateResult || isPlaying) return;
if (audioRef.current) { stopAudioHelper();
audioRef.current.pause();
audioRef.current = null;
}
if ('speechSynthesis' in window) {
window.speechSynthesis.cancel();
}
setIsPlaying(true); setIsPlaying(true);
try { try {
const audioBlob = await getTTS(translateResult, 'male'); const audioBlob = await getTTS(translateResult, 'male');
const audioUrl = URL.createObjectURL(audioBlob); const audioUrl = URL.createObjectURL(audioBlob);
audioUrlRef.current = audioUrl;
const audio = new Audio(audioUrl); const audio = new Audio(audioUrl);
audioRef.current = audio; audioRef.current = audio;
audio.onended = () => setIsPlaying(false); audio.onended = () => setIsPlaying(false);
@@ -321,17 +333,12 @@ const TeacherMenuModal: React.FC<TeacherMenuModalProps> = ({
}; };
const handlePlayLetter = async (word: string) => { const handlePlayLetter = async (word: string) => {
if (audioRef.current) { stopAudioHelper();
audioRef.current.pause();
audioRef.current = null;
}
if ('speechSynthesis' in window) {
window.speechSynthesis.cancel();
}
setIsPlaying(true); setIsPlaying(true);
try { try {
const audioBlob = await getTTS(word, 'male'); const audioBlob = await getTTS(word, 'male');
const audioUrl = URL.createObjectURL(audioBlob); const audioUrl = URL.createObjectURL(audioBlob);
audioUrlRef.current = audioUrl;
const audio = new Audio(audioUrl); const audio = new Audio(audioUrl);
audioRef.current = audio; audioRef.current = audio;
audio.onended = () => setIsPlaying(false); audio.onended = () => setIsPlaying(false);
+9 -1
View File
@@ -43,11 +43,18 @@ const TypingModal: React.FC<TypingModalProps> = ({ isOpen, onClose }) => {
...extra, ...extra,
}); });
// 🔧 ФИКС (22.09.2026): утечка ObjectURL в playAnswer.
const audioUrlRef = useRef<string | null>(null);
const stopAudio = () => { const stopAudio = () => {
if (audioRef.current) { if (audioRef.current) {
audioRef.current.pause(); audioRef.current.pause();
audioRef.current = null; audioRef.current = null;
} }
if (audioUrlRef.current) {
try { URL.revokeObjectURL(audioUrlRef.current); } catch { /* ignore */ }
audioUrlRef.current = null;
}
if ('speechSynthesis' in window) { if ('speechSynthesis' in window) {
window.speechSynthesis.cancel(); window.speechSynthesis.cancel();
} }
@@ -146,13 +153,14 @@ const TypingModal: React.FC<TypingModalProps> = ({ isOpen, onClose }) => {
setShowAnswer(true); setShowAnswer(true);
}; };
const playAnswer = async () => { const playAnswer = async () => {
if (!correctAnswer) return; if (!correctAnswer) return;
stopAudio(); stopAudio();
setIsPlaying(true); setIsPlaying(true);
try { try {
const audioBlob = await getTTS(correctAnswer, 'male'); const audioBlob = await getTTS(correctAnswer, 'male');
const audioUrl = URL.createObjectURL(audioBlob); const audioUrl = URL.createObjectURL(audioBlob);
audioUrlRef.current = audioUrl;
const audio = new Audio(audioUrl); const audio = new Audio(audioUrl);
audioRef.current = audio; audioRef.current = audio;
audio.onended = () => setIsPlaying(false); audio.onended = () => setIsPlaying(false);