Skip to content

Commit 4d54323

Browse files
committed
limit thread amount
1 parent 1040e1d commit 4d54323

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/App.vue

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { FileObj } from './types/file';
88
import { computed, ref, watch } from 'vue';
99
import { compressFile } from './functions/imageCompression';
1010
import { comressToZip } from './functions/zipCompression';
11+
import { paginate } from './functions/paginate';
1112
import { useI18n } from './hooks/useI18n';
1213
1314
const { t } = useI18n();
@@ -20,13 +21,13 @@ const zipData = ref('');
2021
const isZipCompressing = ref(false);
2122
const anyUncompressed = computed(() => files.value.some((file) => !file.isCompressed));
2223
24+
const availableThreads = Math.max(navigator.hardwareConcurrency - 2, 1);
25+
2326
async function editFileObj(fileObj: FileObj) {
2427
try {
2528
const compressedFile = await compressFile(fileObj.file);
26-
const item = files.value.find((item) => item.id === fileObj.id);
27-
if (!item) return;
28-
item.file = compressedFile;
29-
item.isCompressed = true;
29+
fileObj.file = compressedFile;
30+
fileObj.isCompressed = true;
3031
} catch {
3132
fileObj.isTooLarge = true;
3233
}
@@ -37,9 +38,13 @@ async function compressFiles() {
3738
3839
const uncompressedFiles = files.value.filter((fileObj: FileObj) => !fileObj.isCompressed);
3940
40-
const promises = uncompressedFiles.map(editFileObj);
41+
const paginatedFileArray = paginate(uncompressedFiles, availableThreads);
42+
43+
for (const subArray of paginatedFileArray) {
44+
const promises = subArray.map(editFileObj);
45+
await Promise.all(promises);
46+
}
4147
42-
await Promise.all(promises);
4348
isCompressing.value = false;
4449
}
4550

src/components/FileItem.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ watchEffect(() => {
2121
}
2222
});
2323
24-
const fileData = computed(() => URL.createObjectURL(props.fileObj.file));
24+
const objectUrl = computed(() => URL.createObjectURL(props.fileObj.file));
2525
2626
const computeFileSize = (size: number) =>
2727
(size / (1024 * 1024)).toLocaleString(undefined, { minimumFractionDigits: 1, maximumFractionDigits: 1 }); // NoSonar this is to convert byte to MB
@@ -31,12 +31,12 @@ const computeFileSize = (size: number) =>
3131
<div class="file-item">
3232
<div class="item-set">
3333
<a
34-
:href="fileData"
34+
:href="objectUrl"
3535
rel="noopener noreferrer"
3636
target="_blank"
3737
>
3838
<img
39-
:src="fileData"
39+
:src="objectUrl"
4040
alt="Image preview"
4141
class="preview"
4242
width="200"
@@ -64,7 +64,7 @@ const computeFileSize = (size: number) =>
6464
<a
6565
:class="{ secondary: !fileObj.isCompressed }"
6666
:disabled="!fileObj.isCompressed || undefined"
67-
:href="fileObj.isCompressed ? fileData : undefined"
67+
:href="fileObj.isCompressed ? objectUrl : undefined"
6868
role="button"
6969
download
7070
>{{ t('translation.download') }}</a

src/functions/paginate.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { FileObj } from '@/types/file';
2+
3+
export function paginate(files: FileObj[], chunkSize: number): FileObj[][] {
4+
const paginatedArray: FileObj[][] = files.reduce((resultArray: FileObj[][], file, index) => {
5+
const chunkIndex = Math.floor(index / chunkSize);
6+
7+
resultArray[chunkIndex] ??= []; // start a new chunk
8+
9+
resultArray[chunkIndex].push(file);
10+
11+
return resultArray;
12+
}, []);
13+
14+
return paginatedArray;
15+
}

0 commit comments

Comments
 (0)