Files
pterodactyl-panel/resources/scripts/extensions/development.ts
T

86 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-10-05 18:10:18 -04:00
import type { SiteExtensionEntry } from './registry';
export function startExtensionDevelopmentReload(
entries: readonly SiteExtensionEntry[],
reload: () => void = () => window.location.reload()
): () => void {
const active = entries.flatMap((entry) => (entry.development ? [entry.development] : []));
2026-10-08 16:53:20 -04:00
if (active.length === 0) {
return () => {};
}
2026-10-05 18:10:18 -04:00
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
let controller: AbortController | undefined;
const stop = () => {
stopped = true;
clearTimeout(timer);
controller?.abort();
window.removeEventListener('pagehide', stop);
};
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
const poll = async () => {
2026-10-08 16:53:20 -04:00
if (stopped) {
return;
}
2026-10-05 18:10:18 -04:00
if (!document.hidden) {
controller = new AbortController();
const timeout = setTimeout(() => controller?.abort(), 5000);
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
try {
const changed = await Promise.all(
active.map(async ({ url, version }) => {
try {
const response = await fetch(url, { cache: 'no-store', signal: controller?.signal });
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
if (response.status === 404) {
const index = active.findIndex((entry) => entry.url === url);
2026-10-08 16:53:20 -04:00
if (index !== -1) {
active.splice(index, 1);
}
2026-10-05 18:10:18 -04:00
return false;
}
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
const next = response.ok ? (await response.text()).trim() : '';
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return /^[a-f0-9]{64}$/.test(next) && next !== version;
} catch {
return false;
}
})
);
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
if (!stopped && changed.some(Boolean)) {
stop();
reload();
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return;
}
} finally {
clearTimeout(timeout);
}
}
2026-10-08 16:53:20 -04:00
if (active.length === 0) {
stop();
}
if (!stopped) {
2026-10-05 18:10:18 -04:00
timer = setTimeout(() => {
void poll();
}, 1500);
2026-10-08 16:53:20 -04:00
}
2026-10-05 18:10:18 -04:00
};
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
window.addEventListener('pagehide', stop);
timer = setTimeout(() => {
void poll();
}, 1500);
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return stop;
}