@weedzcokie/scheduler
Advanced tools
Comparing version 1.0.2 to 1.0.3
22
main.js
/** | ||
* export @typedef Task | ||
* @prop {number} name | ||
* @prop {number} period | ||
* @prop {() => unknown | Promise<unknown>} fn | ||
* @prop {number} timeout | ||
* @prop {() => number} getNextExecutionTime | ||
*/ | ||
@@ -12,2 +12,8 @@ | ||
export function msUntilFullMinute() { | ||
const date = new Date(Date.now() + 60000); | ||
date.setUTCSeconds(0, 0); | ||
return date.getTime() - Date.now(); | ||
} | ||
export function msUntilFullHour() { | ||
@@ -30,8 +36,6 @@ const date = new Date(Date.now() + msInHour); | ||
async function runTask(task) { | ||
const start = Date.now(); | ||
await task.fn(); | ||
const timeDrift = Date.now() - start; | ||
// Make sure we don't restart a stopped task | ||
if (task.timeout) { | ||
task.timeout = setTimeout(() => runTask(task), task.period - timeDrift); | ||
task.timeout = setTimeout(() => runTask(task), task.getNextExecutionTime()); | ||
} | ||
@@ -46,13 +50,13 @@ } | ||
* @param {Task["fn"]} fn | ||
* @param {number} period | ||
* @param {number} [offset=0] | ||
* @param {() => number} [period=() => 1000] | ||
*/ | ||
export function schedule(name, fn, period, offset = 0) { | ||
export function schedule(name, fn, period = () => 1000) { | ||
/** @type {Task} */ | ||
const task = { | ||
name, | ||
fn, | ||
period, | ||
timeout: 0, | ||
getNextExecutionTime: period, | ||
}; | ||
task.timeout = setTimeout(() => runTask(task), task.period + offset); | ||
task.timeout = setTimeout(() => runTask(task), task.getNextExecutionTime()); | ||
tasks.set(name, task); | ||
@@ -59,0 +63,0 @@ } |
{ | ||
"name": "@weedzcokie/scheduler", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3768
81