Package rx.subjects

Class AsyncSubject<T>

java.lang.Object
rx.Observable<T>
rx.subjects.Subject<T,T>
rx.subjects.AsyncSubject<T>
Type Parameters:
T - the type of item expected to be observed by the Subject
All Implemented Interfaces:
Observer<T>

public final class AsyncSubject<T> extends Subject<T,T>
Subject that publishes only the last item observed to each Observer once the source Observable has completed. The item is cached and published to any Observers which subscribe after the source has completed. If the source emitted no items, AsyncSubject completes without emitting anything. If the source terminated in an error, current and future subscribers will receive only the error.

Example usage:

 

  // observer will receive no onNext events because the subject.onCompleted() isn't called.
  AsyncSubject<Object> subject = AsyncSubject.create();
  subject.subscribe(observer);
  subject.onNext("one");
  subject.onNext("two");
  subject.onNext("three");

  // observer will receive "three" as the only onNext event.
  AsyncSubject<Object> subject = AsyncSubject.create();
  subject.subscribe(observer);
  subject.onNext("one");
  subject.onNext("two");
  subject.onNext("three");
  subject.onCompleted();

   
  • Field Details

  • Constructor Details

  • Method Details

    • create

      public static <T> AsyncSubject<T> create()
      Creates and returns a new AsyncSubject.
      Type Parameters:
      T - the result value type
      Returns:
      the new AsyncSubject
    • onCompleted

      public void onCompleted()
      Description copied from interface: Observer
      Notifies the Observer that the Observable has finished sending push-based notifications.

      The Observable will not call this method if it calls Observer.onError(java.lang.Throwable).

    • onError

      public void onError(Throwable e)
      Description copied from interface: Observer
      Notifies the Observer that the Observable has experienced an error condition.

      If the Observable calls this method, it will not thereafter call Observer.onNext(T) or Observer.onCompleted().

      Parameters:
      e - the exception encountered by the Observable
    • onNext

      public void onNext(T v)
      Description copied from interface: Observer
      Provides the Observer with a new item to observe.

      The Observable may call this method 0 or more times.

      The Observable will not call this method again after it calls either Observer.onCompleted() or Observer.onError(java.lang.Throwable).

      Parameters:
      v - the item emitted by the Observable
    • hasObservers

      public boolean hasObservers()
      Description copied from class: Subject
      Indicates whether the Subject has Observers subscribed to it.
      Specified by:
      hasObservers in class Subject<T,T>
      Returns:
      true if there is at least one Observer subscribed to this Subject, false otherwise
    • hasValue

      @Beta public boolean hasValue()
      Check if the Subject has a value.

      Use the getValue() method to retrieve such a value.

      Note that unless hasCompleted() or hasThrowable() returns true, the value retrieved by getValue() may get outdated.

      Returns:
      true if and only if the subject has some value but not an error
    • hasThrowable

      @Beta public boolean hasThrowable()
      Check if the Subject has terminated with an exception.
      Returns:
      true if the subject has received a throwable through onError.
    • hasCompleted

      @Beta public boolean hasCompleted()
      Check if the Subject has terminated normally.
      Returns:
      true if the subject completed normally via onCompleted()
    • getValue

      @Beta public T getValue()
      Returns the current value of the Subject if there is such a value and the subject hasn't terminated with an exception.

      The method can return null for various reasons. Use hasValue(), hasThrowable() and hasCompleted() to determine if such null is a valid value, there was an exception or the Subject terminated without receiving any value.

      Returns:
      the current value or null if the Subject doesn't have a value, has terminated with an exception or has an actual null as a value.
    • getThrowable

      @Beta public Throwable getThrowable()
      Returns the Throwable that terminated the Subject.
      Returns:
      the Throwable that terminated the Subject or null if the subject hasn't terminated yet or it terminated normally.