Hi Andy,
On 03/04/2023 21:14, Andy Goryachev wrote:
Right. I am not saying we should take these classes as is.
In my opinion, this functionality might be better supported by a
separate facility(ies). Specifically, to handle the case of multiple
observables.
Can you elaborate on what cases you see with multiple observables? I
think you mean events here; events can also benefit from debouncing or
throttling, but that's something quite different. An ObservableValue is
not an event source, but the values it takes on can be mapped, delayed
or even interpolated. In the end however, there is always a value
immediately available, making them suitable for binding. An event
source on the other hand can't supply events (values) on demand, can't
repeat them and it does not remember the last one -- you can't bind an
event source in the same way either; at the most you can set a default
value and then update it when events come in.
I also think the APIs are large and complicated enough already, it
might be better to add/extract (a rarely used) functionality to a
separate class or set of classes.
Could you clarify at what point an API is too large or too complicated?
Certainly there are far larger API's in the Java ecosystem and in the
JDK itself. I'm pretty sure there are far more complicated ones as well.
--John
-andy
*From: *John Hendrikx <john.hendr...@gmail.com>
*Date: *Monday, April 3, 2023 at 12:05
*To: *Andy Goryachev <andy.goryac...@oracle.com>, Marius Hanl
<mariush...@web.de>
*Cc: *openjfx-dev@openjdk.org <openjfx-dev@openjdk.org>
*Subject: *[External] : Re: Gauging interest in bindings that can
delay changing their value (debounce/throttle)
Hi Andy,
Those examples seem to be just timers, it would be hard to construct
the primitives like throttle and debounce with these, as they don't
take into account when the value last changed, or whether or not is
important that the value changed again (reset timer or not). These
timers would just run forever, while the functionality I propose here
would have no timers running when things are stable. The timeout would
also trigger precisily on the first value change. Having a running
timer is more like sampling, not throttling or debouncing.
The functionality I'm proposing would be more along the lines of
#reduceSucessions in
https://github.com/TomasMikula/ReactFX/blob/master/reactfx/src/main/java/org/reactfx/EventStream.java
<https://urldefense.com/v3/__https:/github.com/TomasMikula/ReactFX/blob/master/reactfx/src/main/java/org/reactfx/EventStream.java__;!!ACWV5N9M2RV99hQ!IUX7QjZNn8vhK8ISBY_DO3YOqEeBlcegZahboYpjq2XsYz5Xj-XoFVxWKltMxZxJS2aFla51yf2gREfkFoeF1VL_66lB$>
-- except that it would never support accumulation or combining of
values (that's something for streams, not for values).
--John
On 03/04/2023 18:47, Andy Goryachev wrote:
My two cents: I think the functionality of debouncing should
better be solved by a separate facility, rather than added to
observables. An example would be a use case when multiple
observables trigger an expensive or delayed computation or a UI
update.
Something along the lines of
https://github.com/TomasMikula/ReactFX/blob/master/reactfx/src/main/java/org/reactfx/util/FxTimer.java
<https://urldefense.com/v3/__https:/github.com/TomasMikula/ReactFX/blob/master/reactfx/src/main/java/org/reactfx/util/FxTimer.java__;!!ACWV5N9M2RV99hQ!IUX7QjZNn8vhK8ISBY_DO3YOqEeBlcegZahboYpjq2XsYz5Xj-XoFVxWKltMxZxJS2aFla51yf2gREfkFoeF1YeFiEIP$>
or
https://github.com/andy-goryachev/FxEditor/blob/master/src/goryachev/fx/FxTimer.java
<https://urldefense.com/v3/__https:/github.com/andy-goryachev/FxEditor/blob/master/src/goryachev/fx/FxTimer.java__;!!ACWV5N9M2RV99hQ!IUX7QjZNn8vhK8ISBY_DO3YOqEeBlcegZahboYpjq2XsYz5Xj-XoFVxWKltMxZxJS2aFla51yf2gREfkFoeF1VyqfNCE$>
cheers,
-andy
*From: *openjfx-dev <openjfx-dev-r...@openjdk.org>
<mailto:openjfx-dev-r...@openjdk.org> on behalf of Marius Hanl
<mariush...@web.de> <mailto:mariush...@web.de>
*Date: *Thursday, March 30, 2023 at 15:20
*To: *John Hendrikx <john.hendr...@gmail.com>
<mailto:john.hendr...@gmail.com>
*Cc: *openjfx-dev@openjdk.org <openjfx-dev@openjdk.org>
<mailto:openjfx-dev@openjdk.org>
*Subject: *Aw: Gauging interest in bindings that can delay
changing their value (debounce/throttle)
+ 1 for this. Debouncing is a common functionality for observables.
One of the common scenarios is obviously something like a search
filter functionality, where typing in characters triggers an
expensive calculation.
Debouncing solves the problem by doing that when nothing happened
for some time, which is typically met when the user finished typing.
-- Marius
*Gesendet:* Donnerstag, 23. März 2023 um 18:09 Uhr
*Von:* "John Hendrikx" <john.hendr...@gmail.com>
<mailto:john.hendr...@gmail.com>
*An:* openjfx-dev@openjdk.org
*Betreff:* Gauging interest in bindings that can delay changing
their value (debounce/throttle)
Hi list,
I've been working on a potential new API (and proof of concept
implementation) for adding a new type of fluent binding which can
delay
changing their values, and I'm wondering how much interest there is in
such a thing.
The main purpose of such an API is to prevent being flooded with
changes
when properties change often, or to simply delay certain actions until
the user has settled on a selection or has stopped typing.
For this purpose I would like to introduce a default method on
`ObservableValue` with the signature:
ObservableValue<T> throttle(Throttler throttler);
The parameter `Throttler` can be obtained via static methods of a
helper
class named `FXThrottlers`. These provide various pre-configured
throttlers that work correctly with JavaFX's event thread model. My
current proof of concept provides:
public static Throttler debounce(Duration quietPeriod);
public static Throttler debounceTrailing(Duration quietPeriod);
public static Throttler throttle(Duration period);
public static Throttler throttleTrailing(Duration period);
These are variations of similar concepts, and vary mostly in when
exactly they will allow value changes; debouncers will wait for a
period
without any changes, while throttlers will periodically allow changes.
The trailing variants will not immediately emit the first change but
will wait for the period to elapse first; all variants will eventually
take on the value of the source observable. Debouncing is typically
used when you wish for an input to settle before taking action (like
typing in a search bar), while throttling is used to give regular
feedback but avoid doing so too often (like feedback during window
resizing).
Usage example which updates a preview panel when the user has finished
(cursor) scrolling through a list view:
ObjectProperty<T> selectedItem =
listView.getSelectionModel().selectedItemProperty();
selectedItem
.throttle(FXThrottlers.debounceTrailing(Duration.ofMillis(500)))
.addListener((obs, old, current) -> {
if (current != null) {
updatePreviewPanel(current);
}
});
Implementation details:
ObservableValue is part of javafx.base, and as such can't use
animations
or call Platform::runLater. The ThrottledBinding implementation has
abstracted all of these out into the Throttler class, and FXThrottlers
(which would live in javafx.graphics) therefore provides the necessary
call backs to integrate property changes correctly back onto the
JavaFX
event thread. The Throttler class also simplifies testing; the
test can
provide its own timing source and background scheduler. The Throttler
interface has the following methods:
/**
* Schedules a command to run on an unspecified thread after
the time
* given by {@code nanos} elapses.
*
* @param command a command to run, cannot be {@code null}
* @param nanos a time in nanoseconds
*/
void schedule(Runnable command, long nanos);
/**
* Provides the current time in nanoseconds.
*
* @return the current time in nanoseconds
*/
long nanoTime();
/**
* Runs the given command as soon as possible on a thread
specified
by this
* throttler for updating property values.
*
* @param command a command to run, cannot be {@code null}
*/
void update(Runnable command);
/**
* Given the current elapsed time in the current change
window, and the
* amount of time elapsed since the last change was detected,
determines
* if and by how much the current change window should be
extended.
*
* @param elapsed nanoseconds elapsed since the start of the
current change window
* @param elapsedSinceLastChange nanoseconds elapsed since the
last
change
* @return nanoseconds to extend the window with
*/
long determineInterval(long elapsed, long elapsedSinceLastChange);
For testing purposes, the schedule and nanoTime can be provided such
that the throttle function can be tested deterministically. For
integrating with JavaFX, update is implemented as
`Platform.runLater(command)`. The schedule and nanoTime methods
delegate to an Executor and System.nanoTime respectively. When using
properties without JavaFX, Throttler implementations can be provided
which run property updates on a scheduler thread (just calling
Runnable::run on the current thread) or via some user provided
executor.
A sample test case looks like this (read with a mono space font :-)):
@Test
void testThrottleLeadingAndTrailing() {
// create Throttler with deterministic behavior:
Throttler throttler =
create(Throttler.IntervalHandler.throttle(Duration.ofNanos(4));
// create throttled observable:
ObservableValue<String> binding = source.throttle(throttler);
assertChanges(
binding,
"--a-b--c---d-----e-------f-g-----------f-g-----",
"--a---b---c---d---e------f---g---------f---g---"
);
assertInvalidations(
binding,
"--a-b--c---d-----e-------f-g-----------f-g-----",
"--i---i---i---i---i------i---i---------i---i---"
);
}
Thanks for reading, I look forward to your feedback!
--John