Skip to content

Commit

Permalink
Added try-catch after getData() starts reading
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxi-std committed Jan 24, 2025
1 parent 80cf814 commit d06f2a9
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions src/libs/OIerDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,60 +312,63 @@ const getData = async (

if (!Array.isArray(urls)) urls = [urls];

let response: Response = null;
let realUrl: string = null;
for (const url of urls) {
try {
response = await fetch(url);
realUrl = url;
if (response.ok) break;
} catch (e) {
console.error(e);
}
}
let response = await fetch(url);

Check failure on line 317 in src/libs/OIerDb.ts

View workflow job for this annotation

GitHub Actions / build

'response' is never reassigned. Use 'const' instead
let realUrl = url;

Check failure on line 318 in src/libs/OIerDb.ts

View workflow job for this annotation

GitHub Actions / build

'realUrl' is never reassigned. Use 'const' instead

if (!response.ok) continue;

let receivedSize = 0;
const chunks: Uint8Array[] = [];

const reader = response.body.getReader();
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
receivedSize += value.length;

if (setProgressPercent) {
setProgressPercent(
Math.ceil(
start + Math.min((receivedSize / size) * (end - start), end)
)
);
}
}

let receivedSize = 0;
const chunks: Uint8Array[] = [];

const reader = response.body.getReader();
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
receivedSize += value.length;

if (setProgressPercent) {
setProgressPercent(
Math.ceil(start + Math.min((receivedSize / size) * (end - start), end))
);
}
}
const chunksAll = new Uint8Array(receivedSize);
let pos = 0;
for (const chunk of chunks) {
chunksAll.set(chunk, pos);
pos += chunk.length;
}

const chunksAll = new Uint8Array(receivedSize);
let pos = 0;
for (const chunk of chunks) {
chunksAll.set(chunk, pos);
pos += chunk.length;
}
const data = new TextDecoder().decode(chunksAll);

const data = new TextDecoder().decode(chunksAll);
if (trackLabel) {
const timeUsed = performance.now() - startTime;

if (trackLabel) {
const timeUsed = performance.now() - startTime;
trackEvent('Download: ' + trackLabel, {
props: {
url: realUrl,
time:
timeUsed < 100
? Math.floor(timeUsed / 25) * 25
: Math.floor(timeUsed / 100) * 100,
},
});
}

trackEvent('Download: ' + trackLabel, {
props: {
url: realUrl,
time:
timeUsed < 100
? Math.floor(timeUsed / 25) * 25
: Math.floor(timeUsed / 100) * 100,
},
});
return data;
} catch (e) {
console.error(e);
}
}

return data;
throw new Error('Failed to fetch data');
};

export const initDb = async (setProgressPercent?: (p: number) => void) => {
Expand Down

0 comments on commit d06f2a9

Please sign in to comment.