ReactiveX Project Documentation

repository·develop·Indexed 40 days ago

https://github.com/reactivex/reactivex.github.io

Documentation for the ReactiveX project, including guides for RxJava, RxJS, RxSwift, Rx.NET, RxCpp, and RxPy. It covers core concepts like Observables, Operators, and Observers, as well as installation instructions for RxJava 0.19.0 and 0.17.0 via Maven, Ivy, and SBT.

Tokens
43.1K
Snippets
60
Records
160
Agent score
48%

What's inside ReactiveX

  1. Overview of Polymer

    develop
    Polymer is a web library designed to leverage browser infrastructure (like Shadow DOM and Custom Elements) to provide encapsulation and extendability. It is built on the principles of Web Components and Model Driven Views. While many of its core features currently rely on polyfills or shims, the library is designed to transition to native browser implementations as they become standard.
  2. Find ReactiveX implementations for different languages

    develop

    ReactiveX is available for a wide variety of programming languages. Use the following list to find the specific implementation for your development environment:

    Core Languages

  3. Explore ReactiveX Operators

    develop

    The ReactiveX documentation provides a comprehensive index of operators available across various implementations (e.g., RxJava, RxJS, Rx.NET, RxJS, RxClojure, Rx.rb). Operators are used to transform, filter, combine, and manage asynchronous data streams.

    Common categories of operators include:

    • Creation Operators: Just, Range, Interval, Never, Items.
    • Transformation Operators: Map, Select, Scan, Reduce, Materialize.
    • Filtering Operators: Filter, Skip, Take, OfType, Distinct.
    • Combination Operators: Merge, Zip, Join, GroupBy.
    • Error Handling Operators: Retry, OnErrorResumeNext, Catch.
    • Time-based Operators: Sample, Debounce, Throttle, Delay.
    • Lifecycle/Subscription Operators: Publish, Replay, RefCount, Share.
  4. What is a Subject and how does it work?

    develop

    A Subject acts as both an Observer and an Observable.

    • As an Observer: It can subscribe to one or more Observables.
    • As an Observable: It can pass through the items it observes by reemitting them and can also emit new items directly.

    By subscribing to an Observable, a Subject can turn a "cold" Observable (one that waits for a subscription to start emitting) into a "hot" Observable variant.

    Note on Thread Safety: In most implementations, the observer side of a Subject is not thread-safe. While Subscribe() is always thread-safe, calling OnNext, OnError, or OnCompleted from multiple threads can lead to an undefined state. To make the observer side thread-safe, look for the ToSerialized operator in your specific implementation.

  5. What is a Single and how does it work?

    develop

    A Single is a variant of an event source (Observable) that is guaranteed to emit exactly one item or an error notification. Unlike an Observable, which can emit a sequence of events using onNext, onError, and onCompleted, a Single simplifies the lifecycle by using only two methods:

    • onSuccess: Called when the single event is emitted.
    • onError: Called when a Throwable occurs and the single event cannot be emitted.

    A Single will call exactly one of these methods exactly once. Once either method is called, the Single terminates and the subscription ends.

  6. What is ReactiveX and the Observable model?

    develop

    ReactiveX is a library for composing asynchronous and event-based programs using Observable sequences. It extends the Observer pattern to support sequences of data and/or events, providing operators to compose these sequences declaratively.

    The Observable Abstraction

    An Observable<T> is a single abstraction used to handle various data flows, including single scalar values, sequences of values, or infinite streams. It acts as the asynchronous/push "dual" to the synchronous/pull Iterable.

    EventIterable (pull)Observable (push)
    Retrieve dataT next()onNext(T)
    Discover errorthrows ExceptiononError(Exception)
    Complete!hasNext()onCompleted()

    Key Characteristics

    • Composable: Designed to compose flows and sequences of asynchronous data without the complexity of nested Futures or callback hell.
    • Flexible: Can handle both single items and multiple items, whether synchronous or asynchronous.
    • Implementation Agnostic: From the consumer's perspective, it doesn't matter if the Observable is implemented via thread-pools, event loops, non-blocking I/O, or Actors. The client treats all interactions as asynchronous.
    // Comparison of data access patterns
    // Synchronous / Single Item: T getData()
    // Asynchronous / Single Item: Future<T> getData()
    // Synchronous / Multiple Items: Iterable<T> getData()
    // Asynchronous / Multiple Items: Observable<T> getData()
  7. What is a Subject in ReactiveX

    develop

    A Subject acts as both an Observer and an Event Source (Observable).

    • As an Observer: It can subscribe to one or more event sources.
    • As an Event Source: It can pass along events it observes or emit new events directly.

    Because a Subject subscribes to an event source, it can effectively turn a "cold" observable (one that waits for a subscription to start emitting) into a "hot" observable.

  8. What is a Single and how to subscribe to it

    develop

    A Single is a variant of an Observable that is designed to emit exactly one value or an error notification. Unlike an Observable, which can emit zero to infinite values, a Single always terminates after its first emission or error.

    When subscribing to a Single, you use two specific methods instead of the standard three (onNext, onError, onCompleted):

    • onSuccess: Called with the single item emitted by the Single.
    • onError: Called with the Throwable that caused the Single to fail.

    A Single will call only one of these methods exactly once, after which the subscription ends.

  9. The Observable Contract: onNext, onError, and onCompleted

    develop

    When an Observer subscribes to an Observable via the subscribe method, the Observable interacts with the Observer using three primary methods. According to the Observable contract, an Observable can call onNext zero or more times, followed by exactly one terminal notification: either onCompleted or onError.

    MethodDescription
    onNextCalled when the Observable emits an item. It receives the item as a parameter.
    onErrorCalled when the Observable encounters an error or fails to generate data. This is a terminal event; no further onNext or onCompleted calls will occur.
    onCompletedCalled after the Observable has successfully emitted all items without error. This is a terminal event.

    Emissions refer to onNext calls, while Notifications refer to the terminal onCompleted or onError calls.

    def myOnNext     = { item -> /* process item */ };
    def myError      = { throwable -> /* handle error */ };
    def myComplete   = { /* perform cleanup */ };
    def myObservable = someMethod(itsParameters);
    
    // Full subscription pattern
    myObservable.subscribe(myOnNext, myError, myComplete);
  10. Compose sequences using Observable Operators

    develop

    The power of ReactiveX comes from operators—functions that allow you to transform, combine, manipulate, and work with the sequences of items emitted by Observables.

    Chaining Operators

    Most operators return a new Observable, allowing them to be chained together.

    Crucial Note: Unlike the Builder Pattern where method order often doesn't matter, in ReactiveX operator order matters. Each operator in a chain operates on the Observable generated by the operator immediately preceding it in the chain.

  11. Use AsyncSubject

    develop

    An AsyncSubject emits only the last value emitted by the source Observable, and only after the source Observable completes.

    • If the source emits no values, the AsyncSubject completes without emitting anything.
    • Subsequent observers will receive this same final value.
    • If the source terminates with an error, the AsyncSubject will not emit any items but will pass along the error notification.