UNPKG

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