calculateProcessingProgress.ts 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import type { FileProcessingInfo } from '@uppy/utils/lib/FileProgress'
  2. import type { UppyFile } from '@uppy/utils/lib/UppyFile'
  3. export default function calculateProcessingProgress(
  4. files: Record<string, UppyFile<any, any>>,
  5. ): FileProcessingInfo {
  6. const values: number[] = []
  7. let mode: FileProcessingInfo['mode'] = 'indeterminate'
  8. let message: FileProcessingInfo['message']
  9. for (const { progress } of Object.values(files)) {
  10. const { preprocess, postprocess } = progress
  11. // In the future we should probably do this differently. For now we'll take the
  12. // mode and message from the first file…
  13. if (message == null && (preprocess || postprocess)) {
  14. ;({ mode, message } = preprocess || postprocess!) // eslint-disable-line @typescript-eslint/no-non-null-assertion
  15. }
  16. if (preprocess?.mode === 'determinate') values.push(preprocess.value)
  17. if (postprocess?.mode === 'determinate') values.push(postprocess.value)
  18. }
  19. const value = values.reduce((total, progressValue) => {
  20. return total + progressValue / values.length
  21. }, 0)
  22. return {
  23. mode,
  24. message,
  25. value,
  26. } as FileProcessingInfo
  27. }