feat: 并发限流上传

This commit is contained in:
mozzie 2023-08-29 17:28:13 +08:00
parent 56f2303fa8
commit 09bcfb81b4
3 changed files with 51 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import { Typography } from "antd";
import { useState } from "react";
import { User } from "@@/domain/User/entities/User";
import { limitConcurrency } from "./limitConcurrency";
import { useProgress } from "./useProgress";
const { Text } = Typography;
@ -27,6 +28,8 @@ const openOHIFViewer = (
export const DicomUpload = (props: DicomUploadProps) => {
const { UploadInput, fileCalculator, studys, isLoading } = useDicomUploader();
const { ProgressModal, showProgress, hideProgress, updatePercent } =
useProgress();
const { dicomDomainService, userDomainService } = useDomain();
const { dcmFileNum, totalFileNum, dcmFileSize } = fileCalculator;
const [selectRows, setSelectedRows] = useState<Study[]>([]);
@ -48,9 +51,16 @@ export const DicomUpload = (props: DicomUploadProps) => {
openOHIFViewer(StudyInstanceUID, SeriesInstanceUID);
} else {
const uploadFunc = (f: File) => () => dicomDomainService.upload2Pacs(f);
limitConcurrency(series.subs.map(uploadFunc), 10, (completed, total) => {
showProgress();
limitConcurrency(
series.subs.map(uploadFunc),
10,
(completed, total) => {
console.log(`${completed} out of ${total} tasks completed.`);
});
updatePercent(Math.floor((completed / total) * 100));
},
() => hideProgress()
);
}
};
@ -127,6 +137,7 @@ export const DicomUpload = (props: DicomUploadProps) => {
}))}
/>
</Modal>
<ProgressModal />
</div>
);
};

View File

@ -1,16 +1,10 @@
type PromiseFunction = () => Promise<any>;
/**
*
* @param {Promise<Function>} tasks promise请求函数
* @param {number} maxConcurrency
* @param {} onProgress
* @returns
*/
export async function limitConcurrency(
tasks: PromiseFunction[],
maxConcurrency: number,
onProgress?: (completed: number, total: number) => void
onProgress?: (completed: number, total: number) => void,
onComplete?: () => void
): Promise<any[]> {
const results: any[] = new Array(tasks.length);
let currentIndex = 0;
@ -24,6 +18,9 @@ export async function limitConcurrency(
if (index >= tasks.length) {
if (results.length === tasks.length) {
resolve(results);
if (onComplete) {
onComplete();
}
}
return;
}

View File

@ -0,0 +1,32 @@
import { useState, useCallback } from "react";
import { Modal, Progress } from "antd";
export const useProgress = () => {
const [visible, setVisible] = useState(false);
const [percent, setPercent] = useState(0);
const showProgress = useCallback(() => {
setVisible(true);
}, []);
const hideProgress = useCallback(() => {
setVisible(false);
}, []);
const updatePercent = useCallback((newPercent: number) => {
setPercent(newPercent);
}, []);
const ProgressModal = () => (
<Modal open={visible} footer={null} closable={false}>
<Progress percent={percent} />
</Modal>
);
return {
ProgressModal,
showProgress,
hideProgress,
updatePercent,
};
};