2024-02-18 13:25:18 +02:00
|
|
|
const scheduledExecutions: Record<string, boolean> = {};
|
2023-01-13 11:53:25 +01:00
|
|
|
|
|
|
|
/**
|
2023-06-29 22:10:13 +02:00
|
|
|
* Subsequent calls will not move the timer to the future. The first caller determines the time of execution.
|
2023-01-13 11:53:25 +01:00
|
|
|
*
|
|
|
|
* The good thing about synchronous better-sqlite3 is that this cannot interrupt transaction. The execution will be called
|
|
|
|
* only outside of a transaction.
|
|
|
|
*/
|
2024-02-18 13:25:18 +02:00
|
|
|
function scheduleExecution(name: string, milliseconds: number, cb: () => void) {
|
2023-01-13 11:53:25 +01:00
|
|
|
if (name in scheduledExecutions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduledExecutions[name] = true;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
delete scheduledExecutions[name];
|
|
|
|
|
|
|
|
cb();
|
|
|
|
}, milliseconds);
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2023-01-13 11:53:25 +01:00
|
|
|
scheduleExecution
|
2023-06-29 22:10:13 +02:00
|
|
|
};
|