Giter Site home page Giter Site logo

mplatvoet / kovenant Goto Github PK

View Code? Open in Web Editor NEW
654.0 654.0 28.0 1.57 MB

Kovenant. Promises for Kotlin.

Home Page: http://kovenant.komponents.nl

License: MIT License

Kotlin 99.43% Shell 0.05% Java 0.52%
andriod async concurrency kotlin promises rx rxjava

kovenant's Introduction

CircleCI branch Maven Central DUB develop: Develop dependency status master: Master dependency status

Kovenant

Promises for Kotlin.

The easy asynchronous library for Kotlin. With extensions for Android, RxJava, JavaFX and much more.

task { "world" } and task { "Hello" } success {
    println("${it.second} ${it.first}!")
}

Please refer to the Kovenant site for API usage and more.

Getting started

Build against Kotlin: 1.0.3. Source and target compatibility is 1.6

Gradle

dependencies {
    compile 'nl.komponents.kovenant:kovenant:3.3.0'
}

Maven

<dependency>
	<groupId>nl.komponents.kovenant</groupId>
	<artifactId>kovenant</artifactId>
	<version>3.3.0</version>
</dependency>

Android Demo app

Checkout the Android Demo App on Github.

Artifacts

Kovenant has been structured in sub projects so you can cherry pick what you need.

artifact description
kovenant Container artifact that consists of kovenant-core, kovenant-combine, kovenant-jvm and kovenant-functional
kovenant-core The core of kovenant. Provides the API and default implementations
kovenant-combine Adds combine functionality that keep everything strongly typed
kovenant-jvm Support for converting between Executors and Dispatchers
kovenant-ui Support for UI frameworks that need UI work to operate on a specific process
kovenant-rx Add promise support to Rx
kovenant-android Extensions for Android specific needs
kovenant-jfx Extensions for JavaFX specific needs
kovenant-disruptor LMAX Disruptor work queues
kovenant-progress Progress configuration helper
kovenant-functional Functional Programming idiomatic additions

Issues

Issues are tracked in Youtrack

Release notes

See Changelog for release notes

Slack

Join the #kovenant channel on Kotlin Slack.

More Kotlin libraries

Check out Awesome Kotlin

kovenant's People

Contributors

aembleton avatar cliffbrown avatar gimlet2 avatar mplatvoet avatar seanfisk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kovenant's Issues

Question about chaining long processes.

I was trying to chain long processes but it wasn't working. All the long processes were returning deferredPromises.

I thought this was working not too long ago but the update to AndroidStudio 3.1 seems to have broken something. I was seeing pending promises fall through. It was something like.

renewToken() then {
getData()
} successUI {
broadcast update
}

The broad cast update was happening while getData was pending. I have worked around by doing:

all(renewToken(), getData()) successUI {
}

It appears though that the then is not working at all. Its just falling through. I had another case where this was falling through when trying to log out then show login screen when logout was complete.

Blocking call in task

Call from this function

private fun loginWithFacebookAccessToken(accessToken: AccessToken) {
        promiseOnUi {
            mvpView?.showLoggingProgress()
        } then {
            loginManager.loginWithFacebook(accessToken).get() //Call from this side
		} alwaysUi {
            mvpView?.hideLoggingProgress()
        } successUi {
            mvpView?.navigateToMainScreen()
        } failUi { error ->
			mvpView?.showMessage(error.localizedMessage)
        }
    }

To this function

    fun loginWithFacebook(fbAccessToken: AccessToken): Promise<Unit, Exception> {
		ti { "KOVENANT In Function" }
        return task {
			ti { "KOVENANT In task" }
        } then {
			ti { "KOVENANT In then" }
        }
    }

Hi
I have a problem in the last function, my call is block in the task { .. }
My application is deploy on a SAMSUNG S4 MINI API 19 Version 4.4.2
And the logcat only show : "KOVENANT In Function"

Android - `onTerminate` will never be called in production environments

It may be useful to update your Android configuration documentation to state that onTerminate will never be called in production environments. https://developer.android.com/reference/android/app/Application.html#onTerminate()

Your webpage currently lists the following example:

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        // Configure Kovenant with standard dispatchers
        // suitable for an Android environment.
        startKovenant()
    }

    override fun onTerminate() {
        super.onTerminate()
        // Dispose of the Kovenant thread pools.
        // For quicker shutdown you could use
        // `force=true`, which ignores all current
        // scheduled tasks
        stopKovenant()
    }
}

http://kovenant.komponents.nl/android/config/

Nested promise never complete with all

Version
nl.komponents.kovenant:kovenant:3.3.0
nl.komponents.kovenant:kovenant-android:3.3.0
Kotlin: 1.2.10
Android Studio 3.0.1
Build #AI-171.4443003, built on November 10, 2017
JRE: 1.8.0_152-release-915-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1

Test Code:

val test = mutableListOf<Promise<Int, Exception>>()
test.add(task { task { 1 }.get() })
all(test) success {
    // Never called
}

Note that Promise.of(1).get() works fine but not task { 1 }.get().

I can confirm that the inner task has been executed but somehow success is never called. I was trying to use this as fallback task, for example:

task {
    cache.get(key) ?: loadFromWebTask(key).get()
}

unwrapping the return Promise of then

Hey there,

kovenant is a great library. I love to use it. However, there is a small feature I like I wish you can provide is "unwrap the return Promise of then"

For example, I have two methods

fun method1() : Promise<String, Exception>{}
fun method2() : Promise<String, Exception>{}

// current
val p: Promise<Promise<String, Exception>, Exception> = method1().then{ method2() }
// can it just be 
val p: Promise<String, Exception> = method1().then{ method2() }

Now, I added these extensions in my project to achieve it, but I don't know whether it is a good way to do it and it is a little unclear to use thenFlat instead of then.

infix fun <V, R> Promise<V, Exception>.thenFlat(bind: (V) -> Promise<R, Exception>): Promise<R, Exception> {
    val deferred = deferred<R, Exception>(context)
    success {
        try {
            bind(it).success(deferred::resolve).fail(deferred::reject)
        } catch (e: Exception) {
            deferred.reject(ApiError(e))
        }
    }.fail(deferred::reject)
    
    return deferred.promise
}

// so it can be
val p: Promise<String, Exception> = method1().thenFlat{ method2() }

Rethrow exception in fail block

I would like to catch any Exception in fail block and then rethrow it as WrappedException. Code should be look like that:

task {
    ...
} fail { error ->
    throw WrappedException(message = "Something went wrong", cause = error)
}

But instead of providing WrappedException in sequential fail blocks, kovenant provides the same error. As I can see that's happening because lambdas, that are passed to fail, are used as callbacks instead of something like map function.
What are you thoughts on this problem and what can you suggest to overcome this problem?

Tuple1..22 and the like create unnecessary additional classes

One strength Kotlin really has going for it now is a low number of class/method points which is important in environments like Android where you have a set limit. The cases where you have combine could easily be vararg instead of fixed parameters, with lists of things instead of set individual first, second, third, fourth, etc.

Plus your code becomes littered with variations of all these classes that must be kept in sync.

Simplify, it is really a list of things to combine, so treat it as such instead of hard set parameters and fields. Otherwise your library will go unused in environments such as Android (which may not be important to you, but it is a key driver helping Kotlin thrive right now, so let's all help that continue)

Not supported anymore?

Question. The last changes were made 8 months ago. Is this library still going to be supported for 1.3 and beyond?

Question about chaining with `all()`

Hi all,
I have using kovenant framework with some questions
I would like to fetch a table of contents and fetch the content of the table
simultaneously, then output a mixed list with table content title and content itself.

But I faced the problems for coding with kovenant.

For example, I have MySubject and MySubjectDetails class.

class MySubject {
    var id: String = ""
    var name: String = ""
}

class MySubjectDetails {
    var id: String = ""
    var name: String = ""
    var data: List<String> = ArrayList()
}

and APICalls class (part of code is pseudo code),

object APICalls {
    fun getSubjects(): Promise<List<MySubject>, Exception> {
        val deferred = deferred<List<MySubject>, Exception>()
        // ... API calls ...
        return deferred.promise
    }

    fun getSubjectById(id: String): Promise<MySubjectDetail, Exception> {
        val deferred = deferred<MySubjectDetail, Exception>()
        // ... API calls ...
        return deferred.promise
    }
}

I may fetch the data like is:

APICalls.getSubjects().then { subjects ->
    val promises = subjects.map {
        APICalls.getSubjectById(it.id)
    }
    all(promises)
}.then { promise: Promise<List<MySubjectDetail>, Exception> ->
    //       ^~~~~~ What's this?
}

I will get a result with Promise<List<MySubjectDetail>, Exception> type then I can't continue the code.
I expected it will be a combined list (List<MySubjectDetail>) but it comes up a Promise<List<MySubjectDetail>, Exception> object thats confused me.

How can I do or fix the code? Thanks.

Using deferred promise in middle of chain..

How can you use a deferred promise int he middle of a chain? The only way I can see doing it right now is.

task {
setup1()
} then {
setup2()
} then {
deffered()
} then { promise ->
val result = promise ?: throw(error)
if result.isError() {
throw(error)
} else {
it.get()
}
} fail {
}

Unwrap doesn't seem to exist like is suggested.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.