jvmMain.base.edt.SafeAsync.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.edt
import jetbrains.datalore.base.async.Async
import jetbrains.datalore.base.async.Asyncs
import jetbrains.datalore.base.async.ThreadSafeAsync
import jetbrains.datalore.base.function.Consumer
import jetbrains.datalore.base.registration.Registration
import java.util.concurrent.atomic.AtomicBoolean
actual class SafeAsync actual constructor() : Async {
private val myAsync: ThreadSafeAsync = ThreadSafeAsync()
private val myFulfilled: AtomicBoolean = AtomicBoolean(false)
internal fun success(result: ItemT) {
if (notFulfilled()) {
myAsync.success(result)
}
}
internal fun fail(t: Throwable) {
if (notFulfilled()) {
myAsync.failure(t)
}
}
internal fun delegate(async: Async) {
if (notFulfilled()) {
Asyncs.delegate(async, myAsync)
}
}
private fun notFulfilled(): Boolean {
return myFulfilled.compareAndSet(false, true)
}
override fun onSuccess(successHandler: Consumer): Registration {
return myAsync.onSuccess(successHandler)
}
override fun onResult(successHandler: Consumer, failureHandler: Consumer): Registration {
return myAsync.onResult(successHandler, failureHandler)
}
override fun onFailure(failureHandler: Consumer): Registration {
return myAsync.onFailure(failureHandler)
}
override fun map(success: (ItemT) -> ResultT): Async {
return myAsync.map(success)
}
override fun flatMap(success: (ItemT) -> Async?): Async {
return myAsync.flatMap(success)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy