Quellcode durchsuchen

@uppy/transloadit: close assembly if upload is cancelled (#3591)

Fixes: https://github.com/transloadit/uppy/issues/3156
Antoine du Hamel vor 3 Jahren
Ursprung
Commit
aba7b29662

+ 27 - 0
e2e/cypress/integration/dashboard-transloadit.spec.ts

@@ -15,4 +15,31 @@ describe('Dashboard with Transloadit', () => {
 
     cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
   })
+
+  it('should close assembly polling when cancelled', () => {
+    cy.get('@file-input').attachFile(['images/cat.jpg', 'images/traffic.jpg'])
+    cy.get('.uppy-StatusBar-actionBtn--upload').click()
+
+    cy.intercept({
+      method: 'GET',
+      url: '/assemblies/*',
+    }).as('assemblyPolling')
+    cy.intercept(
+      { method: 'PATCH', pathname: '/files/*', times: 1 },
+      { statusCode: 204, body: {} },
+    )
+    cy.intercept(
+      { method: 'DELETE', pathname: '/resumable/files/*', times: 1 },
+      { statusCode: 204, body: {} },
+    )
+    cy.wait('@assemblyPolling')
+    cy.window().then(({ uppy }) => {
+      expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
+    })
+    cy.get('button[data-cy=cancel]').click()
+
+    cy.window().then(({ uppy }) => {
+      expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
+    })
+  })
 })

+ 2 - 2
e2e/tsconfig.json

@@ -1,7 +1,7 @@
 {
   "compilerOptions": {
-    "target": "es5",
-    "lib": ["es5", "dom"],
+    "target": "es2020",
+    "lib": ["es2020", "dom"],
     "types": ["cypress", "cypress-file-upload"]
   },
   "include": ["cypress/**/*.ts"]

+ 1 - 0
packages/@uppy/status-bar/src/Components.js

@@ -85,6 +85,7 @@ function CancelBtn (props) {
       title={i18n('cancel')}
       aria-label={i18n('cancel')}
       onClick={() => uppy.cancelAll()}
+      data-cy="cancel"
       data-uppy-super-focusable
     >
       <svg

+ 1 - 0
packages/@uppy/transloadit/src/Assembly.js

@@ -265,6 +265,7 @@ class TransloaditAssembly extends Emitter {
       this.socket = null
     }
     clearInterval(this.pollInterval)
+    this.pollInterval = null
   }
 }
 

+ 13 - 0
packages/@uppy/transloadit/src/index.js

@@ -217,6 +217,19 @@ module.exports = class Transloadit extends BasePlugin {
           ...updatedFiles,
         },
       })
+      const fileRemovedHandler = (fileRemoved, reason) => {
+        if (reason === 'cancel-all') {
+          assembly.close()
+          this.uppy.off(fileRemovedHandler)
+        } else if (fileRemoved.id in updatedFiles) {
+          delete updatedFiles[fileRemoved.id]
+          if (Object.keys(updatedFiles).length === 0) {
+            assembly.close()
+            this.uppy.off(fileRemovedHandler)
+          }
+        }
+      }
+      this.uppy.on('file-removed', fileRemovedHandler)
 
       this.uppy.emit('transloadit:assembly-created', status, fileIDs)