Class OnSubscribePublishMulticast<T>

java.lang.Object
java.lang.Number
java.util.concurrent.atomic.AtomicInteger
rx.internal.operators.OnSubscribePublishMulticast<T>
Type Parameters:
T - the input and output type
All Implemented Interfaces:
Serializable, Action, Action1<Subscriber<? super T>>, Function, Observable.OnSubscribe<T>, Observer<T>, Subscription

public final class OnSubscribePublishMulticast<T> extends AtomicInteger implements Observable.OnSubscribe<T>, Observer<T>, Subscription
Multicasts notifications coming through its input Subscriber view to its client Subscribers via lockstep backpressure mode.

The difference between this class and OperatorPublish is that this class doesn't consume the upstream if there are no child subscribers but waits for them to show up. Plus if the upstream source terminates, late subscribers will be immediately terminated with the same terminal event unlike OperatorPublish which just waits for the next connection.

The class extends AtomicInteger which is the work-in-progress gate for the drain-loop serializing subscriptions and child request changes.

See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • queue

      final Queue<T> queue
      The prefetch queue holding onto a fixed amount of items until all all child subscribers have requested something.
    • prefetch

      final int prefetch
      The number of items to prefetch from the upstreams source.
    • delayError

      final boolean delayError
      Delays the error delivery to happen only after all values have been consumed.
    • parent

      The subscriber that can be 'connected' to the upstream source.
    • done

      volatile boolean done
      Indicates the upstream has completed.
    • error

      Throwable error
      Holds onto the upstream's exception if done is true and this field is non-null.

      This field must be read after done or if subscribers == TERMINATED to establish a proper happens-before.

    • producer

      volatile Producer producer
      Holds the upstream producer if any, set through the parent subscriber.
    • subscribers

      A copy-on-write array of currently subscribed child subscribers' wrapper structure.
    • EMPTY

      Represents an empty array of subscriber wrapper, helps avoid allocating an empty array all the time.
    • TERMINATED

      static final OnSubscribePublishMulticast.PublishProducer<?>[] TERMINATED
      Represents a final state for this class that prevents new subscribers from subscribing to it.
  • Constructor Details

    • OnSubscribePublishMulticast

      public OnSubscribePublishMulticast(int prefetch, boolean delayError)
      Constructor, initializes the fields
      Parameters:
      prefetch - the prefetch amount, > 0 required
      delayError - delay the error delivery after the normal items?
      Throws:
      IllegalArgumentException - if prefetch <= 0
  • Method Details

    • call

      public void call(Subscriber<? super T> t)
      Specified by:
      call in interface Action1<T>
    • onNext

      public void onNext(T t)
      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).

      Specified by:
      onNext in interface Observer<T>
      Parameters:
      t - the item emitted by the Observable
    • 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().

      Specified by:
      onError in interface Observer<T>
      Parameters:
      e - the exception encountered by the Observable
    • 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).

      Specified by:
      onCompleted in interface Observer<T>
    • setProducer

      void setProducer(Producer p)
      Sets the main producer and issues the prefetch amount.
      Parameters:
      p - the producer to set
    • drain

      void drain()
      The serialization loop that determines the minimum request of all subscribers and tries to emit as many items from the queue if they are available.

      The execution of the drain-loop is guaranteed to be thread-safe.

    • checkTerminated

      boolean checkTerminated(boolean d, boolean empty)
      Given the current source state, terminates all child subscribers.
      Parameters:
      d - the source-done indicator
      empty - the queue-emptiness indicator
      Returns:
      true if the class reached its terminal state
    • terminate

      Atomically swaps in the terminated state.
      Returns:
      the last set of subscribers before the state change or an empty array
    • add

      Atomically adds the given wrapper of a child Subscriber to the subscribers array.
      Parameters:
      inner - the wrapper
      Returns:
      true if successful, false if the terminal state has been reached in the meantime
    • remove

      Atomically removes the given wrapper, if present, from the subscribers array.
      Parameters:
      inner - the wrapper to remove
    • subscriber

      public Subscriber<T> subscriber()
      Returns the input subscriber of this class that must be subscribed to the upstream source.
      Returns:
      the subscriber instance
    • unsubscribe

      public void unsubscribe()
      Description copied from interface: Subscription
      Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.

      This allows unregistering an Subscriber before it has finished receiving all events (i.e. before onCompleted is called).

      Specified by:
      unsubscribe in interface Subscription
    • isUnsubscribed

      public boolean isUnsubscribed()
      Description copied from interface: Subscription
      Indicates whether this Subscription is currently unsubscribed.
      Specified by:
      isUnsubscribed in interface Subscription
      Returns:
      true if this Subscription is currently unsubscribed, false otherwise