node-resque
Version:
an opinionated implementation of resque in node
23 lines (19 loc) • 549 B
text/typescript
// inspired by https://github.com/tj/node-blocked
export function EventLoopDelay(limit: number, interval: number, fn: Function) {
let start = process.hrtime();
const timeout = setInterval(() => {
const delta = process.hrtime(start);
const nanosec = delta[0] * 1e9 + delta[1];
const ms = nanosec / 1e6;
const n = ms - interval;
if (n > limit) {
fn(true, Math.round(n));
} else {
fn(false, Math.round(n));
}
start = process.hrtime();
}, interval);
if (timeout.unref) {
timeout.unref();
}
}