Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/plugins/text-tracks-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ function textTracksManager() {
});
};

const createUrlFallbacks = () => {
if (src) return [src, undefined];
if (type !== 'transcript') return [];
const createSourceUrl = () => {
if (src) return src;
if (type !== 'transcript') return undefined;

const source = player.cloudinary.source();
const publicId = source.publicId();
Expand All @@ -96,15 +96,15 @@ function textTracksManager() {
const baseUrl = getTranscriptionFileUrl(urlPrefix, deliveryType, publicId);
const localizedUrl = srclang ? getTranscriptionFileUrl(urlPrefix, deliveryType, publicId, srclang) : null;

return localizedUrl ? [localizedUrl, baseUrl] : [baseUrl, undefined];
return localizedUrl ? localizedUrl : baseUrl;
};

createTextTrackData(track, async (signal) => {
updateTextTrackStatusToPending(track);

const urls = createUrlFallbacks();
const sourceUrl = createSourceUrl();
const response = await fetchFileContent(
...urls,
sourceUrl,
{
signal,
polling: type === 'transcript' && !src,
Expand Down
14 changes: 5 additions & 9 deletions src/plugins/text-tracks-manager/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const fetchFileContent = async (
url,
fallbackUrl,
config = {}
) => {
const {
Expand All @@ -15,21 +14,21 @@ export const fetchFileContent = async (

let attempts = 0;

const attemptFetch = async (currentUrl) => {
const attemptFetch = async () => {
if (signal?.aborted) {
throw new DOMException('Aborted', 'AbortError');
}

attempts++;
onAttempt?.(attempts);

const response = await fetch(currentUrl, { signal });
const response = await fetch(url, { signal });

if (response.status === 202 && polling) {
if (attempts < maxAttempts) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
attemptFetch(url).then(resolve).catch(reject);
attemptFetch().then(resolve).catch(reject);
}, interval);

signal?.addEventListener('abort', () => {
Expand All @@ -43,10 +42,7 @@ export const fetchFileContent = async (
}

if (!response.ok) {
if (fallbackUrl) {
return attemptFetch(fallbackUrl);
}
throw new Error(`Failed fetching from ${currentUrl} with status code ${response.status}`);
throw new Error(`Failed fetching from ${url} with status code ${response.status}`);
}

const text = await response.text();
Expand All @@ -55,7 +51,7 @@ export const fetchFileContent = async (
};

try {
return await attemptFetch(url);
return await attemptFetch();
} catch (error) {
if (error.name === 'AbortError') {
console.warn('Polling aborted');
Expand Down