Recv/lib/workers/abstractintervalworker.js

45 lines
594 B
JavaScript

class AbstractIntervalWorker
{
start(interval)
{
var self = this;
self.stop();
self.timer = setInterval(async () =>
{
if (self.ticking)
return;
self.ticking = true;
try
{
await self.tick();
}
catch (err)
{
console.log(err);
}
self.ticking = false;
}, interval);
}
stop()
{
var self = this;
if (self.timer)
{
clearInterval(self.timer);
self.timer = null;
}
}
/* Implement this:
async tick()
{
}
*/
}
module.exports = AbstractIntervalWorker;