ReactiveX Project Documentation
repository·develop·Indexed 40 days ago
https://github.com/reactivex/reactivex.github.ioDocumentation 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.
What's inside ReactiveX
- 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.
Find ReactiveX implementations for platforms and frameworks
developReactiveX provides specialized implementations for specific platforms and frameworks:
- RxNetty: For Netty-based networking.
- RxAndroid: For Android development.
- RxCocoa: For iOS development (Swift/Objective-C).
Find ReactiveX implementations for different languages
developReactiveX is available for a wide variety of programming languages. Use the following list to find the specific implementation for your development environment:
Core Languages
- Java: RxJava
- JavaScript: RxJS
- C#: Rx.NET
- C# (Unity): UniRx (Note: no further developments)
- Scala: RxScala
- Clojure: RxClojure
- C++: RxCpp
- Lua: RxLua
- Perl: RxPerl
- Ruby: Rx.rb
- Python: RxPY
- Go: RxGo
- Groovy: RxGroovy
- JRuby: RxJRuby
- Kotlin (RxJava): RxKotlin
- Kotlin (Multiplatform): Reaktive
- Swift: RxSwift
- PHP: RxPHP
- Elixir: reaxive
- Dart: RxDart
- Julia: Rocket
Explore ReactiveX Operators
developThe 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.
- Creation Operators:
What is a Subject and how does it work?
developA 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, callingOnNext,OnError, orOnCompletedfrom multiple threads can lead to an undefined state. To make the observer side thread-safe, look for theToSerializedoperator in your specific implementation.What is a Single and how does it work?
developA
Singleis a variant of an event source (Observable) that is guaranteed to emit exactly one item or an error notification. Unlike anObservable, which can emit a sequence of events usingonNext,onError, andonCompleted, aSinglesimplifies the lifecycle by using only two methods:onSuccess: Called when the single event is emitted.onError: Called when aThrowableoccurs and the single event cannot be emitted.
A
Singlewill call exactly one of these methods exactly once. Once either method is called, theSingleterminates and the subscription ends.What is ReactiveX and the Observable model?
developReactiveX 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/pullIterable.Event Iterable (pull) Observable (push) Retrieve data T next()onNext(T)Discover error throws 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
Observableis 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()What is a Subject in ReactiveX
developA
Subjectacts 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
Subjectsubscribes to an event source, it can effectively turn a "cold" observable (one that waits for a subscription to start emitting) into a "hot" observable.What is a Single and how to subscribe to it
developA
Singleis a variant of anObservablethat is designed to emit exactly one value or an error notification. Unlike anObservable, which can emit zero to infinite values, aSinglealways 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 theSingle.onError: Called with theThrowablethat caused theSingleto fail.
A
Singlewill call only one of these methods exactly once, after which the subscription ends.The Observable Contract: onNext, onError, and onCompleted
developWhen an Observer subscribes to an Observable via the
subscribemethod, the Observable interacts with the Observer using three primary methods. According to the Observable contract, an Observable can callonNextzero or more times, followed by exactly one terminal notification: eitheronCompletedoronError.Method Description 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 onNextoronCompletedcalls will occur.onCompletedCalled after the Observable has successfully emitted all items without error. This is a terminal event. Emissions refer to
onNextcalls, while Notifications refer to the terminalonCompletedoronErrorcalls.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);Compose sequences using Observable Operators
developThe 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.
Use AsyncSubject
developAn
AsyncSubjectemits only the last value emitted by the source Observable, and only after the source Observable completes.- If the source emits no values, the
AsyncSubjectcompletes without emitting anything. - Subsequent observers will receive this same final value.
- If the source terminates with an error, the
AsyncSubjectwill not emit any items but will pass along the error notification.
- If the source emits no values, the