Package arez.dom

Class EventDrivenValue<SourceType extends akasha.EventTarget,ValueType>

java.lang.Object
arez.dom.EventDrivenValue<SourceType,ValueType>
Type Parameters:
SourceType - the type of the DOM element that generates events of interest.
ValueType - the type of the value returned by the "value" observable.

@ArezComponent(requireId=DISABLE, disposeNotifier=DISABLE) public abstract class EventDrivenValue<SourceType extends akasha.EventTarget,ValueType> extends Object
Generic component that exposes a property as observable where changes to the variable are signalled using a browser event. A typical example is making the value of window.innerWidth observable by listening to "resize" events on the window. This could be achieved with code such as:

 EventDrivenValue<Window, Integer> innerWidth = EventDrivenValue.create( window, "resize", () -> window.innerWidth )
 

It is important that the code not add a listener to the underlying event source until there is an observer accessing the "value" observable defined by the EventDrivenValue class. The first observer that observes the observable will result in an event listener being added to the event source and this listener will not be removed until there is no observers left observing the value. This means that a component that is not being used has very little overhead.

  • Method Details

    • create

      @Nonnull public static <SourceType extends akasha.EventTarget, ValueType> EventDrivenValue<SourceType,ValueType> create(@Nonnull SourceType source, @Nonnull String event, @Nonnull EventDrivenValue.Accessor<SourceType,ValueType> getter)
      Create the component.
      Type Parameters:
      SourceType - the type of the DOM element that generates events of interest.
      ValueType - the type of the value returned by the "value" observable.
      Parameters:
      source - the DOM element that generates events of interest.
      event - the event type that could result in changes to the observed value. The event type is expected to be generated by the source element.
      getter - the function that retrieves the observed value from the platform.
      Returns:
      the new component.
    • getSource

      @Nonnull @Observable public SourceType getSource()
      Return the element that generates the events that report potential changes to the observed value.
      Returns:
      the associated element.
    • setSource

      public void setSource(@Nonnull SourceType source)
      Set the element that generates events. This ensures that the event listeners are managed correctly if the source is currently being observed.
      Parameters:
      source - the the event source.
    • getValue

      Return the value.
      Returns:
      the value.