Last active
May 15, 2017 19:52
-
-
Save apaleslimghost/afb330918f6b781b611fe53ef7757861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addHrtime = require('@quarterto/add-hrtime'); | |
const timers = new Set(); | |
exports.setHrTimeout = (fn, args, time) => { | |
const end = addHrtime(process.hrtime(), time); | |
const id = timers.size; | |
timers.add(id); | |
function loop() { | |
const [s, ns] = process.hrtime(end); | |
if(!timers.has(id)) return; | |
if(s >= 0 && ns >= 0) { | |
fn(...args); | |
exports.clearHrTimeout(id); | |
} else { | |
setImmediate(loop); | |
} | |
} | |
setImmediate(loop); | |
return id; | |
}; | |
exports.clearHrTimeout = id => timers.delete(id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment