AssemblyWatcher.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const Emitter = require('component-emitter')
  2. /**
  3. * Track completion of multiple assemblies.
  4. *
  5. * Emits 'assembly-complete' when an assembly completes.
  6. * Emits 'assembly-error' when an assembly fails.
  7. * Exposes a `.promise` property that resolves when all assemblies have
  8. * completed (or failed).
  9. */
  10. class TransloaditAssemblyWatcher extends Emitter {
  11. constructor (uppy, assemblyIDs) {
  12. super()
  13. this._uppy = uppy
  14. this._assemblyIDs = assemblyIDs
  15. this._remaining = assemblyIDs.length
  16. this.promise = new Promise((resolve, reject) => {
  17. this._resolve = resolve
  18. this._reject = reject
  19. })
  20. this._onAssemblyComplete = this._onAssemblyComplete.bind(this)
  21. this._onAssemblyCancel = this._onAssemblyCancel.bind(this)
  22. this._onAssemblyError = this._onAssemblyError.bind(this)
  23. this._onImportError = this._onImportError.bind(this)
  24. this._addListeners()
  25. }
  26. /**
  27. * Are we watching this assembly ID?
  28. */
  29. _watching (id) {
  30. return this._assemblyIDs.indexOf(id) !== -1
  31. }
  32. _onAssemblyComplete (assembly) {
  33. if (!this._watching(assembly.assembly_id)) {
  34. return
  35. }
  36. this._uppy.log(`[Transloadit] AssemblyWatcher: Got Assembly finish ${assembly.assembly_id}`)
  37. this.emit('assembly-complete', assembly.assembly_id)
  38. this._checkAllComplete()
  39. }
  40. _onAssemblyCancel (assembly) {
  41. if (!this._watching(assembly.assembly_id)) {
  42. return
  43. }
  44. this._checkAllComplete()
  45. }
  46. _onAssemblyError (assembly, error) {
  47. if (!this._watching(assembly.assembly_id)) {
  48. return
  49. }
  50. this._uppy.log(`[Transloadit] AssemblyWatcher: Got Assembly error ${assembly.assembly_id}`)
  51. this._uppy.log(error)
  52. this.emit('assembly-error', assembly.assembly_id, error)
  53. this._checkAllComplete()
  54. }
  55. _onImportError (assembly, fileID, error) {
  56. if (!this._watching(assembly.assembly_id)) {
  57. return
  58. }
  59. // Not sure if we should be doing something when it's just one file failing.
  60. // ATM, the only options are 1) ignoring or 2) failing the entire upload.
  61. // I think failing the upload is better than silently ignoring.
  62. // In the future we should maybe have a way to resolve uploads with some failures,
  63. // like returning an object with `{ successful, failed }` uploads.
  64. this._onAssemblyError(assembly, error)
  65. }
  66. _checkAllComplete () {
  67. this._remaining -= 1
  68. if (this._remaining === 0) {
  69. // We're done, these listeners can be removed
  70. this._removeListeners()
  71. this._resolve()
  72. }
  73. }
  74. _removeListeners () {
  75. this._uppy.off('transloadit:complete', this._onAssemblyComplete)
  76. this._uppy.off('transloadit:assembly-cancel', this._onAssemblyCancel)
  77. this._uppy.off('transloadit:assembly-error', this._onAssemblyError)
  78. this._uppy.off('transloadit:import-error', this._onImportError)
  79. }
  80. _addListeners () {
  81. this._uppy.on('transloadit:complete', this._onAssemblyComplete)
  82. this._uppy.on('transloadit:assembly-cancel', this._onAssemblyCancel)
  83. this._uppy.on('transloadit:assembly-error', this._onAssemblyError)
  84. this._uppy.on('transloadit:import-error', this._onImportError)
  85. }
  86. }
  87. module.exports = TransloaditAssemblyWatcher