|
// retoor <retoor@molodetz.nl>
|
|
|
|
import { Http } from "./Http.js";
|
|
|
|
export class JobPoller {
|
|
static run(statusUrl, options = {}) {
|
|
const intervalMs = options.intervalMs || 1500;
|
|
const maxAttempts = options.maxAttempts || 200;
|
|
return new Promise((resolve) => {
|
|
let attempts = 0;
|
|
const timer = window.setInterval(async () => {
|
|
attempts += 1;
|
|
if (attempts > maxAttempts) {
|
|
window.clearInterval(timer);
|
|
if (options.onTimeout) options.onTimeout();
|
|
resolve(null);
|
|
return;
|
|
}
|
|
let status;
|
|
try {
|
|
status = await Http.getJson(statusUrl);
|
|
} catch (error) {
|
|
return;
|
|
}
|
|
if (status.status === "done") {
|
|
window.clearInterval(timer);
|
|
if (options.onDone) options.onDone(status);
|
|
resolve(status);
|
|
} else if (status.status === "failed") {
|
|
window.clearInterval(timer);
|
|
if (options.onFailed) options.onFailed(status);
|
|
resolve(status);
|
|
}
|
|
}, intervalMs);
|
|
});
|
|
}
|
|
}
|