RFR: JDK-8304439: Subscription based listeners [v2]

Michael Strauß mstrauss at openjdk.org
Sat Jun 24 19:26:09 UTC 2023


On Fri, 23 Jun 2023 22:28:57 GMT, Andy Goryachev <angorya at openjdk.org> wrote:

>>> On that same topic of naming methods:
>>> 
>>> What do people think of `Subscription#unsubscribe`? Should it be `cancel`? Something else? Okay as it is?
>> 
>> OK as is. Especially if we go with one of the two choices with "subscribe" in the name.
>
> I might prefer the second option, subscribeXXX().
> And unsubscribe() is good.

I prefer `subscribe` with overloads, as this comes closest to the existing naming scheme of JavaFX listener operations (`addListener` / `removeListener` with overloads).

I would also like to see a subscription-style API that works with the existing listener types. This has the advantage of not needing a wrapper for each listener registration, and it can be a drop-in replacement in many places where JavaFX listeners are used today.

My suggestion is to have the following new operations:
* `subscribe` (`Runnable`, `Consumer`, `BiConsumer`)
* `subscribeListener` (`InvalidationListener`, `ChangeListener`)

This pairs nicely with the existing operations:
* `addListener` (`InvalidationListener`, `ChangeListener`)
* `removeListener` (`InvalidationListener`, `ChangeListener`)

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

PR Review Comment: https://git.openjdk.org/jfx/pull/1069#discussion_r1240925324


More information about the openjfx-dev mailing list