aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt
blob: f90237588b7b626be74fa685d295fba293a33675 (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
30
31
32
33
34
package at.hannibal2.skyhanni.utils

import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy
import kotlin.time.Duration

// TODO find better sync bug fix than creating a new map for each use
object DelayedRun {
    var map = mapOf<() -> Any, SimpleTimeMark>()

    fun runDelayed(duration: Duration, run: () -> Unit) {
        map = map.editCopy {
            this[run] = SimpleTimeMark.now() + duration
        }
    }

    fun runNextTick(run: () -> Unit) {
        map = map.editCopy {
            this[run] = SimpleTimeMark.now()
        }
    }

    fun checkRuns() {
        if (map.isEmpty()) return
        map = map.editCopy {
            entries.removeIf { (runnable, time) ->
                val inPast = time.isInPast()
                if (inPast) {
                    runnable()
                }
                inPast
            }
        }
    }
}