Download queue manager

This commit is contained in:
Bryan Ashby 2016-10-14 21:56:45 -06:00
parent 78607f8a48
commit 1ef546d569
1 changed files with 47 additions and 0 deletions

47
core/download_queue.js Normal file
View File

@ -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');
}
}
};