tornadofx
tornadofx copied to clipboard
Controller dont fire event on OpenJDK 11
With the exact same code from de GUIDE about EventBUS.
import tornadofx.EventBus.RunOn.*
button("Load customers").action {
fire(CustomerListRequest)
}
class CustomerController : Controller() {
init {
subscribe<CustomerListRequest> {
val customers = loadCustomers()
fire(CustomerListEvent(customers))
}
}
fun loadCustomers(): List<Customer> = db.selectAllCustomers()
}
tableview<Customer> {
column("Name", Customer::nameProperty)
column("Age", Customer::ageProperty)
subscribe<CustomerListEvent> { event ->
items.setAll(event.customers)
}
}
The same event can be Fired from view.
Please post a minimal runnable example.
from this code snippet it is not clear how to reproduce the problem
I was able to do it like this.
class MyApp : App(TestView::class)
class Launch {
companion object {
@JvmStatic
fun main(args: Array<String>) {
launch<MyApp>(args)
}
}
}
class MessageEvent(val message: String) : FXEvent()
class MessageRequest : FXEvent()
data class Todo(val userId: Int, val id: Int, val title: String, val completed: Boolean)
class TestController : Controller() {
val apiClient = HttpClient(CIO) {
install(JsonFeature) {
serializer = JacksonSerializer {
enable(SerializationFeature.INDENT_OUTPUT)
dateFormat = DateFormat.getDateInstance()
}
}
}
init {
subscribe<MessageRequest> { getTodo() }
}
fun fireMessage(message: String) {
fire(MessageEvent(message))
}
fun getTodo() {
GlobalScope.launch(Dispatchers.IO) {
val todo = apiClient.get<Todo>("https://jsonplaceholder.typicode.com/todos/1")
launch(Dispatchers.JavaFx) {
fireMessage("todo_${todo.id}=${todo.title}")
}
}
}
}
class TestView : View() {
val testController: TestController by inject()
override val root = vbox {
label("default message") {
subscribe<MessageEvent> {
text = it.message
}
}
button("Fire from view.").action { fire(MessageEvent("Hi, from view.")) }
button("From controller").action { testController.fireMessage("Hi, from controller") }
button("From API response").action { fire(MessageRequest()) }
}
}
Im using Ktor && Jackson. When you launch our coroutine from your subscribe, then fires your async function the code dont works.
But if you make the launch from the funciton body, it works just fine.
Idk if im buying something wrong about coroutines or ktor.
I was able to do it like this to make my UI dont freeze. ✌️ Thkx for the awesome job 👍
please close if you have solved the problem