Package arez

Class ComputableValue<T>

java.lang.Object
arez.Node
arez.ComputableValue<T>
All Implemented Interfaces:
Disposable

public final class ComputableValue<T> extends Node
The ComputableValue represents an ObservableValue derived from other ObservableValues within the Arez system. The value is calculated lazily. i.e. The ComputableValue will only be calculated if the ComputableValue has observers.

It should be noted that the ComputableValue is backed by both an ObservableValue and an Observer. The id's of each of these nodes differ but they share the name and thus while debugging appear to be a single element.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Flags that can configure ComputableValue instances during creation.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Dispose the ComputableValue so that it can no longer be used.
    get()
    Return the computable value, calculating the value if it is not up to date.
    boolean
    Return true if dispose() has been called on object.
    Invoke this method to ensure that the ComputableValue is activated and computing a value even if there are no observers.
    void
    Invoked when a non-arez dependency of the ComputableValue has changed.

    Methods inherited from class arez.Node

    getContext, getName, toString

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface arez.Disposable

    isNotDisposed
  • Method Details

    • get

      public T get()
      Return the computable value, calculating the value if it is not up to date. Before invoking this method, a transaction MUST be active but it may be read-only or read-write.
      Returns:
      the computable value.
    • reportPossiblyChanged

      public void reportPossiblyChanged()
      Invoked when a non-arez dependency of the ComputableValue has changed. The ComputableValue may or may not change as a result of the dependency change but Arez will recalculate the ComputableValue during the normal reaction cycle or when next accessed and will propagate the change at that time if required. This method must be explicitly invoked by the developer if the ComputableValue is derived from non-arez data and that data changes. Before invoking this method, a read-write transaction MUST be active.
    • dispose

      public void dispose()
      Dispose the ComputableValue so that it can no longer be used.
    • isDisposed

      public boolean isDisposed()
      Description copied from interface: Disposable
      Return true if dispose() has been called on object.
      Returns:
      true if dispose has been called.
    • keepAlive

      @Nonnull public Disposable keepAlive()
      Invoke this method to ensure that the ComputableValue is activated and computing a value even if there are no observers. This is used when there is a chance that the value will be accessed multiple times, without being accessed from within a tracking transaction (i.e. the value may only be accessed from actions or may have observers come and go).

      This method should not be called if the computable value was created with the ComputableValue.Flags.KEEPALIVE as it is never deactivated in that configuration.

      When the computable value no longer needs to be kept alive the return value from this method should be disposed.

      Returns:
      the object to dispose when no longer need to keep alive.