aboutsummaryrefslogtreecommitdiff
path: root/utils/delayUtils.js
blob: 6604203c4833a2143c7a466f6fd67747578e6f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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++
        functionQueue.push(id, Date.now() + time)
        functions.set(id, callback)
    }

    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