improved exist check

This commit is contained in:
Danny Coates 2017-08-13 18:44:59 -07:00
parent e1137db946
commit 43fa551a64
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB

View File

@ -13,16 +13,20 @@ document.addEventListener('DOMContentLoaded', function() {
fileList = document.getElementById('file-list'); fileList = document.getElementById('file-list');
toggleHeader(); toggleHeader();
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
for (let file of storage.files) { Promise.all(
const id = file.fileId; storage.files.map(file => {
checkExistence(id).then(exists => { const id = file.fileId;
if (exists) { return checkExistence(id).then(exists => {
addFile(storage.getFileById(id)); if (exists) {
} else { addFile(storage.getFileById(id));
storage.remove(id); } else {
} storage.remove(id);
}); }
} });
})
)
.catch(err => console.error(err))
.then(toggleHeader);
}); });
function toggleHeader() { function toggleHeader() {
@ -209,11 +213,14 @@ async function checkExistence(id) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
resolve(xhr.status === 200); resolve(xhr.status === 200);
} }
}; };
xhr.onerror = reject;
xhr.ontimeout = reject;
xhr.open('get', '/exists/' + id); xhr.open('get', '/exists/' + id);
xhr.timeout = 2000;
xhr.send(); xhr.send();
}); });
} }