chore(server): linting

This commit is contained in:
Roberto Tonino 2021-05-19 20:41:40 +02:00
parent 57f7f4d6db
commit 8574259115
7 changed files with 50 additions and 53 deletions

View File

@ -33,7 +33,7 @@ export function saveSettings(newSettings: any) {
}
export let queueOrder: string[] = []
export let queue: any = {}
export const queue: any = {}
export let currentJob: any = null
restoreQueueFromDisk()
@ -52,7 +52,8 @@ export async function addToQueue(dz: any, url: string, bitrate: number) {
downloadObjs.forEach((downloadObj: any) => {
// Check if element is already in queue
if (Object.keys(queue).includes(downloadObj.uuid)) throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject)
if (Object.keys(queue).includes(downloadObj.uuid))
throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject)
// Save queue status when adding something to the queue
if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue')
@ -60,10 +61,10 @@ export async function addToQueue(dz: any, url: string, bitrate: number) {
queueOrder.push(downloadObj.uuid)
fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder))
queue[downloadObj.uuid] = downloadObj.getEssentialDict()
queue[downloadObj.uuid].status = "inQueue"
queue[downloadObj.uuid].status = 'inQueue'
let savedObject = downloadObj.toDict()
savedObject.status = "inQueue"
const savedObject = downloadObj.toDict()
savedObject.status = 'inQueue'
fs.writeFileSync(configFolder + `queue${sep}${downloadObj.uuid}.json`, JSON.stringify(savedObject))
slimmedObjects.push(downloadObj.getSlimmedDict())
@ -83,9 +84,9 @@ async function startQueue(dz: any): Promise<any> {
}
currentJob = true // lock currentJob
const currentUUID: string = queueOrder.shift() || ""
const currentUUID: string = queueOrder.shift() || ''
console.log(currentUUID)
queue[currentUUID].status = "downloading"
queue[currentUUID].status = 'downloading'
const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${currentUUID}.json`).toString())
let downloadObject: any
switch (currentItem.__type__) {
@ -107,14 +108,14 @@ async function startQueue(dz: any): Promise<any> {
if (!downloadObject.isCanceled) {
// Set status
if (downloadObject.failed == downloadObject.size) {
queue[currentUUID].status = "failed"
queue[currentUUID].status = 'failed'
} else if (downloadObject.failed > 0) {
queue[currentUUID].status = "withErrors"
queue[currentUUID].status = 'withErrors'
} else {
queue[currentUUID].status = "completed"
queue[currentUUID].status = 'completed'
}
let savedObject = downloadObject.getSlimmedDict()
const savedObject = downloadObject.getSlimmedDict()
savedObject.status = queue[currentUUID].status
// Save queue status
@ -130,15 +131,17 @@ async function startQueue(dz: any): Promise<any> {
export function cancelDownload(uuid: string) {
if (Object.keys(queue).includes(uuid)) {
switch (queue[uuid].status) {
case "downloading":
case 'downloading':
currentJob.downloadObject.isCanceled = true
listener.send("cancellingCurrentItem", uuid)
listener.send('cancellingCurrentItem', uuid)
break
case "inQueue":
case 'inQueue':
queueOrder.splice(queueOrder.indexOf(uuid), 1)
fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder))
// break
default:
listener.send("removedFromQueue", uuid)
// This gets called even in the 'inQueue' case. Is this the expected behaviour? If no, de-comment the break
listener.send('removedFromQueue', uuid)
break
}
fs.unlinkSync(configFolder + `queue${sep}${uuid}.json`)
@ -150,26 +153,26 @@ export function cancelAllDownloads(){
queueOrder = []
let currentItem: string | null = null
Object.values(queue).forEach((downloadObject: any) => {
if (downloadObject.status == "downloading"){
if (downloadObject.status == 'downloading') {
currentJob.downloadObject.isCanceled = true
listener.send("cancellingCurrentItem", downloadObject.uuid)
listener.send('cancellingCurrentItem', downloadObject.uuid)
currentItem = downloadObject.uuid
}
fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`)
delete queue[downloadObject.uuid]
})
fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder))
listener.send("removedAllDownloads", currentItem)
listener.send('removedAllDownloads', currentItem)
}
export function clearCompletedDownloads() {
Object.values(queue).forEach((downloadObject: any) => {
if (downloadObject.status === "completed"){
if (downloadObject.status === 'completed') {
fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`)
delete queue[downloadObject.uuid]
}
})
listener.send("removedFinishedDownloads")
listener.send('removedFinishedDownloads')
}
export function restoreQueueFromDisk() {
@ -194,7 +197,7 @@ export function restoreQueueFromDisk(){
break
}
queue[downloadObject.uuid] = downloadObject.getEssentialDict()
queue[downloadObject.uuid].status = "inQueue"
queue[downloadObject.uuid].status = 'inQueue'
} else {
queue[currentItem.uuid] = currentItem
}

View File

@ -1,17 +1,16 @@
// @ts-expect-error
import { Deezer } from 'deezer-js'
// import { Deezer } from 'deezer-js'
import { ApiHandler } from '../../../types'
import { queueOrder, queue, currentJob } from '../../../main'
const path: ApiHandler['path'] = '/getQueue'
let homeCache: any
// let homeCache: any
const handler: ApiHandler['handler'] = async (req, res) => {
let result = {
const handler: ApiHandler['handler'] = (req, res) => {
const result = {
queue,
order: queueOrder,
currentItem: "",
currentItem: ''
}
if (currentJob) result.currentItem = currentJob.downloadObject.uuid

View File

@ -18,7 +18,7 @@ router.get('/connect', (req, res) => {
if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer()
const dz = sessionDZ[req.session.id]
let result: any = {
const result: any = {
update: {
currentCommit: 'testing',
latestCommit: 'testing',

View File

@ -3,9 +3,4 @@ import removeFinishedDownloads from './removeFinishedDownloads'
import removeFromQueue from './removeFromQueue'
import cancelAllDownloads from './cancelAllDownloads'
export default [
saveSettings,
removeFinishedDownloads,
removeFromQueue,
cancelAllDownloads
]
export default [saveSettings, removeFinishedDownloads, removeFromQueue, cancelAllDownloads]