Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: DelayedRun sync #746

Merged
merged 6 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
package at.hannibal2.skyhanni.utils

import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy
import at.hannibal2.skyhanni.utils.LorenzUtils.drainTo
import java.util.concurrent.ConcurrentLinkedQueue
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>()
private val tasks = mutableListOf<Pair<() -> Any, SimpleTimeMark>>()
private val futureTasks = ConcurrentLinkedQueue<Pair<() -> Any, SimpleTimeMark>>()

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

/** Runs in the next full Tick so the delay is between 50ms to 100ms**/
fun runNextTick(run: () -> Unit) {
Thunderblade73 marked this conversation as resolved.
Show resolved Hide resolved
map = map.editCopy {
this[run] = SimpleTimeMark.now()
}
futureTasks.add(Pair(run, SimpleTimeMark.farPast()))
}

fun checkRuns() {
if (map.isEmpty()) return
map = map.editCopy {
entries.removeIf { (runnable, time) ->
val inPast = time.isInPast()
if (inPast) {
runnable()
}
inPast
tasks.removeIf { (runnable, time) ->
val inPast = time.isInPast()
if (inPast) {
runnable()
}
inPast
}
futureTasks.drainTo(tasks)
Thunderblade73 marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 9 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import java.util.Timer
import java.util.TimerTask
import java.util.WeakHashMap
import java.util.regex.Matcher
import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KProperty
Expand Down Expand Up @@ -340,7 +341,7 @@ object LorenzUtils {
hover: List<String>,
command: String? = null,
prefix: Boolean = true,
prefixColor: String = "§e"
prefixColor: String = "§e",
) {
val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else ""
val text = ChatComponentText(msgPrefix + message)
Expand Down Expand Up @@ -430,7 +431,7 @@ object LorenzUtils {
prefix: String,
getName: (T) -> String,
isCurrent: (T) -> Boolean,
crossinline onChange: (T) -> Unit
crossinline onChange: (T) -> Unit,
) = buildList {
add(prefix)
for (entry in enumValues<T>()) {
Expand Down Expand Up @@ -660,9 +661,15 @@ object LorenzUtils {
return runCatching { this.group(groupName) }.getOrNull()
}

fun <E> ConcurrentLinkedQueue<E>.drainTo(list: MutableCollection<E>) {
while (true)
list.add(this.poll() ?: break)
}

// Let garbage collector handle the removal of entries in this list
fun <T> weakReferenceList(): MutableSet<T> = Collections.newSetFromMap(WeakHashMap<T, Boolean>())


fun <T> MutableCollection<T>.filterToMutable(predicate: (T) -> Boolean) = filterTo(mutableListOf(), predicate)

}
Loading