Browse Source

Transloadit: make assembly finish events work with multiple simultaneous uploads

Renée Kooi 8 years ago
parent
commit
38803bee56
1 changed files with 35 additions and 25 deletions
  1. 35 25
      src/plugins/Transloadit/index.js

+ 35 - 25
src/plugins/Transloadit/index.js

@@ -171,14 +171,15 @@ module.exports = class Transloadit extends Plugin {
       this.socket.on('result', this.onResult.bind(this))
     }
 
-    this.assemblyReady = new Promise((resolve, reject) => {
-      if (this.opts.waitForEncoding) {
-        this.socket.on('finished', resolve)
-      } else if (this.opts.waitForMetadata) {
-        this.socket.on('metadata', resolve)
-      }
-      this.socket.on('error', reject)
-    })
+    if (this.opts.waitForEncoding) {
+      this.socket.on('finished', () => {
+        this.core.emit('transloadit:complete', assembly)
+      })
+    } else if (this.opts.waitForMetadata) {
+      this.socket.on('metadata', () => {
+        this.core.emit('transloadit:complete', assembly)
+      })
+    }
 
     return new Promise((resolve, reject) => {
       this.socket.on('connect', resolve)
@@ -212,31 +213,40 @@ module.exports = class Transloadit extends Plugin {
       return
     }
 
+    // Pick a file that is part of this assembly...
     const fileID = fileIDs[0]
     const file = this.core.state.files[fileID]
-    const assembly = this.state.assemblies[file.assembly]
 
     this.core.emit('informer', this.opts.locale.strings.encoding, 'info', 0)
-    return this.assemblyReady.then(() => {
-      return this.client.getAssemblyStatus(assembly.assembly_ssl_url)
-    }).then((assembly) => {
-      this.updateState({
-        assemblies: Object.assign({}, this.state.assemblies, {
-          [assembly.assembly_id]: assembly
+    const onAssemblyFinished = (assembly) => {
+      // An assembly for a different upload just finished. We can ignore it.
+      if (assembly.assembly_id !== file.transloadit.assembly) {
+        return
+      }
+      // Remove this handler once we find the assembly we needed.
+      this.core.emitter.off('transloadit:complete', onAssemblyFinished)
+
+      return this.client.getAssemblyStatus(assembly.assembly_ssl_url).then((assembly) => {
+        this.updateState({
+          assemblies: Object.assign({}, this.state.assemblies, {
+            [assembly.assembly_id]: assembly
+          })
         })
-      })
 
-      // TODO set the `file.uploadURL` to a result?
-      // We will probably need an option here so the plugin user can tell us
-      // which result to pick…?
+        // TODO set the `file.uploadURL` to a result?
+        // We will probably need an option here so the plugin user can tell us
+        // which result to pick…?
 
-      this.core.emit('informer:hide')
-    }).catch((err) => {
-      // Always hide the Informer
-      this.core.emit('informer:hide')
+        this.core.emit('informer:hide')
+      }).catch((err) => {
+        // Always hide the Informer
+        this.core.emit('informer:hide')
 
-      throw err
-    })
+        throw err
+      })
+    }
+
+    this.core.on('transloadit:complete', onAssemblyFinished)
   }
 
   install () {