diff --git a/docs/uppy-core.mdx b/docs/uppy-core.mdx index 6abb41bf09..0d507bfc15 100644 --- a/docs/uppy-core.mdx +++ b/docs/uppy-core.mdx @@ -764,11 +764,7 @@ Retry an upload (after an error, for example). Retry all uploads (after an error, for example). -#### `cancelAll({ reason: 'user' })` - -| Argument | Type | Description | -| -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `reason` | `string` | The reason for canceling. Plugins can use this to provide different cleanup behavior (Transloadit plugin cancels an Assembly if user clicked on the “cancel” button). Possible values are: `user` (default) - The user has pressed “cancel”; `unmount` - The Uppy instance has been closed programmatically | +#### `cancelAll()` Cancel all uploads, reset progress and remove all files. @@ -874,11 +870,7 @@ uppy.getPlugin('Dashboard').setOptions({ }); ``` -#### `close({ reason: 'user' })` - -| Argument | Type | Description | -| -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `reason` | `string` | The reason for canceling. Plugins can use this to provide different cleanup behavior (Transloadit plugin cancels an Assembly if user clicked on the “cancel” button). Possible values are: `user` (default) - The user has pressed “cancel”; `unmount` - The Uppy instance has been closed programmatically | +#### `destroy()` Uninstall all plugins and close down this Uppy instance. Also runs `uppy.cancelAll()` before uninstalling. @@ -1024,25 +1016,19 @@ Fired each time a file is removed. **Parameters** - `file` - The [Uppy file](#working-with-uppy-files) that was removed. -- `reason` - A string explaining why the file was removed. See - [#2301](https://github.com/transloadit/uppy/issues/2301#issue-628931176) for - details. Current reasons are: `removed-by-user` and `cancel-all`. **Example** ```js -uppy.on('file-removed', (file, reason) => { +uppy.on('file-removed', (file) => { console.log('Removed file', file); }); ``` ```js -uppy.on('file-removed', (file, reason) => { +uppy.on('file-removed', (file) => { removeFileFromUploadingCounterUI(file); - - if (reason === 'removed-by-user') { - sendDeleteRequestForFile(file); - } + sendDeleteRequestForFile(file); }); ``` @@ -1322,10 +1308,6 @@ Fired when “info” message should be hidden in the UI. See #### `cancel-all` -| Argument | Type | Description | -| -------- | -------- | ----------------------------------- | -| `reason` | `string` | See [uppy.cancelAll](####cancelAll) | - Fired when `cancelAll()` is called, all uploads are canceled, files removed and progress is reset. diff --git a/packages/@uppy/aws-s3/src/index.ts b/packages/@uppy/aws-s3/src/index.ts index 039c0fa7aa..88f346cf11 100644 --- a/packages/@uppy/aws-s3/src/index.ts +++ b/packages/@uppy/aws-s3/src/index.ts @@ -838,11 +838,9 @@ export default class AwsS3Multipart< resolve(`upload ${removed} was removed`) }) - eventManager.onCancelAll(file.id, (options) => { - if (options?.reason === 'user') { - upload.abort() - this.resetUploaderReferences(file.id, { abort: true }) - } + eventManager.onCancelAll(file.id, () => { + upload.abort() + this.resetUploaderReferences(file.id, { abort: true }) resolve(`upload ${file.id} was canceled`) }) diff --git a/packages/@uppy/companion-client/src/RequestClient.ts b/packages/@uppy/companion-client/src/RequestClient.ts index 5c5388491a..036f414a75 100644 --- a/packages/@uppy/companion-client/src/RequestClient.ts +++ b/packages/@uppy/companion-client/src/RequestClient.ts @@ -574,10 +574,8 @@ export default class RequestClient { resolve() } - const onCancelAll = ({ reason }: { reason?: string }) => { - if (reason === 'user') { - socketSend('cancel') - } + const onCancelAll = () => { + socketSend('cancel') socketAbortController?.abort?.() this.uppy.log(`upload ${file.id} was canceled`, 'info') resolve() diff --git a/packages/@uppy/core/src/Uppy.test.ts b/packages/@uppy/core/src/Uppy.test.ts index 8ba1c149f2..b0a60d90c7 100644 --- a/packages/@uppy/core/src/Uppy.test.ts +++ b/packages/@uppy/core/src/Uppy.test.ts @@ -363,7 +363,7 @@ describe('src/Core', () => { core.cancelAll() expect(coreCancelEventMock).toHaveBeenCalledWith( - { reason: 'user' }, + undefined, undefined, undefined, undefined, @@ -555,7 +555,7 @@ describe('src/Core', () => { core.destroy() expect(coreCancelEventMock).toHaveBeenCalledWith( - { reason: 'user' }, + undefined, undefined, undefined, undefined, diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index a983640a9f..cfb63c7295 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -53,8 +53,6 @@ type Processor = ( uploadID: string, ) => Promise | void -type FileRemoveReason = 'user' | 'cancel-all' | 'unmount' - type LogLevel = 'info' | 'warning' | 'error' | 'success' export type UnknownPlugin< @@ -228,7 +226,7 @@ export type NonNullableUppyOptions = Required< export interface _UppyEventMap { 'back-online': () => void - 'cancel-all': (reason: { reason?: FileRemoveReason }) => void + 'cancel-all': () => void complete: (result: UploadResult) => void error: ( error: { name: string; message: string; details?: string }, @@ -236,7 +234,7 @@ export interface _UppyEventMap { response?: UppyFile['response'], ) => void 'file-added': (file: UppyFile) => void - 'file-removed': (file: UppyFile, reason?: FileRemoveReason) => void + 'file-removed': (file: UppyFile) => void 'files-added': (files: UppyFile[]) => void 'info-hidden': () => void 'info-visible': () => void @@ -1137,7 +1135,7 @@ export class Uppy { } } - removeFiles(fileIDs: string[], reason?: FileRemoveReason): void { + removeFiles(fileIDs: string[]): void { const { files, currentUploads } = this.getState() const updatedFiles = { ...files } const updatedUploads = { ...currentUploads } @@ -1197,7 +1195,7 @@ export class Uppy { const removedFileIDs = Object.keys(removedFiles) removedFileIDs.forEach((fileID) => { - this.emit('file-removed', removedFiles[fileID], reason) + this.emit('file-removed', removedFiles[fileID]) }) if (removedFileIDs.length > 5) { @@ -1207,8 +1205,8 @@ export class Uppy { } } - removeFile(fileID: string, reason?: FileRemoveReason): void { - this.removeFiles([fileID], reason) + removeFile(fileID: string): void { + this.removeFiles([fileID]) } pauseResume(fileID: string): boolean | undefined { @@ -1305,21 +1303,18 @@ export class Uppy { return this.#runUpload(uploadID) } - cancelAll({ reason = 'user' }: { reason?: FileRemoveReason } = {}): void { - this.emit('cancel-all', { reason }) - - // Only remove existing uploads if user is canceling - if (reason === 'user') { - const { files } = this.getState() + cancelAll(): void { + this.emit('cancel-all') - const fileIDs = Object.keys(files) - if (fileIDs.length) { - this.removeFiles(fileIDs, 'cancel-all') - } + const { files } = this.getState() - this.setState(defaultUploadState) - // todo should we call this.emit('reset-progress') like we do for resetProgress? + const fileIDs = Object.keys(files) + if (fileIDs.length) { + this.removeFiles(fileIDs) } + + this.setState(defaultUploadState) + // todo should we call this.emit('reset-progress') like we do for resetProgress? } retryUpload(fileID: string): Promise | undefined> { @@ -1818,12 +1813,12 @@ export class Uppy { /** * Uninstall all plugins and close down this Uppy instance. */ - destroy({ reason }: { reason?: FileRemoveReason } | undefined = {}): void { + destroy(): void { this.log( `Closing Uppy instance ${this.opts.id}: removing all files and uninstalling plugins`, ) - this.cancelAll({ reason }) + this.cancelAll() this.#storeUnsubscribe() diff --git a/packages/@uppy/transloadit/src/index.ts b/packages/@uppy/transloadit/src/index.ts index 7d3652bb90..b851588085 100644 --- a/packages/@uppy/transloadit/src/index.ts +++ b/packages/@uppy/transloadit/src/index.ts @@ -441,20 +441,14 @@ export default class Transloadit< // TODO: this should not live inside a `file-removed` event but somewhere more deterministic. // Such as inside the function where the assembly has succeeded or cancelled. // For the use case of cancelling the assembly when needed, we should try to do that with just `cancel-all`. - const fileRemovedHandler = ( - fileRemoved: UppyFile, - reason?: string, - ) => { + const fileRemovedHandler = (fileRemoved: UppyFile) => { // If the assembly has successfully completed, we do not need these checks. // Otherwise we may cancel an assembly after it already succeeded if (assembly.status?.ok === 'ASSEMBLY_COMPLETED') { this.uppy.off('file-removed', fileRemovedHandler) return } - if (reason === 'cancel-all') { - assembly.close() - this.uppy.off('file-removed', fileRemovedHandler) - } else if (fileRemoved.id in updatedFiles) { + if (fileRemoved.id in updatedFiles) { delete updatedFiles[fileRemoved.id] const nbOfRemainingFiles = Object.keys(updatedFiles).length @@ -667,10 +661,8 @@ export default class Transloadit< /** * When all files are removed, cancel in-progress Assemblies. */ - #onCancelAll = async ({ reason }: { reason?: string } = {}) => { + #onCancelAll = async () => { try { - if (reason !== 'user') return - const { uploadsAssemblies } = this.getPluginState() const assemblyIDs = Object.values(uploadsAssemblies).flat(1) const assemblies = assemblyIDs.map((assemblyID) => diff --git a/packages/@uppy/tus/src/index.ts b/packages/@uppy/tus/src/index.ts index 86350c589c..43bda0f549 100644 --- a/packages/@uppy/tus/src/index.ts +++ b/packages/@uppy/tus/src/index.ts @@ -501,11 +501,9 @@ export default class Tus extends BasePlugin< upload.abort() }) - eventManager.onCancelAll(file.id, ({ reason } = {}) => { - if (reason === 'user') { - queuedRequest.abort() - this.resetUploaderReferences(file.id, { abort: !!upload.url }) - } + eventManager.onCancelAll(file.id, () => { + queuedRequest.abort() + this.resetUploaderReferences(file.id, { abort: !!upload.url }) resolve(`upload ${file.id} was canceled`) }) diff --git a/packages/@uppy/xhr-upload/src/index.ts b/packages/@uppy/xhr-upload/src/index.ts index 6b3124f813..546812792e 100644 --- a/packages/@uppy/xhr-upload/src/index.ts +++ b/packages/@uppy/xhr-upload/src/index.ts @@ -347,10 +347,8 @@ export default class XHRUpload< }) events.onFileRemove(file.id, () => controller.abort()) - events.onCancelAll(file.id, ({ reason }) => { - if (reason === 'user') { - controller.abort() - } + events.onCancelAll(file.id, () => { + controller.abort() }) try {