task-clock
v2.2.4
Published
Task scheduler
Downloads
2
Readme
TaskClock
const { TaskClock } = require("task-clock");
const LocaleTimezoneDate = require("locale-timezone-date");
const { TaskClock } = require("task-clock");
// ...
//*
new TaskClock({
start: new Date(new Date().setSeconds(0, 0)), interval: { s: 3 }, ticks: 3,
task: (now, tick) => console.log(now.toISOString(), "task", tick)
});
// 2021-04-08T23:31:06.720Z task 1
// 2021-04-08T23:31:09.014Z task 2
// 2021-04-08T23:31:12.003Z task 3
// 2021-04-08T23:31:15.010Z done
// ...
//*/
//*
class MyClockA extends TaskClock {
task(now, tick) {
const nowMs = now.getTime();
const delay = nowMs - this.prevTick;
if (tick > 1)
console.log(now.toLocaleISOString(), `delay: ${delay} ms`);
else
console.log(now.toLocaleISOString());
this.prevTick = this.nextTick;
};
lastTick(now, tick) {
console.log(now.toLocaleISOString(), "last tick", tick);
};
get DateModel() {
return LocaleTimezoneDate;
};
};
new MyClockA({ start: new LocaleTimezoneDate().startOfDate({ ms: false }), interval: { ms: 200 }, ticks: 10 });
// 2021-04-09T01:34:37.736+0200
// 2021-04-09T01:34:37.802+0200 delay: 2 ms
// 2021-04-09T01:34:38.005+0200 delay: 5 ms
// 2021-04-09T01:34:38.206+0200 delay: 6 ms
// 2021-04-09T01:34:38.408+0200 delay: 8 ms
// 2021-04-09T01:34:38.611+0200 delay: 11 ms
// 2021-04-09T01:34:38.815+0200 delay: 15 ms
// 2021-04-09T01:34:39.001+0200 delay: 1 ms
// 2021-04-09T01:34:39.205+0200 delay: 5 ms
// 2021-04-09T01:34:39.407+0200 delay: 7 ms
// 2021-04-09T01:34:39.611+0200 last tick 11
// ...
//*/
//*
class MyClockB extends TaskClock {
#finish;
constructor(finish, lastTick) {
super({
start: new Date(new Date().setHours(0, 0, 0, 0)),
interval: { ms: 200 },
ticks: 10,
lastTick, // if condition did not meet reject on lastTick
autoStart: false // maybe i need to initialize a property
});
this.#finish = finish;
this.start();
};
task(now, tick) {
const nowMs = now.getTime();
const delay = nowMs - this.prevTick;
if (tick > 1)
console.log(now.toLocaleISOString(), `delay: ${delay} ms`, tick);
else
console.log(now.toLocaleISOString());
if (delay <= 2)
return this.stop();
this.prevTick = this.nextTick;
};
stop() {
this.lastTick = this.#finish
super.stop();
};
get DateModel() {
return LocaleTimezoneDate;
};
static promisify(resolve, reject) {
return new MyClockB(resolve, reject);
};
};
new Promise(MyClockB.promisify)
.then(() => console.log("finished promise"))
.catch(now => console.log("Rejected: ", now.toLocaleISOString()));
/*
2022-05-15T14:38:32.816+0200 1
2022-05-15T14:38:33.011+0200 2 delay: 11 ms
2022-05-15T14:38:33.214+0200 3 delay: 14 ms
2022-05-15T14:38:33.404+0200 4 delay: 4 ms
2022-05-15T14:38:33.606+0200 5 delay: 6 ms
2022-05-15T14:38:33.810+0200 6 delay: 10 ms
2022-05-15T14:38:34.015+0200 7 delay: 15 ms
2022-05-15T14:38:34.203+0200 8 delay: 3 ms
2022-05-15T14:38:34.406+0200 9 delay: 6 ms
2022-05-15T14:38:34.610+0200 10 delay: 10 ms
Rejected: 2022-05-15T14:38:34.812+0200
-----
2022-05-15T14:39:51.967+0200 1
2022-05-15T14:39:52.009+0200 2 delay: 9 ms
2022-05-15T14:39:52.212+0200 3 delay: 12 ms
2022-05-15T14:39:52.400+0200 4 delay: 0 ms
finished promise
*/
//*
const timer = new TaskClock();
// 2021-03-07T15:21:15.274Z running task 1
// 2021-03-07T15:21:16.281Z running task 2
// 2021-03-07T15:21:17.283Z running task 3
// etc...
// ...
//{somewhere in the future} running task 1057304576
timer.finish();
//{somewhere in the future} done
//*/