2022-05-11 16:29:18 +02:00
|
|
|
import { httpRequest } from '@utils';
|
|
|
|
|
|
|
|
import { ApplicationController } from './application_controller';
|
|
|
|
|
|
|
|
const DEFAULT_POLL_INTERVAL = 3000;
|
2022-12-08 13:04:44 +01:00
|
|
|
const DEFAULT_MAX_CHECKS = 20;
|
2022-05-11 16:29:18 +02:00
|
|
|
|
|
|
|
// Periodically check the state of a URL.
|
|
|
|
//
|
|
|
|
// Each time the given URL is requested, a turbo-stream 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.
|
|
|
|
export class TurboPollController extends ApplicationController {
|
|
|
|
static values = {
|
|
|
|
url: String,
|
|
|
|
maxChecks: { type: Number, default: DEFAULT_MAX_CHECKS },
|
|
|
|
interval: { type: Number, default: DEFAULT_POLL_INTERVAL }
|
|
|
|
};
|
|
|
|
|
|
|
|
declare readonly urlValue: string;
|
|
|
|
declare readonly intervalValue: number;
|
|
|
|
declare readonly maxChecksValue: number;
|
|
|
|
|
2022-07-05 13:38:00 +02:00
|
|
|
#timer?: ReturnType<typeof setTimeout>;
|
2022-05-11 16:29:18 +02:00
|
|
|
#abortController?: AbortController;
|
|
|
|
|
|
|
|
connect(): void {
|
2022-12-08 13:04:44 +01:00
|
|
|
this.schedule();
|
2022-05-11 16:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect(): void {
|
|
|
|
this.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
this.#abortController = new AbortController();
|
|
|
|
|
|
|
|
httpRequest(this.urlValue, { signal: this.#abortController.signal })
|
|
|
|
.turbo()
|
|
|
|
.catch(() => null);
|
|
|
|
}
|
|
|
|
|
2022-12-08 13:04:44 +01:00
|
|
|
private schedule(): void {
|
2022-05-11 16:29:18 +02:00
|
|
|
this.cancel();
|
2022-12-08 13:04:44 +01:00
|
|
|
this.#timer = setInterval(() => {
|
|
|
|
const nextState = this.nextState();
|
|
|
|
|
|
|
|
if (!nextState) {
|
|
|
|
this.cancel();
|
|
|
|
} else {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
}, this.intervalValue);
|
2022-05-11 16:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private cancel(): void {
|
2022-12-08 13:04:44 +01:00
|
|
|
clearInterval(this.#timer);
|
2022-05-11 16:29:18 +02:00
|
|
|
this.#abortController?.abort();
|
|
|
|
this.#abortController = window.AbortController
|
|
|
|
? new AbortController()
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
private nextState(): PollState | false {
|
|
|
|
const state = pollers.get(this.urlValue);
|
2022-12-08 13:04:44 +01:00
|
|
|
|
2022-05-11 16:29:18 +02:00
|
|
|
if (!state) {
|
|
|
|
return this.resetState();
|
|
|
|
}
|
2022-12-08 13:04:44 +01:00
|
|
|
|
2022-05-11 16:29:18 +02:00
|
|
|
state.checks += 1;
|
|
|
|
if (state.checks <= this.maxChecksValue) {
|
|
|
|
return state;
|
|
|
|
} else {
|
|
|
|
this.resetState();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private resetState(): PollState {
|
|
|
|
const state = {
|
|
|
|
interval: this.intervalValue,
|
|
|
|
checks: 0
|
|
|
|
};
|
|
|
|
pollers.set(this.urlValue, state);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PollState = {
|
|
|
|
checks: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We keep a global state of the pollers. It will be reset on every page change.
|
|
|
|
const pollers = new Map<string, PollState>();
|