rxdart 0.9.0 rxdart: ^0.9.0 copied to clipboard
Native Dart rx implementation
RxDart #
About #
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.
How To Use RxDart #
For Example: Reading the Konami Code #
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)
.bufferWithCount(10, 1)
.where((lastTenKeyCodes) => const IterableEquality<int>().equals(lastTenKeyCodes, konamiKeyCodes))
.listen((_) => result.innerHtml = 'KONAMI!');
}
API Overview #
RxDart's Observables extend the Stream class. This has two major implications:
- All methods defined on the Stream class exist on RxDart's Observables as well.
- All Observables can be passed to any API that expects a Dart Stream as an input.
Instantiation #
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.
Usage
var myObservable = new Observable(myStream);
Available Factory Methods
- amb
- concat
- defer
- error
- just
- merge
- never
- periodic
- retry
- timer
Usage
var myObservable = new Observable.merge([myFirstStream, mySecondStream]);
Available Static Methods
- combineLatest (combineLatest2, combineLatest3, combineLatest4, ..., combineLatest9)
- range
- tween
- zip (zip2, zip3, zip4, ..., zip9)
Usage
var myObservable = Observable.combineLatest3(
myFirstStream,
mySecondStream,
myThirdStream,
(firstData, secondData, thirdData) => print("$firstData $secondData $thirdData"));
Transformations #
Available Methods
- bufferWithCount
- call
- concatMap
- concatWith
- debounce
- dematerialize
- flatMapLatest
- flatMap
- groupBy
- interval
- materialize
- mergeWith
- max
- min
- pluck
- repeat
- sample
- scan
- skipUntil
- startWith
- startWithMany
- takeUntil
- timeInterval
- timestamp
- throttle
- windowWithCount
- withLatestFrom
- zipWith
Usage
var myObservable = new Observable(myStream)
.bufferWithCount(5)
.distinct();
Objects #
- Observable
- BehaviourSubject
- ReplaySubject
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:
- Clone this repo and enter the directory
- Run
pub get
- Run
pub serve example/web
- Navigate to http://localhost:8080 in your browser
Command Line Examples #
In order to run the command line example, please follow these steps:
- Clone this repo and enter the directory
- Run
pub get
- Run
dart example/bin/fibonacci.dart 10
Flutter Example #
Install Flutter
In order to run this repo, you must have Flutter installed. For help getting started with Flutter, view the online documentation.
Run the app
- Open up an Android Emulator, the iOS Simulator, or connect an appropriate mobile device for debugging.
- Open up a terminal
cd
into theexample/flutter/github_search
directory- Run
flutter doctor
to ensure you have all Flutter dependencies working. - Run
flutter packages get
- Run
flutter run
Notable References #
Changelog #
Refer to the Changelog to get all release notes.