mirror of
https://github.com/Myxelium/Bridge-Multi.git
synced 2026-04-11 22:29:38 +00:00
Restructure; use DaisyUI
This commit is contained in:
@@ -1,86 +1,82 @@
|
||||
import { Download } from '../../shared/interfaces/download.interface'
|
||||
import { IPCEmitHandler } from '../../shared/IPCHandler'
|
||||
import { Download } from '../../../src-shared/interfaces/download.interface'
|
||||
import { ChartDownload } from './ChartDownload'
|
||||
import { DownloadQueue } from './DownloadQueue'
|
||||
|
||||
class DownloadHandler implements IPCEmitHandler<'download'> {
|
||||
event = 'download' as const
|
||||
const downloadQueue: DownloadQueue = new DownloadQueue()
|
||||
const retryWaiting: ChartDownload[] = []
|
||||
|
||||
downloadQueue: DownloadQueue = new DownloadQueue()
|
||||
currentDownload: ChartDownload = undefined
|
||||
retryWaiting: ChartDownload[] = []
|
||||
let currentDownload: ChartDownload | undefined = undefined
|
||||
|
||||
handler(data: Download) {
|
||||
switch (data.action) {
|
||||
case 'add': this.addDownload(data); break
|
||||
case 'retry': this.retryDownload(data); break
|
||||
case 'cancel': this.cancelDownload(data); break
|
||||
}
|
||||
export async function download(data: Download) {
|
||||
switch (data.action) {
|
||||
case 'add': addDownload(data); break
|
||||
case 'retry': retryDownload(data); break
|
||||
case 'cancel': cancelDownload(data); break
|
||||
}
|
||||
}
|
||||
|
||||
function addDownload(data: Download) {
|
||||
const filesHash = data.data!.driveData.filesHash // Note: using versionID would cause chart packs to download multiple times
|
||||
if (currentDownload?.hash === filesHash || downloadQueue.isDownloadingLink(filesHash)) {
|
||||
return
|
||||
}
|
||||
|
||||
private addDownload(data: Download) {
|
||||
const filesHash = data.data.driveData.filesHash // Note: using versionID would cause chart packs to download multiple times
|
||||
if (this.currentDownload?.hash == filesHash || this.downloadQueue.isDownloadingLink(filesHash)) {
|
||||
return
|
||||
}
|
||||
const newDownload = new ChartDownload(data.versionID, data.data!)
|
||||
addDownloadEventListeners(newDownload)
|
||||
if (currentDownload === undefined) {
|
||||
currentDownload = newDownload
|
||||
newDownload.beginDownload()
|
||||
} else {
|
||||
downloadQueue.push(newDownload)
|
||||
}
|
||||
}
|
||||
|
||||
const newDownload = new ChartDownload(data.versionID, data.data)
|
||||
this.addDownloadEventListeners(newDownload)
|
||||
if (this.currentDownload == undefined) {
|
||||
this.currentDownload = newDownload
|
||||
newDownload.beginDownload()
|
||||
function retryDownload(data: Download) {
|
||||
const index = retryWaiting.findIndex(download => download.versionID === data.versionID)
|
||||
if (index !== -1) {
|
||||
const retryDownload = retryWaiting.splice(index, 1)[0]
|
||||
retryDownload.displayRetrying()
|
||||
if (currentDownload === undefined) {
|
||||
currentDownload = retryDownload
|
||||
retryDownload.retry()
|
||||
} else {
|
||||
this.downloadQueue.push(newDownload)
|
||||
}
|
||||
}
|
||||
|
||||
private retryDownload(data: Download) {
|
||||
const index = this.retryWaiting.findIndex(download => download.versionID == data.versionID)
|
||||
if (index != -1) {
|
||||
const retryDownload = this.retryWaiting.splice(index, 1)[0]
|
||||
retryDownload.displayRetrying()
|
||||
if (this.currentDownload == undefined) {
|
||||
this.currentDownload = retryDownload
|
||||
retryDownload.retry()
|
||||
} else {
|
||||
this.downloadQueue.push(retryDownload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private cancelDownload(data: Download) {
|
||||
if (this.currentDownload?.versionID == data.versionID) {
|
||||
this.currentDownload.cancel()
|
||||
this.currentDownload = undefined
|
||||
this.startNextDownload()
|
||||
} else {
|
||||
this.downloadQueue.remove(data.versionID)
|
||||
}
|
||||
}
|
||||
|
||||
private addDownloadEventListeners(download: ChartDownload) {
|
||||
download.on('complete', () => {
|
||||
this.currentDownload = undefined
|
||||
this.startNextDownload()
|
||||
})
|
||||
|
||||
download.on('error', () => {
|
||||
this.retryWaiting.push(this.currentDownload)
|
||||
this.currentDownload = undefined
|
||||
this.startNextDownload()
|
||||
})
|
||||
}
|
||||
|
||||
private startNextDownload() {
|
||||
if (!this.downloadQueue.isEmpty()) {
|
||||
this.currentDownload = this.downloadQueue.shift()
|
||||
if (this.currentDownload.hasFailed) {
|
||||
this.currentDownload.retry()
|
||||
} else {
|
||||
this.currentDownload.beginDownload()
|
||||
}
|
||||
downloadQueue.push(retryDownload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const downloadHandler = new DownloadHandler()
|
||||
function cancelDownload(data: Download) {
|
||||
if (currentDownload?.versionID === data.versionID) {
|
||||
currentDownload.cancel()
|
||||
currentDownload = undefined
|
||||
startNextDownload()
|
||||
} else {
|
||||
downloadQueue.remove(data.versionID)
|
||||
}
|
||||
}
|
||||
|
||||
function addDownloadEventListeners(download: ChartDownload) {
|
||||
download.on('complete', () => {
|
||||
currentDownload = undefined
|
||||
startNextDownload()
|
||||
})
|
||||
|
||||
download.on('error', () => {
|
||||
if (currentDownload) {
|
||||
retryWaiting.push(currentDownload)
|
||||
currentDownload = undefined
|
||||
}
|
||||
startNextDownload()
|
||||
})
|
||||
}
|
||||
|
||||
function startNextDownload() {
|
||||
currentDownload = downloadQueue.shift()
|
||||
if (currentDownload) {
|
||||
if (currentDownload.hasFailed) {
|
||||
currentDownload.retry()
|
||||
} else {
|
||||
currentDownload.beginDownload()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user