diff options
Diffstat (limited to 'src/utils/delayUtils.js')
-rw-r--r-- | src/utils/delayUtils.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/utils/delayUtils.js b/src/utils/delayUtils.js new file mode 100644 index 0000000..3ec6d95 --- /dev/null +++ b/src/utils/delayUtils.js @@ -0,0 +1,29 @@ +const { default: FlatQueue } = require("../datastructures/flatqueue") + +if (!global.delayThingSoopy) { + + let functionQueue = new FlatQueue() + let functions = new Map() + let functionId = 0 + + function delay(time, callback) { + let id = functionId++ + functions.set(id, callback) + functionQueue.push(id, Date.now() + time) + } + + register("tick", () => { + while (functionQueue.length > 0 && Date.now() > functionQueue.peekValue()) { + let id = functionQueue.pop() + + functions.get(id)() + functions.delete(id) + } + }) + + global.delayThingSoopy = { + delay: delay + } +} + +module.exports = global.delayThingSoopy |