mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-13 05:50:49 +08:00
- 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>
49 lines
1.1 KiB
TypeScript
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);
|
|
};
|
|
}
|