sodium icon indicating copy to clipboard operation
sodium copied to clipboard

Cell updates listener: is this expected behaviour?

Open spockoyno opened this issue 7 years ago • 7 comments

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?

spockoyno avatar Nov 03 '17 21:11 spockoyno

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.

clinuxrulz avatar Nov 04 '17 08:11 clinuxrulz

That's a very useful clarification. Thank you!

spockoyno avatar Nov 04 '17 17:11 spockoyno

@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 avatar Nov 05 '17 22:11 the-real-blackh

@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.)

clinuxrulz avatar Nov 06 '17 01:11 clinuxrulz

@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 avatar Nov 06 '17 19:11 spockoyno

@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

the-real-blackh avatar Nov 06 '17 20:11 the-real-blackh

I'd be happy to. Provided it's decent quality stuff, still early days.

spockoyno avatar Nov 07 '17 15:11 spockoyno

Closing as I don't believe this is an issue anymore after the rework of the Scala version.

jam40jeff avatar Apr 22 '23 14:04 jam40jeff