SodiumFRP / sodium

Sodium - Functional Reactive Programming (FRP) Library for multiple languages
http://sodium.nz/
Other
848 stars 138 forks source link

Cell updates listener: is this expected behaviour? #115

Closed spockoyno closed 1 year ago

spockoyno commented 6 years ago

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?

clinuxrulz commented 6 years ago

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.

spockoyno commented 6 years ago

That's a very useful clarification. Thank you!

the-real-blackh commented 6 years ago

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

clinuxrulz commented 6 years ago

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

spockoyno commented 6 years ago

@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 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!

the-real-blackh commented 6 years ago

@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

spockoyno commented 6 years ago

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

jam40jeff commented 1 year ago

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