On Fri, 7 Oct 2022 20:50:55 GMT, Andy Goryachev <ango...@openjdk.org> wrote:

> Introduction
> 
> There is a number of places where various listeners (strong as well as weak) 
> are added, to be later disconnected in one go. For example, Skin 
> implementations use dispose() method to clean up the listeners installed in 
> the corresponding Control (sometimes using 
> LambdaMultiplePropertyChangeListenerHandler class).
> 
> This pattern is also used by app developers, but there is no public utility 
> class to do that, so every one must invent their own, see for instance
> https://github.com/andy-goryachev/FxTextEditor/blob/master/src/goryachev/fx/FxDisconnector.java
> 
> Proposal
> 
> It might be beneficial to create a class (ListenerHelper, feel free to 
> suggest a better name) which:
> 
> - provides convenient methods like addChangeListener, 
> addInvalidationListener, addWeakChangeListener, etc.
> - keeps track of the listeners and the corresponding ObservableValues
> - provides a single disconnect() method to remove all the listeners in one go.
> - optionally, it should be possible to add a lambda (Runnable) to a group of 
> properties
> - optionally, there should be a boolean flag to fire the lambda immediately
> - strongly suggest implementing an IDisconnectable functional interface with 
> a single disconnect() method
> 
> Make it Public Later
> 
> Initially, I think we could place the new class in package 
> com.sun.javafx.scene.control; to be made publicly accessible later.
> 
> Where It Will Be Useful
> 
> [JDK-8294589](https://bugs.openjdk.org/browse/JDK-8294589) "MenuBarSkin: 
> memory leak when changing skin"
> and other skins, as a replacement for 
> LambdaMultiplePropertyChangeListenerHandler.
> 
> https://github.com/openjdk/jfx/pull/908#:~:text=19%20hours%20ago-,8295175%3A%20SplitPaneSkinSkin%3A%20memory%20leak%20when%20changing%20skin%20%23911,-Draft
> 
> https://github.com/openjdk/jfx/pull/914

Thank you @nlisker and @hjohn for providing the link and explanations.  

You are right, ListenerHelper is somewhat similar to Subscription in that it 
allows for quick disconnect().

It is, however, designed for a different purpose, that goes beyond what is 
needed from a simple listener management.  Specifically,

- it assumes multiple listeners/handlers/callbacks from the start
- it allows for invoking listeners/callbacks immediately as it is a frequent 
pattern not only in Skins but also in application code (I do want to make this 
class a part of public API eventually)
- unlike Subscription, we can addEventHandlers and addEventFilters
- unlike Subscription, we can add Weak listeners/callbacks, though I admit it 
might be a design decision to use or not to use that

to summarize, I see some minor overlap between Subscription and ListenerHelper, 
but mostly these are two different beasts designed for two different purposes.

I do agree with the call to cooperate!  Would love to.

what do you think?

-------------

PR: https://git.openjdk.org/jfx/pull/908

Reply via email to