Explorar o código

@uppy/core: remove `reason` (#5159)

Antoine du Hamel hai 11 meses
pai
achega
f72138ff4b

+ 5 - 23
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.
 

+ 3 - 5
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`)
       })
 

+ 2 - 4
packages/@uppy/companion-client/src/RequestClient.ts

@@ -574,10 +574,8 @@ export default class RequestClient<M extends Meta, B extends Body> {
           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()

+ 2 - 2
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,

+ 17 - 22
packages/@uppy/core/src/Uppy.ts

@@ -53,8 +53,6 @@ type Processor = (
   uploadID: string,
 ) => Promise<unknown> | void
 
-type FileRemoveReason = 'user' | 'cancel-all' | 'unmount'
-
 type LogLevel = 'info' | 'warning' | 'error' | 'success'
 
 export type UnknownPlugin<
@@ -228,7 +226,7 @@ export type NonNullableUppyOptions<M extends Meta, B extends Body> = Required<
 
 export interface _UppyEventMap<M extends Meta, B extends Body> {
   'back-online': () => void
-  'cancel-all': (reason: { reason?: FileRemoveReason }) => void
+  'cancel-all': () => void
   complete: (result: UploadResult<M, B>) => void
   error: (
     error: { name: string; message: string; details?: string },
@@ -236,7 +234,7 @@ export interface _UppyEventMap<M extends Meta, B extends Body> {
     response?: UppyFile<M, B>['response'],
   ) => void
   'file-added': (file: UppyFile<M, B>) => void
-  'file-removed': (file: UppyFile<M, B>, reason?: FileRemoveReason) => void
+  'file-removed': (file: UppyFile<M, B>) => void
   'files-added': (files: UppyFile<M, B>[]) => void
   'info-hidden': () => void
   'info-visible': () => void
@@ -1137,7 +1135,7 @@ export class Uppy<M extends Meta, B extends Body> {
     }
   }
 
-  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<M extends Meta, B extends Body> {
 
     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<M extends Meta, B extends Body> {
     }
   }
 
-  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<M extends Meta, B extends Body> {
     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<UploadResult<M, B> | undefined> {
@@ -1818,12 +1813,12 @@ export class Uppy<M extends Meta, B extends Body> {
   /**
    * 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()
 

+ 3 - 11
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<M, B>,
-          reason?: string,
-        ) => {
+        const fileRemovedHandler = (fileRemoved: UppyFile<M, B>) => {
           // 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) =>

+ 3 - 5
packages/@uppy/tus/src/index.ts

@@ -501,11 +501,9 @@ export default class Tus<M extends Meta, B extends Body> 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`)
       })
 

+ 2 - 4
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 {