Package ghidra.async

# Class AsyncDebouncer<T>

java.lang.Object
ghidra.async.AsyncDebouncer<T>

Type Parameters:
  `T` - the value type

Direct Known Subclasses:
  `AsyncDebouncer.Bypass`

public class AsyncDebouncer<T> extends Object
A debouncer for asynchronous events

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
  Modifier and Type
  Class
  Description
  `static class`
  `AsyncDebouncer.Bypass<T>`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected CompletableFuture<Void>`
  `alarm`
  `protected T`
  `lastContact`
  `protected final List<Consumer<T>>`
  `listeners`
  `protected CompletableFuture<T>`
  `settledPromise`
  `protected final AsyncTimer`
  `timer`
  `protected final long`
  `windowMillis`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AsyncDebouncer(AsyncTimer timer, long windowMillis)`
  Construct a new debouncer

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addListener(Consumer<T> listener)`
  Add a listener for the settled event
  `void`
  `contact(T val)`
  Send a contact event
  `protected void`
  `doSettled()`
  `void`
  `removeListener(Consumer<T> listener)`
  Remove a listener from the settled event
  `CompletableFuture<T>`
  `settled()`
  Receive the next settled event
  `CompletableFuture<T>`
  `stable()`
  Wait for the debouncer to be stable

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### timer

    protected final AsyncTimer timer

  - ### windowMillis

    protected final long windowMillis

  - ### listeners

    protected final List<Consumer<T>> listeners

  - ### settledPromise

    protected CompletableFuture<T> settledPromise

  - ### lastContact

    protected T lastContact

  - ### alarm

    protected CompletableFuture<Void> alarm

- ## Constructor Details

  - ### AsyncDebouncer

    public AsyncDebouncer(AsyncTimer timer, long windowMillis)
    Construct a new debouncer
    Parameters:
      `timer` - the timer to use for delay
      `windowMillis` - the timing window of changes to ignore

- ## Method Details

  - ### addListener

    public void addListener(Consumer<T> listener)
    Add a listener for the settled event
    Parameters:
      `listener` - the listener

  - ### removeListener

    public void removeListener(Consumer<T> listener)
    Remove a listener from the settled event
    Parameters:
      `listener` - the listener

  - ### doSettled

    protected void doSettled()

  - ### contact

    public void contact(T val)
    Send a contact event

    This 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

    public CompletableFuture<T> settled()
    Receive the next settled event

    The returned future completes after all registered listeners have been invoked.

    Returns:
      a future which completes with the value of the next settled event

  - ### stable

    public CompletableFuture<T> stable()
    Wait for the debouncer to be stable

    If 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