1 | const Emitter = require('component-emitter')
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | class 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 |
|
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 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | this._onAssemblyError(assembly, error)
|
83 | }
|
84 |
|
85 | _checkAllComplete () {
|
86 | this._remaining -= 1
|
87 | if (this._remaining === 0) {
|
88 |
|
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 |
|
109 | module.exports = TransloaditAssemblyWatcher
|