mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 03:23:02 +00:00
feat(DownloadAll): 并行下载
This commit is contained in:
@@ -6,6 +6,7 @@ import { toast } from 'react-toastify';
|
|||||||
export function DownloadAll() {
|
export function DownloadAll() {
|
||||||
const files = useAppSelector(selectFiles);
|
const files = useAppSelector(selectFiles);
|
||||||
const onClickDownloadAll = async () => {
|
const onClickDownloadAll = async () => {
|
||||||
|
console.time('DownloadAll'); //开始计时
|
||||||
if (Object.keys(files).length === 0) {
|
if (Object.keys(files).length === 0) {
|
||||||
toast.warning('未添加文件');
|
toast.warning('未添加文件');
|
||||||
return;
|
return;
|
||||||
@@ -20,11 +21,10 @@ export function DownloadAll() {
|
|||||||
|
|
||||||
//过滤处理失败的文件
|
//过滤处理失败的文件
|
||||||
const completeFiles = Object.values(files).filter((file) => file.state === ProcessState.COMPLETE);
|
const completeFiles = Object.values(files).filter((file) => file.state === ProcessState.COMPLETE);
|
||||||
const filesLength = Object.keys(completeFiles).length;
|
const fileCount = Object.keys(files).length;
|
||||||
|
|
||||||
//开始下载
|
//开始下载
|
||||||
let dir: FileSystemDirectoryHandle | undefined;
|
let dir: FileSystemDirectoryHandle | undefined;
|
||||||
let success = 0;
|
|
||||||
try {
|
try {
|
||||||
dir = await window.showDirectoryPicker({ mode: 'readwrite' });
|
dir = await window.showDirectoryPicker({ mode: 'readwrite' });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -34,27 +34,35 @@ export function DownloadAll() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
toast.warning('开始下载,请稍候');
|
toast.warning('开始下载,请稍候');
|
||||||
|
|
||||||
|
let success = 0;
|
||||||
|
const promises: Promise<void>[] = [];
|
||||||
for (const [_, file] of Object.entries(completeFiles)) {
|
for (const [_, file] of Object.entries(completeFiles)) {
|
||||||
if (file.state !== ProcessState.COMPLETE) {
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
return;
|
console.log(`开始下载: ${file.fileName}`);
|
||||||
}
|
const action = dir ? DownloadNew(dir, file) : DownloadOld(file);
|
||||||
try {
|
action.then(
|
||||||
if (dir) {
|
() => {
|
||||||
await DownloadNew(dir, file);
|
console.log(`成功下载: ${file.fileName}`);
|
||||||
} else {
|
success++;
|
||||||
await DownloadOld(file);
|
resolve();
|
||||||
}
|
},
|
||||||
success++;
|
(e) => {
|
||||||
} catch (e) {
|
console.error(`下载失败: ${file.fileName}`, e);
|
||||||
console.error(`下载失败: ${file.fileName}`, e);
|
toast.error(`出现错误: ${e}`);
|
||||||
toast.error(`出现错误: ${e}`);
|
reject(e);
|
||||||
}
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
promises.push(promise);
|
||||||
}
|
}
|
||||||
if (success === filesLength) {
|
try {
|
||||||
toast.success(`成功下载: ${success}/${filesLength}首`);
|
await Promise.allSettled(promises);
|
||||||
} else {
|
toast.success(`成功下载: ${success}/${fileCount}首`);
|
||||||
toast.warning(`成功下载: ${success}/${filesLength}首`);
|
} catch {
|
||||||
|
toast.warning(`成功下载: ${success}/${fileCount}首`);
|
||||||
}
|
}
|
||||||
|
console.timeEnd('DownloadAll'); //停止计时
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user