Files
radicle-explorer/src/lib/interval.ts
T
595a81150b Add Releases and Release views
- Add the Releases list and Release page
- Add modals to create, delete and redact releases
- Register artifacts and add or remove their locations
- Download, seed and unseed artifacts from the release page
- Share one artifact node state and show its status
- Pick the release commit from a list
- Rename Stop seeding to Unseed

Co-authored-by: Daniel Norman <daniel@norman.life>
Co-authored-by: Rūdolfs Ošiņš <rudolfs@osins.org>
2026-10-08 18:21:48 +02:00

49 lines
1.1 KiB
TypeScript

const dynamicIntervals = new Map<string, ReturnType<typeof setTimeout>>();
export function dynamicInterval(
key: string,
callback: () => void,
period: number,
) {
// Clear an existing interval for this key, if any.
if (dynamicIntervals.has(key)) {
clearTimeout(dynamicIntervals.get(key));
}
// Set up a new dynamic interval.
const id = setTimeout(() => {
callback();
dynamicInterval(key, callback, period);
}, period);
dynamicIntervals.set(key, id);
}
export function resetDynamicInterval(key: string) {
clearTimeout(dynamicIntervals.get(key));
dynamicIntervals.delete(key);
}
// Unlike setInterval, a slow run never overlaps the next one.
export function poll(
task: (active: () => boolean) => Promise<void>,
period: number,
): () => void {
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const active = () => !stopped;
const run = async () => {
await task(active);
if (!stopped) {
timer = setTimeout(() => void run(), period);
}
};
void run();
return () => {
stopped = true;
clearTimeout(timer);
};
}