Skip to content

Commit 29ef0bf

Browse files
authored
dry retryAll() and upload() (#5691)
* dry retryAll() and upload() improvement of #5677 * fix bug * make it private * fix botched merge
1 parent eac0274 commit 29ef0bf

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

packages/@uppy/core/src/Uppy.ts

+30-43
Original file line numberDiff line numberDiff line change
@@ -1371,38 +1371,47 @@ export class Uppy<
13711371
this.emit('resume-all')
13721372
}
13731373

1374-
async retryAll(): Promise<UploadResult<M, B> | undefined> {
1375-
const updatedFiles = { ...this.getState().files }
1376-
const filesToRetry = Object.keys(updatedFiles).filter((file) => {
1377-
return updatedFiles[file].error
1374+
#getFilesToRetry() {
1375+
const { files } = this.getState()
1376+
return Object.keys(files).filter((file) => {
1377+
return files[file].error
13781378
})
1379+
}
13791380

1380-
filesToRetry.forEach((file) => {
1381-
const updatedFile = {
1382-
...updatedFiles[file],
1381+
async #doRetryAll(): Promise<UploadResult<M, B> | undefined> {
1382+
const filesToRetry = this.#getFilesToRetry()
1383+
1384+
const updatedFiles = { ...this.getState().files }
1385+
filesToRetry.forEach((fileID) => {
1386+
updatedFiles[fileID] = {
1387+
...updatedFiles[fileID],
13831388
isPaused: false,
13841389
error: null,
13851390
}
1386-
updatedFiles[file] = updatedFile
13871391
})
1392+
13881393
this.setState({
13891394
files: updatedFiles,
13901395
error: null,
13911396
})
13921397

1393-
this.emit('retry-all', Object.values(updatedFiles))
1398+
this.emit('retry-all', this.getFilesByIds(filesToRetry))
13941399

13951400
if (filesToRetry.length === 0) {
1396-
return Promise.resolve({
1401+
return {
13971402
successful: [],
13981403
failed: [],
1399-
})
1404+
}
14001405
}
14011406

14021407
const uploadID = this.#createUpload(filesToRetry, {
14031408
forceAllowNewUpload: true, // create new upload even if allowNewUpload: false
14041409
})
1405-
const result = await this.#runUpload(uploadID)
1410+
return this.#runUpload(uploadID)
1411+
}
1412+
1413+
async retryAll(): Promise<UploadResult<M, B> | undefined> {
1414+
const result = await this.#doRetryAll()
14061415
this.emit('complete', result!)
14071416
return result
14081417
}
@@ -2248,44 +2257,22 @@ export class Uppy<
22482257

22492258
let { files } = this.getState()
22502259

2251-
// Check if we have files with errors (for retry behavior)
2252-
const filesToRetry = Object.keys(files).filter(
2253-
(fileID) => files[fileID].error,
2254-
)
2255-
const hasFilesToRetry = filesToRetry.length > 0
2256-
2257-
// If we have files to retry, behave like retryAll() and ignore any new files
2258-
if (hasFilesToRetry) {
2259-
const updatedFiles = { ...files }
2260-
filesToRetry.forEach((fileID) => {
2261-
updatedFiles[fileID] = {
2262-
...updatedFiles[fileID],
2263-
isPaused: false,
2264-
error: null,
2265-
}
2266-
})
2260+
// retry any failed files from a previous upload() call
2261+
const filesToRetry = this.#getFilesToRetry()
2262+
if (filesToRetry.length > 0) {
2263+
const retryResult = await this.#doRetryAll() // we don't want the complete event to fire
22672264

2268-
this.setState({
2269-
files: updatedFiles,
2270-
error: null,
2271-
})
2272-
2273-
this.emit('retry-all', this.getFilesByIds(filesToRetry))
2274-
2275-
const uploadID = this.#createUpload(filesToRetry, {
2276-
forceAllowNewUpload: true, // create new upload even if allowNewUpload: false
2277-
})
2278-
const result = await this.#runUpload(uploadID)
22792265
const hasNewFiles =
22802266
this.getFiles().filter((file) => file.progress.uploadStarted == null)
22812267
.length > 0
22822268

2269+
// if no new files, make it idempotent and return
22832270
if (!hasNewFiles) {
2284-
this.emit('complete', result!)
2285-
2286-
return result
2271+
this.emit('complete', retryResult!)
2272+
return retryResult
22872273
}
2288-
files = this.getState().files
2274+
// reload files which might have changed after retry
2275+
;({ files } = this.getState())
22892276
}
22902277

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

0 commit comments

Comments
 (0)