![JAR search and dependency download from the Maven repository](/logo.png)
.kovenant.kovenant-ui.3.1.0.source-code.callbacks-api.kt Maven / Gradle / Ivy
/*
* Copyright (c) 2015 Mark Platvoet
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* THE SOFTWARE.
*/
@file:JvmName("KovenantUiApi")
package nl.komponents.kovenant.ui
import nl.komponents.kovenant.*
@JvmOverloads fun promiseOnUi(uiContext: UiContext = KovenantUi.uiContext,
context: Context = Kovenant.context,
alwaysSchedule: Boolean = false,
body: () -> V): Promise {
if (directExecutionAllowed(alwaysSchedule, uiContext.dispatcher)) {
return try {
Promise.ofSuccess(context = context, value = body())
} catch(e: Exception) {
Promise.ofFail(context = context, value = e)
}
} else {
val deferred = deferred(context)
uiContext.dispatcher.offer {
try {
val result = body()
deferred.resolve(result)
} catch(e: Exception) {
deferred.reject(e)
}
}
return deferred.promise
}
}
public infix fun Promise.successUi(body: (value: V) -> Unit): Promise = successUi(alwaysSchedule = false, body = body)
@JvmOverloads
public fun Promise.successUi(uiContext: UiContext = KovenantUi.uiContext,
alwaysSchedule: Boolean,
body: (value: V) -> Unit): Promise {
val dispatcherContext = uiContext.dispatcherContextFor(context)
if (isDone() && directExecutionAllowed(alwaysSchedule, dispatcherContext.dispatcher)) {
if (isSuccess()) {
try {
body(get())
} catch(e: Exception) {
dispatcherContext.errorHandler(e)
}
}
} else {
success(dispatcherContext, body)
}
return this
}
public infix fun Promise.failUi(body: (error: E) -> Unit): Promise = failUi(alwaysSchedule = false, body = body)
@JvmOverloads
public fun Promise.failUi(uiContext: UiContext = KovenantUi.uiContext,
alwaysSchedule: Boolean,
body: (error: E) -> Unit): Promise {
val dispatcherContext = uiContext.dispatcherContextFor(context)
if (isDone() && directExecutionAllowed(alwaysSchedule, dispatcherContext.dispatcher)) {
if (isFailure()) {
try {
body(getError())
} catch(e: Exception) {
dispatcherContext.errorHandler(e)
}
}
} else {
fail(dispatcherContext, body)
}
return this
}
public infix fun Promise.alwaysUi(body: () -> Unit): Promise = alwaysUi(alwaysSchedule = false, body = body)
@JvmOverloads
public fun Promise.alwaysUi(uiContext: UiContext = KovenantUi.uiContext,
alwaysSchedule: Boolean,
body: () -> Unit): Promise {
val dispatcherContext = uiContext.dispatcherContextFor(context)
if (isDone() && directExecutionAllowed(alwaysSchedule, dispatcherContext.dispatcher)) {
try {
body()
} catch(e: Exception) {
dispatcherContext.errorHandler(e)
}
} else {
always(dispatcherContext, body)
}
return this
}
private fun directExecutionAllowed(alwaysSchedule: Boolean, dispatcher: Dispatcher): Boolean {
return !alwaysSchedule && dispatcher is ProcessAwareDispatcher && dispatcher.ownsCurrentProcess()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy