RxDart is a reactive functional programming library for Google Dart, based on ReactiveX.
Google 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 on top of it.
Dart 1.0 is supported until release 0.15.x, version 0.16.x is no longer backwards compatible and requires the Dart SDK 2.0
void main() {
const konamiKeyCodes = const <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');
final keyUp = new Observable<KeyboardEvent>(document.onKeyUp);
keyUp
.map((event) => event.keyCode)
.bufferCount(10, 1)
.where((lastTenKeyCodes) => const IterableEquality<int>().equals(lastTenKeyCodes, konamiKeyCodes))
.listen((_) => result.innerHtml = 'KONAMI!');
}
RxDart's Observables extends the Stream class. This has two major implications:
Finally, the Observable class & operators are simple wrappers around Stream
and StreamTransformer
classes. All underlying implementations can be used free of the Observable class, and are exposed in their own libraries. They are linked to below.
Generally speaking, creating a new Observable is either done by wrapping a Dart Stream using the top-level constructor new Observable()
, or by calling a factory method on the Observable class.
But to better support Dart's strong mode, combineLatest
and zip
have been pulled apart into fixed-length constructors.
These methods are supplied as static methods, since Dart's factory methods don't support generic types.
var myObservable = new Observable(myStream);
var myObservable = new Observable.merge([myFirstStream, mySecondStream]);
var myObservable = Observable.combineLatest3(
myFirstStream,
mySecondStream,
myThirdStream,
(firstData, secondData, thirdData) => print("$firstData $secondData $thirdData"));
asyncExpand
)A full list of all methods and properties including those provided by the Dart Stream API (such as first
, asyncMap
, etc), can be seen by examining the DartDocs
var myObservable = new Observable(myStream)
.bufferCount(5)
.distinct();
Web and command-line examples can be found in the example
folder.
In order to run the web examples, please follow these steps:
pub get
pub serve example/web
In order to run the command line example, please follow these steps:
pub get
dart example/bin/fibonacci.dart 10
In order to run the flutter example, you must have Flutter installed. For installation instructions, view the online documentation.
cd
into the example/flutter/github_search
directoryflutter doctor
to ensure you have all Flutter dependencies working.flutter packages get
flutter run
Refer to the Changelog to get all release notes.
stream
property on Observable is now private.stream
property to use a super
constructor instead amb
/race
, combineLatest
, concat
, concat_eager
, merge
and zip
switchLatest
operatoronErrorResume
and onErrorRetryWith
operators. These allow folks to return a specific stream or value depending on the error that occurred. buffer
and window
, allow to schedule using a samplerbuffer
bufferFuture
bufferTest
bufferTime
bufferWhen
window
windowFuture
windowTest
windowTime
windowWhen
onCount
sampler for buffer
and window
onFuture
sampler for buffer
and window
onTest
sampler for buffer
and window
onTime
sampler for buffer
and window
onStream
sampler for buffer
and window
amb
to race
flatMapLatest
to switchMap
bufferWithCount
to bufferCount
windowWithCount
to windowCount
bufferTime
transformer.windowTime
transformer.delay
transformer.sink
are not processed correctly by Subjects
.dematerialize
method for Dart 2.value
to BehaviorSubject
. Allows you to get the latest value emitted by the subject if it exists.values
to ReplayrSubject
. Allows you to get the values stored by the subject if any exists.cast
in favour of the now native Stream cast method.retype
to return an Observable
.exhaustMap
map to inner observable, ignore other values until that observable completes.debounce
to emit the last item of the source stream as soon as the source stream completes.debounce
does not keep open any addition async timers after it has been cancelled.DoStreamTransformer
to return a Future
on cancel for api compatibility. PublishSubject
(thanks to @pauldemarco)doOnX
operators where callbacks were fired too oftencall
operator / StreamTransformer
entirelyStreamSubscription
.call
.DoStreamTransformer
as a replacement for CallStreamTransformer
call
and CallStreamTransformer
. Please use the appropriate doOnX
operator / transformer.distinctUnique
. Emits items if they've never been emitted before. Same as to Rx#distinct.Stream
class in Dart 1.24.0, which includes this methodobservable
factory -- replaced by the constructor new Observable()
combineLatest
-- replaced by Strong-Mode versions combineLatest2
- combineLatest9
zip
-- replaced by Strong-Mode versions zip2
- zip9
asObservable
conversion from Future-returning methods. e.g. new Observable.fromIterable([1, 2]).first.asObservable()
cast
operatorConcatMapStreamTransformer
-- functionality is already supported by asyncExpand
. Keep the concatMap
method as an alias.example/example.dart
import 'package:rxdart/rxdart.dart';
/// generate n-amount of fibonacci numbers
///
/// for example: dart fibonacci.dart 10
/// outputs:
/// 1: 1
/// 2: 1
/// 3: 2
/// 4: 3
/// 5: 5
/// 6: 8
/// 7: 13
/// 8: 21
/// 9: 34
/// 10: 55
/// done!
void main(List<String> arguments) {
// read the command line argument, if none provided, default to 10
var n = (arguments.length == 1) ? int.parse(arguments.first) : 10;
// seed value: this value will be used as the
// starting value for the [scan] method
const seed = const IndexedPair(1, 1, 0);
Observable
// amount of numbers to compute
.range(1, n)
// accumulator: computes a new accumulated
// value each time a [Stream] event occurs
// in this case, the accumulated value is always
// the latest Fibonacci number
.scan((IndexedPair seq, _, __) => new IndexedPair.next(seq), seed)
// finally, print the output
.listen(print, onDone: () => print('done!'));
}
class IndexedPair {
final int n1, n2, index;
const IndexedPair(this.n1, this.n2, this.index);
factory IndexedPair.next(IndexedPair prev) => new IndexedPair(
prev.n2, prev.index <= 1 ? prev.n1 : prev.n1 + prev.n2, prev.index + 1);
@override
String toString() => '$index: $n2';
}
Add this to your package's pubspec.yaml file:
dependencies:
rxdart: ^0.17.0
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:rxdart/rxdart.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.20.0 | Dec 12, 2018 |
|
|
0.19.0 | Oct 12, 2018 |
|
|
0.18.1 | Jul 3, 2018 |
|
|
0.18.0 | Jun 19, 2018 |
|
|
0.17.0 | Jun 7, 2018 |
|
|
0.16.7 | May 2, 2018 |
|
|
0.16.6 | Apr 2, 2018 |
|
|
0.16.5 | Mar 30, 2018 |
|
|
0.16.4 | Mar 29, 2018 |
|
|
0.16.3 | Mar 28, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
100
|
Health:
Code health derived from static analysis.
[more]
|
71
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
91
|
We analyzed this package on Feb 4, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:rxdart/rxdart.dart
.
Fix lib/src/observable.dart
. (-26.49 points)
Analysis of lib/src/observable.dart
failed with 1 error, 4 hints:
line 1909 col 33: The method 'retype' isn't defined for the class 'Stream<T>'.
line 458 col 69: Use =
to separate a named parameter from its default value.
line 677 col 26: Use =
to separate a named parameter from its default value.
line 677 col 41: Use =
to separate a named parameter from its default value.
line 1908 col 17: Method doesn't override an inherited method.
Fix lib/src/transformers/sample.dart
. (-1 points)
Analysis of lib/src/transformers/sample.dart
reported 2 hints:
line 17 col 30: Use =
to separate a named parameter from its default value.
line 26 col 30: Use =
to separate a named parameter from its default value.
Fix lib/src/streams/defer.dart
. (-0.50 points)
Analysis of lib/src/streams/defer.dart
reported 1 hint:
line 23 col 50: Use =
to separate a named parameter from its default value.
Fix additional 4 files with analysis or formatting issues. (-2 points)
Additional issues in the following files:
lib/src/subjects/behavior_subject.dart
(1 hint)lib/src/subjects/publish_subject.dart
(1 hint)lib/src/subjects/replay_subject.dart
(1 hint)lib/src/subjects/subject.dart
(1 hint)Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0-dev <3.0.0 | ||
Dev dependencies | |||
benchmark_harness | >=1.0.0 <2.0.0 | ||
browser | ^0.10.0+2 | ||
coverage | ^0.9.2 | ||
stack_trace | ^1.9.2 | ||
test | ^0.12.34 |