Parcourir la source

Merge pull request #244 from goto-bus-stop/feature/tl-assembly

transloadit: Emit event when assembly is created; add assembly data to existing events.
Artur Paikin il y a 7 ans
Parent
commit
4167ffae3f
1 fichiers modifiés avec 12 ajouts et 6 suppressions
  1. 12 6
      src/plugins/Transloadit/index.js

+ 12 - 6
src/plugins/Transloadit/index.js

@@ -109,6 +109,8 @@ module.exports = class Transloadit extends Plugin {
 
 
       this.core.setState({ files })
       this.core.setState({ files })
 
 
+      this.core.emit('transloadit:assembly', assembly, Object.keys(filesToUpload))
+
       return this.connectSocket(assembly)
       return this.connectSocket(assembly)
     }).then(() => {
     }).then(() => {
       this.core.log('Transloadit: Created assembly')
       this.core.log('Transloadit: Created assembly')
@@ -136,7 +138,7 @@ module.exports = class Transloadit extends Plugin {
     }
     }
   }
   }
 
 
-  onFileUploadComplete (uploadedFile) {
+  onFileUploadComplete (assemblyId, uploadedFile) {
     const file = this.findFile(uploadedFile)
     const file = this.findFile(uploadedFile)
     this.updateState({
     this.updateState({
       files: Object.assign({}, this.state.files, {
       files: Object.assign({}, this.state.files, {
@@ -146,10 +148,10 @@ module.exports = class Transloadit extends Plugin {
         }
         }
       })
       })
     })
     })
-    this.core.bus.emit('transloadit:upload', uploadedFile)
+    this.core.bus.emit('transloadit:upload', uploadedFile, this.getAssembly(assemblyId))
   }
   }
 
 
-  onResult (stepName, result) {
+  onResult (assemblyId, stepName, result) {
     const file = this.state.files[result.original_id]
     const file = this.state.files[result.original_id]
     // The `file` may not exist if an import robot was used instead of a file upload.
     // The `file` may not exist if an import robot was used instead of a file upload.
     result.localId = file ? file.id : null
     result.localId = file ? file.id : null
@@ -157,7 +159,7 @@ module.exports = class Transloadit extends Plugin {
     this.updateState({
     this.updateState({
       results: this.state.results.concat(result)
       results: this.state.results.concat(result)
     })
     })
-    this.core.bus.emit('transloadit:result', stepName, result)
+    this.core.bus.emit('transloadit:result', stepName, result, this.getAssembly(assemblyId))
   }
   }
 
 
   connectSocket (assembly) {
   connectSocket (assembly) {
@@ -167,13 +169,13 @@ module.exports = class Transloadit extends Plugin {
     )
     )
     this.sockets[assembly.assembly_id] = socket
     this.sockets[assembly.assembly_id] = socket
 
 
-    socket.on('upload', this.onFileUploadComplete.bind(this))
+    socket.on('upload', this.onFileUploadComplete.bind(this, assembly.assembly_id))
     socket.on('error', (error) => {
     socket.on('error', (error) => {
       this.core.emit('transloadit:assembly-error', assembly, error)
       this.core.emit('transloadit:assembly-error', assembly, error)
     })
     })
 
 
     if (this.opts.waitForEncoding) {
     if (this.opts.waitForEncoding) {
-      socket.on('result', this.onResult.bind(this))
+      socket.on('result', this.onResult.bind(this, assembly.assembly_id))
     }
     }
 
 
     if (this.opts.waitForEncoding) {
     if (this.opts.waitForEncoding) {
@@ -303,6 +305,10 @@ module.exports = class Transloadit extends Plugin {
     this.core.removePostProcessor(this.afterUpload)
     this.core.removePostProcessor(this.afterUpload)
   }
   }
 
 
+  getAssembly (id) {
+    return this.state.assemblies[id]
+  }
+
   get state () {
   get state () {
     return this.core.state.transloadit || {}
     return this.core.state.transloadit || {}
   }
   }