edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.67k stars 270 forks source link

tornadofx can work with kotlin coroutine ? #881

Open toplinuxsir opened 5 years ago

toplinuxsir commented 5 years ago

Tornadofx can work with kotlin coroutine ? any examples? Thanks!

edvin commented 5 years ago

I know plenty of people are using coroutines with TornadoFX, though there is no special support for it. Not sure if it's even needed :) Take a look here for examples:

https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md#javafx

Better yet, ask in the Slack channel, I'm sure some of the guys can give you some example code!

toplinuxsir commented 5 years ago

@edvin Thanks a lot , I got it

stars-one commented 4 years ago

do you success?The IDEA show me a error that Suspend function 'delay' should be called only from a coroutine or another suspend function when I use the method named delay in coroutines.

I use the library kotlinx-coroutines-javafx,version is 1.3.5

Following is my code

class TestView() : View("My View") {

    override val root = vbox {
        button {
            action {      
                GlobalScope.launch(Dispatchers.UI){
                    //idea show a error tip
                    delay(200L)
                }
            }
        }
    }
}
SchweinchenFuntik commented 4 years ago

@Stars-One This code works for me, but why do you use Dispatchers.UI you need Dispatchers.JavaFx. what version of kotlin do you have?

SchweinchenFuntik commented 4 years ago

also I would not recommend using GlobalScope. Work with coroutines can be hidden in the controller. You can also write auxiliary extension functions for calling coruntin

stars-one commented 4 years ago

yes,I know the Dispatchers.UI,It also show the same error tip.I just want to finish a simple example.

I use the kotlin verison 1.3.70,maybe the reason is the version of idea?my idea version is 2018.2 @SchweinchenFuntik

SchweinchenFuntik commented 4 years ago

most likely yes, new versions of the kotlin compiler didn’t seem to work with the old IDEA

doughoff commented 3 years ago

Try this. Use the JavaFX Dispatcher.


GlobalScope.launch(Dispatchers.IO) {
    try {
        val res = api.put<ApiSuccess<Entity>>("$api_url/entities") {
            body = entity
        }
        launch(Dispatchers.JavaFx) {
            fire(EntityUpdatedEvent(res.data))
        }
    } catch (e: ApiException) {
        ErrorMessage("..err.", e.apiError.error).show()
    }
}