commonMain.jetbrains.datalore.base.async.ThreadSafeAsync.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.base.async
import jetbrains.datalore.base.concurrent.Lock
import jetbrains.datalore.base.concurrent.execute
import jetbrains.datalore.base.function.Consumer
import jetbrains.datalore.base.registration.Registration
class ThreadSafeAsync() : ResolvableAsync {
private val myAsync: SimpleAsync = SimpleAsync()
private val lock = Lock()
override fun onSuccess(successHandler: Consumer): Registration {
lock.execute {
return safeReg(myAsync.onSuccess(successHandler))
}
}
override fun onResult(successHandler: Consumer, failureHandler: Consumer): Registration {
lock.execute {
return safeReg(myAsync.onResult(successHandler, failureHandler))
}
}
override fun onFailure(failureHandler: Consumer): Registration {
lock.execute {
return safeReg(myAsync.onFailure(failureHandler))
}
}
override fun map(success: (ItemT) -> ResultT): Async {
lock.execute {
return Asyncs.map(this, success, ThreadSafeAsync())
}
}
override fun flatMap(success: (ItemT) -> Async?): Async {
lock.execute {
return Asyncs.select(this, success, ThreadSafeAsync())
}
}
private fun safeReg(r: Registration): Registration {
return object : Registration() {
override fun doRemove() {
lock.execute {
r.remove()
}
}
}
}
override fun success(result: ItemT) {
lock.execute {
myAsync.success(result)
}
}
override fun failure(t: Throwable) {
lock.execute {
myAsync.failure(t)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy