From 15ef350ff4caa6718df43a8a8f7b38ffda98a497 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Wed, 8 May 2024 14:50:24 +0200 Subject: [PATCH 1/2] @uppy/core: make getObjectOfFilesPerState more efficient --- packages/@uppy/core/src/Uppy.ts | 69 ++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index 06af04cf78..8600b36bba 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 = [] + const newFiles = [] + const startedFiles = [] + const uploadStartedFiles = [] + const pausedFiles = [] + const completeFiles = [] + const erroredFiles = [] + const inProgressNotPausedFiles = [] + const processingFiles = [] + + 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, From 849aec391c704a27ee828024b5b371b2a2bf3252 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Wed, 8 May 2024 15:23:59 +0200 Subject: [PATCH 2/2] Fixup --- packages/@uppy/core/src/Uppy.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index 8600b36bba..ad056c1a27 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -708,15 +708,15 @@ export class Uppy { const { files: filesObject, totalProgress, error } = this.getState() const files = Object.values(filesObject) - const inProgressFiles = [] - const newFiles = [] - const startedFiles = [] - const uploadStartedFiles = [] - const pausedFiles = [] - const completeFiles = [] - const erroredFiles = [] - const inProgressNotPausedFiles = [] - const processingFiles = [] + 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