Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dry retryAll() and upload() #5691

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 23 additions & 39 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,20 +1371,25 @@ export class Uppy<
this.emit('resume-all')
}

retryAll(): Promise<UploadResult<M, B> | undefined> {
const updatedFiles = { ...this.getState().files }
const filesToRetry = Object.keys(updatedFiles).filter((file) => {
return updatedFiles[file].error
#getFilesToRetry() {
const { files } = this.getState()
return Object.keys(files).filter((file) => {
return files[file].error
})
}

filesToRetry.forEach((file) => {
const updatedFile = {
...updatedFiles[file],
async retryAll(): Promise<UploadResult<M, B> | undefined> {
const filesToRetry = this.#getFilesToRetry()

const updatedFiles = { ...this.getState().files }
filesToRetry.forEach((fileID) => {
updatedFiles[fileID] = {
...updatedFiles[fileID],
isPaused: false,
error: null,
}
updatedFiles[file] = updatedFile
})

this.setState({
files: updatedFiles,
error: null,
Expand All @@ -1393,10 +1398,10 @@ export class Uppy<
this.emit('retry-all', Object.values(updatedFiles))

if (filesToRetry.length === 0) {
return Promise.resolve({
return {
successful: [],
failed: [],
})
}
}

const uploadID = this.#createUpload(filesToRetry, {
Expand Down Expand Up @@ -2248,42 +2253,21 @@ export class Uppy<

let { files } = this.getState()

// Check if we have files with errors (for retry behavior)
const filesToRetry = Object.keys(files).filter(
(fileID) => files[fileID].error,
)
const hasFilesToRetry = filesToRetry.length > 0

// If we have files to retry, behave like retryAll() and ignore any new files
if (hasFilesToRetry) {
const updatedFiles = { ...files }
filesToRetry.forEach((fileID) => {
updatedFiles[fileID] = {
...updatedFiles[fileID],
isPaused: false,
error: null,
}
})
// retry any failed files from a previous upload() call
const filesToRetry = this.#getFilesToRetry()
if (filesToRetry.length > 0) {
const retryResult = await this.retryAll()

this.setState({
files: updatedFiles,
error: null,
})

this.emit('retry-all', Object.values(updatedFiles))

const uploadID = this.#createUpload(filesToRetry, {
forceAllowNewUpload: true, // create new upload even if allowNewUpload: false
})
const result = await this.#runUpload(uploadID)
const hasNewFiles = this.getFiles().filter(
(file) => file.progress.uploadStarted == null,
)

// if no new files, make it idempotent and return
if (!hasNewFiles) {
return result
return retryResult
}
files = this.getState().files
// reload files which might have changed after retry
;({ files } = this.getState())
}

// If no files to retry, proceed with original upload() behavior for new files
Expand Down
Loading