From 1ef546d5692cda56c9b83723473a45bdbc62704b Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Fri, 14 Oct 2016 21:56:45 -0600 Subject: [PATCH] Download queue manager --- core/download_queue.js | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 core/download_queue.js diff --git a/core/download_queue.js b/core/download_queue.js new file mode 100644 index 00000000..6b909643 --- /dev/null +++ b/core/download_queue.js @@ -0,0 +1,47 @@ +/* jslint node: true */ +'use strict'; + +const FileEntry = require('./file_entry.js'); + +module.exports = class DownloadQueue { + constructor(user) { + this.user = user; + + this.user.downloadQueue = this.user.downloadQueue || []; + } + + toggle(fileEntry) { + if(this.isQueued(fileEntry)) { + this.user.downloadQueue = this.user.downloadQueue.filter(e => fileEntry.fileId !== e.fileId); + } else { + this.add(fileEntry); + } + } + + add(fileEntry) { + this.user.downloadQueue.push({ + fileId : fileEntry.fileId, + areaTag : fileEntry.areaTag, + fileName : fileEntry.fileName, + byteSize : fileEntry.meta.byteSize || 0, + }); + } + + isQueued(entryOrId) { + if(entryOrId instanceof FileEntry) { + entryOrId = entryOrId.fileId; + } + + return this.user.downloadQueue.find(e => entryOrId === e.fileId) ? true : false; + } + + toProperty() { return JSON.stringify(this.user.downloadQueue); } + + loadFromProperty(prop) { + try { + this.user.downloadQueue = JSON.parse(prop); + } catch(e) { + this.user.log.error( { error : e.message, property : prop }, 'Failed parsing download queue property'); + } + } +};