Skip to content

Commit

Permalink
fix: make sure DataFetcher only runs once at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed May 16, 2024
1 parent 205edae commit 26ed9bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ object SkytilsCommand : BaseCommand("skytils", listOf("st")) {
} else {
when (args[1].lowercase()) {
"data" -> {
DataFetcher.reloadData().invokeOnCompletion {
DataFetcher.reloadData()
DataFetcher.job?.invokeOnCompletion {
it?.run {
UChat.chat("$failPrefix §cFailed to reload repository data due to a ${it::class.simpleName ?: "error"}: ${it.message}!")
}.ifNull {
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/gg/skytils/skytilsmod/core/DataFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import kotlin.concurrent.fixedRateTimer
import kotlin.reflect.jvm.jvmName

object DataFetcher {
var job: Job? = null

private fun loadData(): Job {
return Skytils.IO.launch {
try {
Expand Down Expand Up @@ -213,8 +215,13 @@ object DataFetcher {
}

@JvmStatic
fun reloadData() =
loadData()
fun reloadData() {
if (job?.isActive != true) {
job = loadData()
} else {
UChat.chat("$failPrefix §cData fetch requested while already fetching!")
}
}

internal fun preload() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.launch
import java.util.concurrent.ConcurrentHashMap
import javax.imageio.ImageIO

object SummonSkins {
// maps name to url
val skinMap = HashMap<String, String>()
val skinMap = ConcurrentHashMap<String, String>()

// maps name to dynamic resource
val skintextureMap = HashMap<String, DynamicResource>()
Expand Down

0 comments on commit 26ed9bb

Please sign in to comment.