.kovenant.kovenant-functional.2.3.2.source-code.moniadic.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kovenant-functional Show documentation
Show all versions of kovenant-functional Show documentation
Functional Kovenant extensions
/*
* 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.
*/
package nl.komponents.kovenant.functional
import nl.komponents.kovenant.*
import java.util.ArrayList
/**
* Asynchronously map the success value of a [Promise] and returns a new [Promise] with the transformed value.
*
* Transforms Promise A to Promise B. If Promise A resolves successful then [fn] is executed on the
* work [DispatcherContext] of the default `Kovenant.context` and returns Promise B. If [fn] is successful,
* meaning now Exception is thrown then Promise B resolves successful, failed otherwise.
*
* If Promise A fails with error E, Promise B will fail with error E too.
*
* @param fn the transform function.
*/
public fun Promise.map(fn: (V) -> R): Promise = then(fn)
/**
* Asynchronously map the success value of a [Promise] and returns a new [Promise] with the transformed value.
*
* Transforms Promise A to Promise B. If Promise A resolves successful then [fn] is executed on the
* work [DispatcherContext] of the default `Kovenant.context` and returns Promise B. If [fn] is successful,
* meaning now Exception is thrown then Promise B resolves successful, failed otherwise.
*
* If Promise A fails with error E, Promise B will fail with error E too.
*
* @param context the on which the map function and returned Promise operate
* @param bind the transform function.
*/
public fun Promise.map(context: Context, bind: (V) -> R): Promise = then(context, bind)
/**
* Asynchronously bind the success value of a [Promise] and returns a new [Promise] with the transformed value.
*
* Transforms Promise A to Promise B with a bind function that returns Promise B.
* If Promise A resolves successful then [fn] is executed on the
* work [DispatcherContext] of the default `Kovenant.context` and returns Promise B. If [bind] is successful,
* meaning now Exception is thrown then Promise B resolves successful, failed otherwise.
*
* If Promise A fails with error E, Promise B will fail with error E too.
*
* @param bind the transform function.
*/
public fun Promise.bind(fn: (V) -> Promise): Promise = bind(context, fn)
/**
* Asynchronously bind the success value of a [Promise] and returns a new [Promise] with the transformed value.
*
* Transforms Promise A to Promise B with a bind function that returns Promise B.
* If Promise A resolves successful then [bind] is executed on the
* work [DispatcherContext] of the default `Kovenant.context` and returns Promise B. If [bind] is successful,
* meaning now Exception is thrown then Promise B resolves successful, failed otherwise.
*
* If Promise A fails with error E, Promise B will fail with error E too.
*
* @param context the on which the bind and returned Promise operate
* @param bind the transform function.
*/
public fun Promise.bind(context: Context, fn: (V) -> Promise): Promise {
if (isDone()) when {
isSuccess() -> {
val deferred = deferred(context)
bindAsync(fn, context, deferred, get())
return deferred.promise
}
isFailure() -> return Promise.ofFail(getError(), context)
}
val deferred = deferred(context)
success {
value ->
bindAsync(fn, context, deferred, value)
}
fail {
deferred reject it
}
return deferred.promise
}
private fun bindAsync(bind: (V) -> Promise,
context: Context,
deferred: Deferred,
value: V) {
context.workerContext offer {
try {
val p = bind(value)
p success { deferred resolve it }
p fail { deferred reject it }
} catch (e: Exception) {
//just like map/then consider bind exception as rejection
deferred reject e
}
}
}
/**
* Applies the map function of the provided `Promise` to the result of this `Promise` and returns a new `Promise` with
* the transformed value.
*
* If either this or the provided `Promise` fails the resulting `Promise` has failed too. this `Promise` takes
* precedence over the provided `Promise` if both fail.
*
* @param promise Promise containing the map function
*/
public fun Promise.apply(promise: Promise<(V) -> R, Exception>): Promise {
return this.apply(this.context, promise)
}
/**
* Applies the map function of the provided `Promise` to the result of this `Promise` and returns a new `Promise` with
* the transformed value.
*
* If either this or the provided `Promise` fails the resulting `Promise` has failed too. this `Promise` takes
* precedence over the provided `Promise` if both fail.
*
* @param context the context on which the map function and the returned promise operate.
* @param promise Promise containing the map function
*/
public fun Promise.apply(context: Context, promise: Promise<(V) -> R, Exception>): Promise {
if (isDone()) when {
isDone() -> {
val deferred = deferred(context)
applyAsync(promise, context, deferred, get())
return deferred.promise
}
isFailure() -> return Promise.ofFail(getError(), context)
}
val deferred = deferred(context)
success {
value ->
applyAsync(promise, context, deferred, value)
}
fail { deferred reject it }
return deferred.promise
}
private fun applyAsync(promise: Promise<(V) -> R, Exception>, context: Context, deferred: Deferred, value: V) {
if (promise.isDone()) when {
promise.isSuccess() -> return applyAsync(context, deferred, promise.get(), value)
promise.isFailure() -> return deferred reject promise.getError()
}
promise success {
fn ->
applyAsync(context, deferred, fn, value)
}
promise fail { deferred reject it }
}
private fun applyAsync(context: Context, deferred: Deferred, fn: (V) -> R, value: V) {
context.workerContext offer {
try {
deferred resolve fn(value)
} catch (e: Exception) {
deferred reject e
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy