sodium
sodium copied to clipboard
Cell updates listener: is this expected behaviour?
The following Scala code (for a subclass of scalafx Label) works fine with GUI:
class LabelRx(val data: Cell[String]) extends Label(" ") { private val listen = data.updates().listen((t: String) => text = t) }
However, the statement
data.updates().listen((t: String) => text = t)
(without assignment) produces an unresponsive label. Is this a bug? Or is it expected behaviour?
I had a quick peak at the Scala code. It seems that listen
in the Scala version is the same as listenWeak
in the Java version. I've never used the Scala version, but I thought all versions were made to work the same way. (listen
in scala did not place the listener into a global keep alive list)
That said though, its probably not a bad thing to have the end-user use weak listeners all the time. They can keep a member variable in the class for the UI, and when that class gets garbage collected, the listener will auto-unlisten and avoid any memory leaks.
That's a very useful clarification. Thank you!
@clinuxrulz and @spockoyno - During the writing of the book, after careful consideration, I changed to the way it is now in the Java version. The Scala version hasn't received much maintenance so it's out of date. There are quite a few issues like this. In fact, the problem you had is pretty much the reason why I changed it.
@the-real-blackh that sounds OK. Better to have a leak, than unexpected behaviour. The end user can always go back to fix theirs leaks. (Remembering to unlisten.)
@the-real-blackh Indeed it seems like there're quite a few differences between the Scala and Java versions. So I'm writing a Scala wrapper around the Java library instead.
A small question: The method in the Cell.java final Stream<A> updates(Transaction trans) { return str; } is independent from the Transaction. Does this have some significance (or is just for compatibility)?
PS Huge thanks for the excellent framework and book!
@spockoyno It would be great to get whatever you've done checked in.
That transaction is left over from an older way of doing things. I just checked in a change to take it out.
3670fe88c823b4da6a0328f911401e118435af54
I'd be happy to. Provided it's decent quality stuff, still early days.
Closing as I don't believe this is an issue anymore after the rework of the Scala version.