UNPKG

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