• Stars
    star
    3,336
  • Rank 12,837 (Top 0.3 %)
  • Language
    Dart
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated 26 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Reactive Extensions for Dart

RxDart

build

Build Status codecov Pub Pub Version (including pre-releases) Gitter Flutter website Build Flutter example License Hits

About

RxDart extends the capabilities of Dart Streams and StreamControllers.

Dart comes with a very decent Streams API out-of-the-box; rather than attempting to provide an alternative to this API, RxDart adds functionality from the reactive extensions specification on top of it.

RxDart does not provide its Observable class as a replacement for Dart Streams. Instead, it offers several additional Stream classes, operators (extension methods on the Stream class), and Subjects.

If you are familiar with Observables from other languages, please see the Rx Observables vs. Dart Streams comparison chart for notable distinctions between the two.

Upgrading from RxDart 0.22.x to 0.23.x

RxDart 0.23.x moves away from the Observable class, utilizing Dart 2.6's new extension methods instead. This requires several small refactors that can be easily automated -- which is just what we've done!

Please follow the instructions on the rxdart_codemod package to automatically upgrade your code to support RxDart 0.23.x.

How To Use RxDart

For Example: Reading the Konami Code

import 'package:rxdart/rxdart.dart';

void main() {
  const konamiKeyCodes = <int>[
    KeyCode.UP,
    KeyCode.UP,
    KeyCode.DOWN,
    KeyCode.DOWN,
    KeyCode.LEFT,
    KeyCode.RIGHT,
    KeyCode.LEFT,
    KeyCode.RIGHT,
    KeyCode.B,
    KeyCode.A,
  ];

  final result = querySelector('#result')!;

  document.onKeyUp
      .map((event) => event.keyCode)
      .bufferCount(10, 1) // An extension method provided by rxdart
      .where((lastTenKeyCodes) => const IterableEquality<int>().equals(lastTenKeyCodes, konamiKeyCodes))
      .listen((_) => result.innerHtml = 'KONAMI!');
}

API Overview

RxDart adds functionality to Dart Streams in three ways:

  • Stream Classes - create Streams with specific capabilities, such as combining or merging many Streams.
  • Extension Methods - transform a source Stream into a new Stream with different capabilities, such as throttling or buffering events.
  • Subjects - StreamControllers with additional powers

Stream Classes

The Stream class provides different ways to create a Stream: Stream.fromIterable or Stream.periodic. RxDart provides additional Stream classes for a variety of tasks, such as combining or merging Streams!

You can construct the Streams provided by RxDart in two ways. The following examples are equivalent in terms of functionality:

  • Instantiating the Stream class directly.
    • Example: final mergedStream = MergeStream([myFirstStream, mySecondStream]);
  • Using static factories from the Rx class, which are useful for discovering which types of Streams are provided by RxDart. Under the hood, these factories call the corresponding Stream constructor.
    • Example: final mergedStream = Rx.merge([myFirstStream, mySecondStream]);

List of Classes / Static Factories

Extension Methods

The extension methods provided by RxDart can be used on any Stream. They convert a source Stream into a new Stream with additional capabilities, such as buffering or throttling events.

Example

Stream.fromIterable([1, 2, 3])
  .throttleTime(Duration(seconds: 1))
  .listen(print); // prints 1

List of Extension Methods

Subjects

Dart provides the StreamController class to create and manage a Stream. RxDart offers two additional StreamControllers with additional capabilities, known as Subjects:

  • BehaviorSubject - A broadcast StreamController that caches the latest added value or error. When a new listener subscribes to the Stream, the latest value or error will be emitted to the listener. Furthermore, you can synchronously read the last emitted value.
  • ReplaySubject - A broadcast StreamController that caches the added values. When a new listener subscribes to the Stream, the cached values will be emitted to the listener.

Rx Observables vs Dart Streams

In many situations, Streams and Observables work the same way. However, if you're used to standard Rx Observables, some features of the Stream API may surprise you. We've included a table below to help folks understand the differences.

Additional information about the following situations can be found by reading the Rx class documentation.

Situation Rx Observables Dart Streams
An error is raised Observable Terminates with Error Error is emitted and Stream continues
Cold Observables Multiple subscribers can listen to the same cold Observable, and each subscription will receive a unique Stream of data Single subscriber only
Hot Observables Yes Yes, known as Broadcast Streams
Is {Publish, Behavior, Replay}Subject hot? Yes Yes
Single/Maybe/Completable ? Yes Yes, uses rxdart_ext Single (Completable = Single<void>, Maybe<T> = Single<T?>)
Support back pressure Yes Yes
Can emit null? Yes, except RxJava Yes
Sync by default Yes No
Can pause/resume a subscription*? No Yes

Examples

Web and command-line examples can be found in the example folder.

Web Examples

In order to run the web examples, please follow these steps:

  1. Clone this repo and enter the directory
  2. Run dart pub get
  3. Run dart run build_runner serve example
  4. Navigate to http://localhost:8080/web/index.html in your browser

Command Line Examples

In order to run the command line example, please follow these steps:

  1. Clone this repo and enter the directory
  2. Run pub get
  3. Run dart example/example.dart 10

Flutter Example

Install Flutter

To run the flutter example, you must have Flutter installed. For installation instructions, view the online documentation.

Run the app

  1. Open up an Android Emulator, the iOS Simulator, or connect an appropriate mobile device for debugging.
  2. Open up a terminal
  3. cd into the example/flutter/github_search directory
  4. Run flutter doctor to ensure you have all Flutter dependencies working.
  5. Run flutter packages get
  6. Run flutter run

Notable References

Changelog

Refer to the Changelog to get all release notes.

Extensions

Check out rxdart_ext, which provides many extension methods and classes built on top of RxDart.

More Repositories

1

RxJava

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
Java
47,636
star
2

rxjs

A reactive programming library for JavaScript
TypeScript
30,172
star
3

RxSwift

Reactive Programming in Swift
Swift
24,144
star
4

RxAndroid

RxJava bindings for Android
Java
19,863
star
5

RxKotlin

RxJava bindings for Kotlin
Kotlin
6,997
star
6

RxGo

Reactive Extensions for the Go language.
Go
4,840
star
7

RxPY

ReactiveX for Python
Python
4,677
star
8

RxCpp

Reactive Extensions for C++
C++
2,949
star
9

RxPHP

Reactive extensions for PHP
PHP
1,680
star
10

learnrx

A series of interactive exercises for learning Microsoft's Reactive Extensions Library for Javascript.
JavaScript
1,403
star
11

RxNetty

Reactive Extension (Rx) Adaptor for Netty
Java
1,383
star
12

IxJS

The Interactive Extensions for JavaScript
TypeScript
1,284
star
13

RxRuby

Reactive Extensions for Ruby
Ruby
959
star
14

RxScala

RxScala – Reactive Extensions for Scala – a library for composing asynchronous and event-based programs using observable sequences
Scala
885
star
15

RxJavaFX

RxJava bindings for JavaFX
Java
517
star
16

RxRust

The Reactive Extensions for the Rust Programming Language
Rust
478
star
17

RxClojure

RxJava bindings for Clojure
Clojure
359
star
18

rxjs-tslint

TSLint rules targeting RxJS
TypeScript
308
star
19

RxJavaReactiveStreams

Adapter between RxJava and ReactiveStreams
Java
234
star
20

RxJavaDebug

Java
162
star
21

rxjs-docs

The home for new work on the new RxJS docs (RxJS v 5 and up). New to this space? Say hi here: https://github.com/ReactiveX/rxjs-docs/issues/24. Want to find out what's up? We're chatting here. https://github.com/ReactiveX/rxjs-docs/issues/4
TypeScript
161
star
22

RxGroovy

RxJava bindings for Groovy
Groovy
156
star
23

reactivex.github.io

ReactiveX Website
JavaScript
139
star
24

RxJavaAsyncUtil

Java
132
star
25

RxJavaString

Java
130
star
26

RxApacheHttp

RxJava bindings for Apache HTTP
Java
118
star
27

RxJavaJoins

Java
100
star
28

RxSwing

RxJava bindings for Swing
Java
97
star
29

RxJavaMath

Math operators for RxJava.
Java
96
star
30

rxjs-advent-2018

RxJS 2018 Advent Calendar
TypeScript
91
star
31

rxjs-core-notes

Notes from RxJS core meetings
77
star
32

RxJavaFileUtils

File utilities with RxJava
Java
62
star
33

RxJavaComputationExpressions

Java
60
star
34

RxJavaGuava

Java
54
star
35

RxJavaParallel

Experimental Parallel Extensions for RxJava
Java
54
star
36

RxJRuby

RxJava bindings for JRuby
Ruby
37
star
37

Rx.NET

Rx.NET – Reactive Extensions for .NET – a library for composing asynchronous and event-based programs using observable sequences for the CLR.
31
star
38

RxQuasar

RxJava bindings for Quasar
Java
16
star
39

RxRoboVM

RxJava bindings for iOS
Java
7
star
40

BuildInfrastructure

Test project for the new build system.
Groovy
5
star
41

RxSwing2

Reactive bindings for Java 8 Swing on top of RxJava 2
4
star