Class AsyncDebouncer<T>
- Type Parameters:
T
- the value type
- Direct Known Subclasses:
AsyncDebouncer.Bypass
A debouncer has an input "contact" event and produces an output "settled" once sufficient time has passed since the last contact event. The goal is to prevent the needless frequent firing of asynchronous events if the next event is going to negate the current one. The idea is that a series of events, each negating the previous, can be fired within relative temporal proximity. Without a debouncer, event processing time may be wasted. By passing the events through a debouncer configured with a time window that contains all the events, only the final event in the cluster will be processed. The cost of doing this is a waiting period, so event processing may be less responsive, but will also be less frantic.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CompletableFuture
<Void> protected T
protected CompletableFuture
<T> protected final AsyncTimer
protected final long
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncDebouncer
(AsyncTimer timer, long windowMillis) Construct a new debouncer -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(Consumer<T> listener) Add a listener for the settled eventvoid
Send a contact eventprotected void
void
removeListener
(Consumer<T> listener) Remove a listener from the settled eventsettled()
Receive the next settled eventstable()
Wait for the debouncer to be stable
-
Field Details
-
timer
-
windowMillis
protected final long windowMillis -
listeners
-
settledPromise
-
lastContact
-
alarm
-
-
Constructor Details
-
AsyncDebouncer
Construct a new debouncer- Parameters:
timer
- the timer to use for delaywindowMillis
- the timing window of changes to ignore
-
-
Method Details
-
addListener
Add a listener for the settled event- Parameters:
listener
- the listener
-
removeListener
Remove a listener from the settled event- Parameters:
listener
- the listener
-
doSettled
protected void doSettled() -
contact
Send a contact eventThis sets or resets the timer for the event window. The settled event will fire with the given value after this waiting period, unless another contact event occurs first.
- Parameters:
val
- the new value
-
settled
Receive the next settled eventThe returned future completes after all registered listeners have been invoked.
- Returns:
- a future which completes with the value of the next settled event
-
stable
Wait for the debouncer to be stableIf the debouncer has not received a contact event within the event window, it's considered stable, and this returns a completed future with the value of the last received contact event. Otherwise, the returned future completes on the next settled event, as in
settled()
.- Returns:
- a future which completes, perhaps immediately, when the debouncer is stable
-