UNPKG

@uppy/transloadit

Version:

The Transloadit plugin can be used to upload files to Transloadit for all kinds of processing, such as transcoding video, resizing images, zipping/unzipping, and more

94 lines (93 loc) 3.52 kB
import Emitter from 'component-emitter'; /** * Track completion of multiple assemblies. * * Emits 'assembly-complete' when an assembly completes. * Emits 'assembly-error' when an assembly fails. * Exposes a `.promise` property that resolves when all assemblies have * completed (or failed). */ class TransloaditAssemblyWatcher extends Emitter { #assemblyIDs; #remaining; promise; #resolve; #reject; #uppy; constructor(uppy, assemblyIDs) { super(); this.#uppy = uppy; this.#assemblyIDs = assemblyIDs; this.#remaining = assemblyIDs.length; this.promise = new Promise((resolve, reject) => { this.#resolve = resolve; this.#reject = reject; }); this.#addListeners(); } /** * Are we watching this assembly ID? */ #watching(id) { return this.#assemblyIDs.indexOf(id) !== -1; } #onAssemblyComplete = (assembly) => { const assemblyId = assembly.assembly_id; if (assemblyId == null || !this.#watching(assemblyId)) { return; } this.#uppy.log(`[Transloadit] AssemblyWatcher: Got Assembly finish ${assemblyId}`); this.emit('assembly-complete', assemblyId); this.#checkAllComplete(); }; #onAssemblyCancel = (assembly) => { const assemblyId = assembly.assembly_id; if (assemblyId == null || !this.#watching(assemblyId)) { return; } this.#checkAllComplete(); }; #onAssemblyError = (assembly, error) => { const assemblyId = assembly.assembly_id; if (assemblyId == null || !this.#watching(assemblyId)) { return; } this.#uppy.log(`[Transloadit] AssemblyWatcher: Got Assembly error ${assemblyId}`); this.#uppy.log(error); this.emit('assembly-error', assemblyId, error); this.#checkAllComplete(); }; #onImportError = (assembly, fileID, error) => { const assemblyId = assembly.assembly_id; if (assemblyId == null || !this.#watching(assemblyId)) { return; } // Not sure if we should be doing something when it's just one file failing. // ATM, the only options are 1) ignoring or 2) failing the entire upload. // I think failing the upload is better than silently ignoring. // In the future we should maybe have a way to resolve uploads with some failures, // like returning an object with `{ successful, failed }` uploads. this.#onAssemblyError(assembly, error); }; #checkAllComplete() { this.#remaining -= 1; if (this.#remaining === 0) { // We're done, these listeners can be removed this.#removeListeners(); this.#resolve(); } } #removeListeners() { this.#uppy.off('transloadit:complete', this.#onAssemblyComplete); this.#uppy.off('transloadit:assembly-cancel', this.#onAssemblyCancel); this.#uppy.off('transloadit:assembly-error', this.#onAssemblyError); this.#uppy.off('transloadit:import-error', this.#onImportError); } #addListeners() { this.#uppy.on('transloadit:complete', this.#onAssemblyComplete); this.#uppy.on('transloadit:assembly-cancel', this.#onAssemblyCancel); this.#uppy.on('transloadit:assembly-error', this.#onAssemblyError); this.#uppy.on('transloadit:import-error', this.#onImportError); } } export default TransloaditAssemblyWatcher;