rxdart 0.3.3 copy "rxdart: ^0.3.3" to clipboard
rxdart: ^0.3.3 copied to clipboard

outdated

Native Dart rx implementation

rxdart #

Build Status Coverage Status Pub

Update This library no longer depends on RxJs and JS interop, while that dependency made things easy, it also meant that rxdart would be a browser lib only.

Some notes:

  • Observable is the main class, it extends Stream obviously
  • Promote a stream to observable by wrapping it => rx.observable(myStream)
  • That doesn't mean wrapping everywhere, dart new rx.Observable.merge(<Stream>[a, b, c, ...]); is fine
  • Subject is not ported, we already have StreamController for that

Example:

void main() {
  List<int> codes = <int>[
      38, // up
      38, // up
      40, // down
      40, // down
      37, // left
      39, // right
      37, // left
      39, // right
      66, // b
      65  // a
  ];
  Element result = querySelector('#result');

  StreamController<KeyboardEvent> controller = new StreamController<KeyboardEvent>();
  rx.Observable<KeyboardEvent> stream = rx.observable(controller.stream);

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

  stream
    .map((KeyboardEvent e) => e.keyCode )     // get the key code
    .bufferWithCount(10, 1)                   // get the last 10 keys
    .where((List<int> x) => _equal(x, codes)) // where we match
    .listen((_) => result.innerHtml = 'KONAMI!');
}

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