UNPKG

ipull

Version:

The only file downloader you'll ever need. For node.js and the browser, CLI and library for fast and reliable file downloads.

62 lines 2.09 kB
import { EventEmitter } from "eventemitter3"; import { DownloadStatus } from "../download-file/progress-status-file.js"; import ProgressStatisticsBuilder from "../../transfer-visualize/progress-statistics-builder.js"; import { promiseWithResolvers } from "../utils/promiseWithResolvers.js"; export class DownloadEngineRemote extends EventEmitter { /** * @internal */ _downloadEndPromise = promiseWithResolvers(); /** * @internal */ _downloadStarted = false; _latestStatus = ProgressStatisticsBuilder.loadingStatusEmptyStatistics(); get status() { return this._latestStatus; } get downloadStatues() { return [this.status]; } get downloadSize() { return this._latestStatus?.totalBytes ?? 0; } get fileName() { return this._latestStatus?.fileName ?? ""; } download() { return this._downloadEndPromise.promise; } emitRemoteProgress(progress) { this._latestStatus = progress; this.emit("progress", progress); const isStatusChanged = this._latestStatus?.downloadStatus !== progress.downloadStatus; if (!isStatusChanged) { return; } switch (progress.downloadStatus) { case DownloadStatus.Active: if (this._latestStatus?.downloadStatus === DownloadStatus.Paused) { this.emit("resumed"); } else { this.emit("start"); this._downloadStarted = true; } break; case DownloadStatus.Finished: case DownloadStatus.Cancelled: this.emit("finished"); this.emit("closed"); this._downloadEndPromise.resolve(); break; case DownloadStatus.Paused: this.emit("paused"); break; } if (progress.downloadStatus === DownloadStatus.Active) { this.emit("start"); } } } //# sourceMappingURL=DownloadEngineRemote.js.map