rxdart 0.6.3+1 copy "rxdart: ^0.6.3+1" to clipboard
rxdart: ^0.6.3+1 copied to clipboard

outdated

Native Dart rx implementation

rxdart #

Build Status Coverage Status Pub Gitter

Update This library no longer depends on RxJs and JS interop, instead it now aims to provide a native Dart implementation of Rx, allowing usage of this library in server side (or Flutter) projects as well.

Currently supported

 /// Use the observable method to wrap a Dart stream and add Rx operators to it
 Observable oStream = observable(myStream);
 
 /// Below operators are now available, next to the original Stream ones (map, where, ...)
 oStream
    .bufferWithCount
    .debounce
    .flatMapLatest
    .flatMap
    .groupBy
    .interval
    .max
    .min
    .pluck
    .repeat
    .retry
    .sample
    .scan
    .startWith
    .startWithMany
    .takeUntil
    .timeInterval
    .tap
    .throttle
    .windowWithCount
    .withLatestFrom

 /// the following are contructors
 new Observable
    .amb
    .combineLatest (deprecated - see below)
    .concat
    .concatEager
    .defer
    .just
    .merge
    .tween
    .zip (deprecated - see below)
    
 /// To better support strong mode, combineLatest and zip
 /// have now been pulled apart into fixed-length constructors.
 /// These methods are available as static methods, since class
 /// factory methods don't support generic method types.
 Observable
    .combineTwoLatest
    .combineThreeLatest
    .combineFourLatest
    .combineFiveLatest
    .combineSixLatest
    .combineSevenLatest
    .combineEightLatest
    .combineNineLatest
 
 Observable
    .zipTwo
    .zipThree
    .zipFour
    .zipFive
    .zipSix
    .zipSeven
    .zipEight
    .zipNine
    
 /// BehaviourSubject and ReplaySubject are available
 /// The default StreamController functions as a PublishSubject
 
 /// On listen, receive the last added event
 StreamController controllerA = new BehaviourSubject();
 /// On listen, receive all past events
 StreamController controllerB = new ReplaySubject();

Example:

void main() {
  var codes = <int>[
      38, // up
      38, // up
      40, // down
      40, // down
      37, // left
      39, // right
      37, // left
      39, // right
      66, // b
      65  // a
  ];
  var result = querySelector('#result');
  var controller = new StreamController<KeyboardEvent>();
  var stream = rx.observable(controller.stream);

  document.addEventListener('keyup', (event) => controller.add(event));

  stream
    .map((event) => event.keyCode )           // get the key code
    .bufferWithCount(10, 1)                   // get the last 10 keys
    .where((list) => _areTwoListsEqual(list, codes))
    .listen((_) => result.innerHtml = 'KONAMI!');
}

bool _areTwoListsEqual(List<int> a, List<int> b) {
  for (int i=0; i<10; i++) if (a[i] != b[i]) return false;
  
  return true;
}