blob: eb83bce3aff256b171802dd44d48d78d2d807257 (
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
35
36
37
38
|
package at.hannibal2.skyhanni.utils
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.CollectionUtils.drainTo
import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.time.Duration
object DelayedRun {
private val tasks = mutableListOf<Pair<() -> Any, SimpleTimeMark>>()
private val futureTasks = ConcurrentLinkedQueue<Pair<() -> Any, SimpleTimeMark>>()
fun runDelayed(duration: Duration, run: () -> Unit): SimpleTimeMark {
val time = SimpleTimeMark.now() + duration
futureTasks.add(Pair(run, time))
return time
}
/** Runs in the next full Tick so the delay is between 50ms to 100ms**/
fun runNextTick(run: () -> Unit) {
futureTasks.add(Pair(run, SimpleTimeMark.farPast()))
}
fun checkRuns() {
tasks.removeIf { (runnable, time) ->
val inPast = time.isInPast()
if (inPast) {
try {
runnable()
} catch (e: Exception) {
ErrorManager.logErrorWithData(e, "DelayedRun task crashed while executing")
}
}
inPast
}
futureTasks.drainTo(tasks)
}
}
|