monifu.reactive

observers

package observers

Visibility
  1. Public
  2. All

Type Members

  1. final class BackPressuredBufferedObserver[-T] extends BufferedObserver[T]

  2. trait BufferedObserver[-T] extends Observer[T]

    Interface describing Observer wrappers that are thread-safe (can receive concurrent events) and that return an immediate Continue when receiving onNext events.

    Interface describing Observer wrappers that are thread-safe (can receive concurrent events) and that return an immediate Continue when receiving onNext events. Meant to be used by data sources that cannot uphold the no-concurrent events and the back-pressure related requirements (i.e. data-sources that cannot wait on Future[Continue] for sending the next event).

    Implementations of this interface have the following contract

    • onNext / onError / onComplete of this interface MAY be called concurrently
    • onNext SHOULD return an immediate Continue, as long as the buffer is not full and the underlying observer hasn't signaled Cancel (N.B. due to the asynchronous nature, Cancel signaled by the underlying observer may be noticed later, so implementations of this interface make no guarantee about queued events - which could be generated, queued and dropped on the floor later)
    • onNext MUST return an immediate Cancel result, after it notices that the underlying observer signaled Cancel (due to the asynchronous nature of observers, this may happen later and queued events might get dropped on the floor)
    • in general the contract for the underlying Observer is fully respected (grammar, non-concurrent notifications, etc...)
    • when the underlying observer canceled (by returning Cancel), or when a concurrent upstream data source triggered an error, this SHOULD eventually be noticed and acted upon
    • as long as the buffer isn't full and the underlying observer isn't Cancel, then implementations of this interface SHOULD not lose events in the process
    • the buffer MAY BE either unbounded or bounded, in case of bounded buffers, then an appropriate policy needs to be set for when the buffer overflows - either an onError triggered in the underlying observer coupled with a Cancel signaled to the upstream data sources, or dropping events from the head or the tail of the queue, or attempting to apply back-pressure, etc...

    See BufferPolicy for the buffer policies available.

  3. final class ConcurrentObserver[-T] extends Observer[T]

    An observer wrapper that ensures the underlying implementation does not receive concurrent onNext / onError / onComplete events - for those cases in which the producer is emitting data too fast or concurrently without fulfilling the back-pressure requirements.

    An observer wrapper that ensures the underlying implementation does not receive concurrent onNext / onError / onComplete events - for those cases in which the producer is emitting data too fast or concurrently without fulfilling the back-pressure requirements.

    The Future returned by onNext on each call is guaranteed to be completed only after downstream has processed the call.

    For high-contention scenarios it is very expensive in terms of both memory usage and throughput. If one needs to send onNext/onComplete events concurrently and buffering, but without the requirement for onNext to return a Future that's only complete when the event was processed by downstream (i.e. thread-safe buffering), then one is better served by BufferedObserver.

  4. final class ConnectableObserver[-T] extends Channel[T] with Observer[T]

    Wraps an Observer into an implementation that abstains from emitting items until the call to connect() happens.

    Wraps an Observer into an implementation that abstains from emitting items until the call to connect() happens. Prior to connect() it's also a Channel into which you can enqueue events for delivery once connect() happens, but before any items emitted by onNext / onComplete and onError.

    Example:

    val obs = ConnectableObserver(observer)
    
    // schedule onNext event, after connect()
    obs.onNext("c")
    
    // schedule event "a" to be emitted first
    obs.pushNext("a")
    // schedule event "b" to be emitted second
    obs.pushNext("b")
    
    // underlying observer now gets events "a", "b", "c" in order
    obs.connect()

    Example of an observer ended in error:

    val obs = ConnectableObserver(observer)
    
    // schedule onNext event, after connect()
    obs.onNext("c")
    
    obs.pushNext("a") // event "a" to be emitted first
    obs.pushNext("b") // event "b" to be emitted first
    
    // schedule an onError sent downstream, once connect()
    // happens, but after "a" and "b"
    obs.pushError(new RuntimeException())
    
    // underlying observer receives ...
    // onNext("a") -> onNext("b") -> onError(RuntimeException)
    obs.connect()
    
    // NOTE: that onNext("c") never happens
  5. final class SafeObserver[-T] extends Observer[T]

    A safe observer ensures two things:

    A safe observer ensures two things:

    - errors triggered by downstream observers are caught and streamed to onError, while the upstream gets an Ack.Cancel, to stop sending events

    - once an onError or onComplete was emitted, the observer no longer accepts onNext events, ensuring that the Rx grammar is respected.

    This implementation doesn't address multi-threading concerns in any way.

  6. final class SynchronousBufferedObserver[-T] extends SynchronousObserver[T] with BufferedObserver[T]

    A highly optimized BufferedObserver implementation.

    A highly optimized BufferedObserver implementation. It supports 2 buffer policies - unbounded or bounded and terminated with a BufferOverflowException.

    To create an instance using an unbounded policy:

    // by default, the constructor for BufferedObserver is returning this unbounded variant
    BufferedObserver(observer)
    
    // or you can specify the Unbounded policy explicitly
    import monifu.reactive.BufferPolicy.Unbounded
    val buffered = BufferedObserver(observer, bufferPolicy = Unbounded)

    To create a bounded buffered observable that triggers BufferOverflowException when over capacity:

    import monifu.reactive.BufferPolicy.OverflowTriggering
    // triggers buffer overflow error after 10000 messages
    val buffered = BufferedObserver(observer, bufferPolicy = OverflowTriggering(bufferSize = 10000))
  7. trait SynchronousObserver[-T] extends Observer[T]

    A SyncObserver is an Observer that signals demand to upstream synchronously (i.e.

    A SyncObserver is an Observer that signals demand to upstream synchronously (i.e. the upstream observable doesn't need to wait on a Future in order to decide whether to send the next event or not).

    Can be used for optimizations.

Ungrouped