nopaque/app/static/js/polls.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

var subscribers = {"corpora": [], "jobs": []};
function getCorpora() {
fetch("/api/v1.0/corpora")
.then(function(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
return Promise.reject(new Error(response.statusText));
}
})
.then(function(response) {
return response.json();
})
.then(function(data) {
if (JSON.stringify(corpora) != JSON.stringify(data)) {
corpora = data;
for (subscriber of subscribers.corpora) {
subscriber();
}
}
})
.catch(function(error) {
console.log('Request failed', error);
});
}
function getJobs() {
fetch("/api/v1.0/jobs")
.then(function(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
return Promise.reject(new Error(response.statusText));
}
})
.then(function(response) {
return response.json();
})
.then(function(json) {
var delta = jsondiffpatch.diff(jobs, json);
if (delta) {
jobs = json;
for (subscriber of subscribers.jobs) {
subscriber(delta);
}
}
})
.catch(function(error) {
console.log('Request failed', error);
});
}
setInterval(getCorpora, 5000);
setInterval(getJobs, 5000);