tornadofx icon indicating copy to clipboard operation
tornadofx copied to clipboard

Controller dont fire event on OpenJDK 11

Open 0v3r12 opened this issue 4 years ago • 6 comments

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.

0v3r12 avatar Feb 25 '21 10:02 0v3r12

Please post a minimal runnable example.

SKeeneCode avatar Mar 01 '21 13:03 SKeeneCode

from this code snippet it is not clear how to reproduce the problem

SchweinchenFuntik avatar Mar 03 '21 18:03 SchweinchenFuntik

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()) }
    }
}


doughoff avatar Mar 03 '21 23:03 doughoff

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.

doughoff avatar Mar 03 '21 23:03 doughoff

I was able to do it like this to make my UI dont freeze. ✌️ Thkx for the awesome job 👍

doughoff avatar Mar 03 '21 23:03 doughoff

please close if you have solved the problem

SchweinchenFuntik avatar Mar 14 '21 16:03 SchweinchenFuntik