import type { Meta, Body, UppyFile } from '@uppy/utils/lib/UppyFile' import type { DeprecatedUppyEventMap, Uppy, UppyEventMap, _UppyEventMap, } from './Uppy' /** * Create a wrapper around an event emitter with a `remove` method to remove * all events that were added using the wrapped emitter. */ export default class EventManager { #uppy: Uppy #events: Array<[keyof UppyEventMap, (...args: any[]) => void]> = [] constructor(uppy: Uppy) { this.#uppy = uppy } on>( event: K, fn: _UppyEventMap[K], ): Uppy /** @deprecated */ on>( event: K, fn: DeprecatedUppyEventMap[K], ): Uppy on>( event: K, fn: UppyEventMap[K], ): Uppy { this.#events.push([event, fn]) return this.#uppy.on(event as keyof _UppyEventMap, fn) } remove(): void { for (const [event, fn] of this.#events.splice(0)) { this.#uppy.off(event, fn) } } onFilePause( fileID: UppyFile['id'], cb: (isPaused: boolean) => void, ): void { this.on('upload-pause', (targetFileID, isPaused) => { if (fileID === targetFileID) { cb(isPaused) } }) } onFileRemove( fileID: UppyFile['id'], cb: (isPaused: UppyFile['id']) => void, ): void { this.on('file-removed', (file) => { if (fileID === file.id) cb(file.id) }) } onPause(fileID: UppyFile['id'], cb: (isPaused: boolean) => void): void { this.on('upload-pause', (targetFileID, isPaused) => { if (fileID === targetFileID) { // const isPaused = this.#uppy.pauseResume(fileID) cb(isPaused) } }) } onRetry(fileID: UppyFile['id'], cb: () => void): void { this.on('upload-retry', (targetFileID) => { if (fileID === targetFileID) { cb() } }) } onRetryAll(fileID: UppyFile['id'], cb: () => void): void { this.on('retry-all', () => { if (!this.#uppy.getFile(fileID)) return cb() }) } onPauseAll(fileID: UppyFile['id'], cb: () => void): void { this.on('pause-all', () => { if (!this.#uppy.getFile(fileID)) return cb() }) } onCancelAll( fileID: UppyFile['id'], eventHandler: UppyEventMap['cancel-all'], ): void { this.on('cancel-all', (...args) => { if (!this.#uppy.getFile(fileID)) return eventHandler(...args) }) } onResumeAll(fileID: UppyFile['id'], cb: () => void): void { this.on('resume-all', () => { if (!this.#uppy.getFile(fileID)) return cb() }) } }