fix bug and errors
This commit is contained in:
@@ -18,7 +18,12 @@ export function useTranscodingStatusPolling(advertisingId, enabled, path = "ads"
|
||||
const attemptsRef = useRef(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !advertisingId) return;
|
||||
if (!enabled || !advertisingId) {
|
||||
// Статус предыдущей сущности нельзя переносить на следующую: иначе
|
||||
// новый элемент сразу получит терминальный статус и polling сразу выключится.
|
||||
setStatus('PROCESSING');
|
||||
return;
|
||||
}
|
||||
|
||||
attemptsRef.current = 0;
|
||||
let cancelled = false;
|
||||
@@ -37,7 +42,7 @@ export function useTranscodingStatusPolling(advertisingId, enabled, path = "ads"
|
||||
const data = await res.json();
|
||||
if (cancelled) return;
|
||||
|
||||
setStatus(data.transcodingStatus);
|
||||
setStatus(data.transcodingStatus ?? 'PROCESSING');
|
||||
|
||||
attemptsRef.current += 1;
|
||||
const isTerminal = TERMINAL_STATUSES.includes(data.transcodingStatus);
|
||||
@@ -49,7 +54,15 @@ export function useTranscodingStatusPolling(advertisingId, enabled, path = "ads"
|
||||
setStatus('TIMEOUT');
|
||||
}
|
||||
} catch (err) {
|
||||
if (cancelled) return;
|
||||
console.warn('Status poll failed, retrying', err);
|
||||
// Ошибки тоже тратят попытки, иначе MAX_POLL_ATTEMPTS недостижим
|
||||
// и запрос к несуществующему эндпоинту будет повторяться вечно.
|
||||
attemptsRef.current += 1;
|
||||
if (attemptsRef.current >= MAX_POLL_ATTEMPTS) {
|
||||
setStatus('TIMEOUT');
|
||||
return;
|
||||
}
|
||||
timeoutId = setTimeout(poll, POLL_INTERVAL_MS);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user