2022-04-28 23:19:03 +02:00
|
|
|
import { httpRequest, delegate } from '@utils';
|
2019-05-21 14:21:55 +02:00
|
|
|
|
2020-05-07 16:31:36 +02:00
|
|
|
addEventListener('DOMContentLoaded', () => {
|
2020-01-29 12:16:38 +01:00
|
|
|
attachementPoller.deactivate();
|
|
|
|
exportPoller.deactivate();
|
2019-05-21 14:21:55 +02:00
|
|
|
|
2020-01-29 12:16:38 +01:00
|
|
|
const attachments = document.querySelectorAll('[data-attachment-poll-url]');
|
|
|
|
const exports = document.querySelectorAll('[data-export-poll-url]');
|
2019-05-21 14:21:55 +02:00
|
|
|
|
2020-01-29 12:16:38 +01:00
|
|
|
for (let { dataset } of attachments) {
|
|
|
|
attachementPoller.add(dataset.attachmentPollUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let { dataset } of exports) {
|
|
|
|
exportPoller.add(dataset.exportPollUrl);
|
2019-05-21 14:21:55 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addEventListener('attachment:update', ({ detail: { url } }) => {
|
2020-01-29 12:16:38 +01:00
|
|
|
attachementPoller.add(url);
|
|
|
|
});
|
|
|
|
|
|
|
|
addEventListener('export:update', ({ detail: { url } }) => {
|
|
|
|
exportPoller.add(url);
|
2019-05-21 14:21:55 +02:00
|
|
|
});
|
|
|
|
|
2020-04-30 15:42:29 +02:00
|
|
|
delegate('click', '[data-attachment-refresh]', (event) => {
|
2019-05-22 12:00:05 +02:00
|
|
|
event.preventDefault();
|
2020-01-29 12:16:38 +01:00
|
|
|
attachementPoller.check();
|
2019-05-22 12:00:05 +02:00
|
|
|
});
|
|
|
|
|
2020-01-08 18:15:03 +01:00
|
|
|
// Periodically check the state of a set of URLs.
|
|
|
|
//
|
|
|
|
// Each time the given URL is requested, the matching `show.js.erb` view is rendered,
|
|
|
|
// causing the state to be refreshed.
|
|
|
|
//
|
|
|
|
// This is used mainly to refresh attachments during the anti-virus check,
|
|
|
|
// but also to refresh the state of a pending spreadsheet export.
|
2020-01-29 12:16:38 +01:00
|
|
|
class RemotePoller {
|
2019-05-21 14:21:55 +02:00
|
|
|
urls = new Set();
|
|
|
|
timeout;
|
|
|
|
checks = 0;
|
|
|
|
|
|
|
|
constructor(settings = {}) {
|
2020-01-29 12:16:38 +01:00
|
|
|
this.interval = settings.interval;
|
|
|
|
this.maxChecks = settings.maxChecks;
|
2019-05-21 14:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get isEnabled() {
|
|
|
|
return this.checks <= this.maxChecks;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isActive() {
|
|
|
|
return this.timeout !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
add(url) {
|
|
|
|
if (this.isEnabled) {
|
|
|
|
if (!this.isActive) {
|
|
|
|
this.activate();
|
|
|
|
}
|
|
|
|
this.urls.add(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 12:00:05 +02:00
|
|
|
check() {
|
|
|
|
let urls = this.urls;
|
|
|
|
this.reset();
|
|
|
|
for (let url of urls) {
|
2020-04-06 16:42:54 +02:00
|
|
|
// Start the request. The JS payload in the response will update the page.
|
|
|
|
// (Errors are ignored, because background tasks shouldn't report errors to the user.)
|
2022-04-28 23:19:03 +02:00
|
|
|
httpRequest(url)
|
|
|
|
.js()
|
|
|
|
.catch(() => {});
|
2019-05-22 12:00:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 14:21:55 +02:00
|
|
|
activate() {
|
2019-05-22 12:00:05 +02:00
|
|
|
clearTimeout(this.timeout);
|
2019-05-21 14:21:55 +02:00
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.checks++;
|
2020-01-29 12:16:38 +01:00
|
|
|
this.currentInterval = this.interval * 1.5;
|
2019-05-22 12:00:05 +02:00
|
|
|
this.check();
|
2020-01-29 12:16:38 +01:00
|
|
|
}, this.currentInterval);
|
2019-05-21 14:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
this.checks = 0;
|
2020-01-29 12:16:38 +01:00
|
|
|
this.currentInterval = this.interval;
|
2019-05-21 14:21:55 +02:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.urls = new Set();
|
|
|
|
this.timeout = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 11:24:16 +01:00
|
|
|
const attachementPoller = new RemotePoller({ interval: 3000, maxChecks: 5 });
|
|
|
|
const exportPoller = new RemotePoller({ interval: 6000, maxChecks: 10 });
|