
util.Loader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of evaluation-proxy-core Show documentation
Show all versions of evaluation-proxy-core Show documentation
Core package for Amplitude's evaluation proxy.
The newest version!
package com.amplitude.util
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
class Loader {
private val jobsMutex = Mutex()
private val jobs = mutableMapOf()
suspend fun load(
key: String,
loader: suspend CoroutineScope.() -> Unit,
) = coroutineScope {
jobsMutex.withLock {
jobs.getOrPut(key) {
launch {
try {
loader()
} finally {
jobsMutex.withLock { jobs.remove(key) }
}
}
}
}.join()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy