Refactor the rqbit-web file by breaking it down into components and helper functions. Improve code organization and maintainability
This commit is contained in:
parent
a5e7a5a5f5
commit
f978ad02fe
25 changed files with 1722 additions and 926 deletions
29
crates/librqbit/webui/src/helper/customSetInterval.ts
Normal file
29
crates/librqbit/webui/src/helper/customSetInterval.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Run a function with initial interval, then run it forever with the interval that the
|
||||
// callback returns.
|
||||
// Returns a callback to clear it.
|
||||
|
||||
export function customSetInterval(
|
||||
asyncCallback: () => Promise<number>,
|
||||
initialInterval: number
|
||||
): () => void {
|
||||
let timeoutId: number;
|
||||
let currentInterval: number = initialInterval;
|
||||
|
||||
const executeCallback = async () => {
|
||||
currentInterval = await asyncCallback();
|
||||
if (currentInterval === null || currentInterval === undefined) {
|
||||
throw "asyncCallback returned null or undefined";
|
||||
}
|
||||
scheduleNext();
|
||||
};
|
||||
|
||||
let scheduleNext = () => {
|
||||
timeoutId = setTimeout(executeCallback, currentInterval);
|
||||
};
|
||||
|
||||
scheduleNext();
|
||||
|
||||
return () => {
|
||||
clearTimeout(timeoutId);
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue