diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index 06af04cf78..ad056c1a27 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -688,7 +688,6 @@ export class Uppy { return ids.map((id) => this.getFile(id)) } - // TODO: remove or refactor this method. It's very inefficient getObjectOfFilesPerState(): { newFiles: UppyFile[] startedFiles: UppyFile[] @@ -708,28 +707,52 @@ export class Uppy { } { const { files: filesObject, totalProgress, error } = this.getState() const files = Object.values(filesObject) - const inProgressFiles = files.filter( - ({ progress }) => !progress.uploadComplete && progress.uploadStarted, - ) - const newFiles = files.filter((file) => !file.progress.uploadStarted) - const startedFiles = files.filter( - (file) => - file.progress.uploadStarted || - file.progress.preprocess || - file.progress.postprocess, - ) - const uploadStartedFiles = files.filter( - (file) => file.progress.uploadStarted, - ) - const pausedFiles = files.filter((file) => file.isPaused) - const completeFiles = files.filter((file) => file.progress.uploadComplete) - const erroredFiles = files.filter((file) => file.error) - const inProgressNotPausedFiles = inProgressFiles.filter( - (file) => !file.isPaused, - ) - const processingFiles = files.filter( - (file) => file.progress.preprocess || file.progress.postprocess, - ) + + const inProgressFiles: UppyFile[] = [] + const newFiles: UppyFile[] = [] + const startedFiles: UppyFile[] = [] + const uploadStartedFiles: UppyFile[] = [] + const pausedFiles: UppyFile[] = [] + const completeFiles: UppyFile[] = [] + const erroredFiles: UppyFile[] = [] + const inProgressNotPausedFiles: UppyFile[] = [] + const processingFiles: UppyFile[] = [] + + for (const file of files) { + const { progress } = file + + if (!progress.uploadComplete && progress.uploadStarted) { + inProgressFiles.push(file) + if (!file.isPaused) { + inProgressNotPausedFiles.push(file) + } + } + if (!progress.uploadStarted) { + newFiles.push(file) + } + if ( + progress.uploadStarted || + progress.preprocess || + progress.postprocess + ) { + startedFiles.push(file) + } + if (progress.uploadStarted) { + uploadStartedFiles.push(file) + } + if (file.isPaused) { + pausedFiles.push(file) + } + if (progress.uploadComplete) { + completeFiles.push(file) + } + if (file.error) { + erroredFiles.push(file) + } + if (progress.preprocess || progress.postprocess) { + processingFiles.push(file) + } + } return { newFiles,