Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Programming with RxJS

Hugo Cordier
February 25, 2015

Reactive Programming with RxJS

Introduction to the Reactive Extensions.
What's an Observable?
In which use cases should I use it?

Hugo Cordier

February 25, 2015
Tweet

More Decks by Hugo Cordier

Other Decks in Programming

Transcript

  1. RxJS I am Hugo Cordier CTO at Melusyn Javascript developer

    Java/Groovy developer You can find me at: @HugoCrd With a lot of non related stock photos
  2. Callbacks & Promises This guy with an iPhone has nothing

    to do with FRP. But look at this beautiful slide!
  3. Callbacks - Welcome to hell getAsync1(function (r1, e) { if

    (er) return error(e) getAsync2(function (r2, e) { if (er) return error(e) getAsync3(function (r3, e) { if (er) return error(e) getAsync4(function (r4, e) { doSomething(r1, r2, r3, r4) }) }) }) }) Error handling becomes messy Hard to understand what the developer intended to do
  4. Promises - Good answer to callbacks var promisedR1 = getAsync1()

    var promisedR2 = getAsync2() var promisedR3 = getAsync3() Promise .all([promisedR1,promisedR2,promisedR3]) .then(function(results) { doSomething(results) }) That’s neat! But...
  5. Promises - But... You can’t use Promises to ➜ Process

    events (UI, Push from server, Angular-style $watch, ...) ➜ Use time-related logic (Timeout, Delay, Debounce, …) ➜ Compose async sources together
  6. Rx - Observables An Observable can : ➜ emit events

    (0 to ∞) ➜ emit an Error ➜ complete stream Once an Error has been emitted, no further events can be. An Observable could not complete.
  7. Rx - Observable are asynchronous Iterables Single return value Mutiple

    return values Synchronous Object Iterables(Array | Set | Map) Asynchronous Promise Observable
  8. Rx - Array/Observable parallel Iterable (Array) [1,2,3,4] .map(v => v+1)

    .filter(v => v>2) .forEach(v => { console.log(v) }) Observable observableSource .map(v => v+1) .filter(v => v>2) .subscribe(v => { console.log(v) }) This is asynchronous
  9. Rx - Create an Observable // Manually var source =

    Rx.Observable.create(function (observer) { observer.onNext(12); observer.onNext(34); observer.onNext(5); observer.onNext(8); observer.onCompleted(); //or observer.onError(“Oops”); });
  10. Rx - Create an Observable // From object Rx.Observable.just(12) //

    From array Rx.Observable.from([12,34,5,6]) // From interval Rx.Observable.interval(100) // From promise Rx.Observable.fromPromise(promise) // From DOM event Rx.Observable.fromEvent(document.getElementById ('searchField'), 'keyup')
  11. Rx - Create an Observable // From Node.js Callback, Events,

    Streams Rx.Node.fromNodeCallback(fs.stat)('file.txt') Rx.Node.fromEvent(eventEmitter, 'eventName') Rx.Node.fromStream(process.stdin) Rx.Node.fromReadableStream(process.stdin) Rx.Node.fromTransformStream(getTransformStreamSomehow()) // From Angular $watch Rx.Observable.$watch(scope, 'search') .flatMap(search => $http.get('api/search/'+search)) .subscribe(result => console.log(result))
  12. ➜ map, filter, reduce, skip, take ➜ zip, merge, concat

    ➜ repeat, retry ➜ delay, debounce, interval ➜ window, buffer ➜ flatmap https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/observable.md Rx - Operators
  13. ➜ Rx.NET ➜ RxJava, RxScala, RxGroovy, RxJRuby, RxNetty, RxAndroid ➜

    Rx.rb ➜ Rx.py ➜ RxClojure ➜ RxCpp ➜ RxKotlin Reactive Extensions world
  14. Javascript : ➜ Bacon.js ➜ Kefir ➜ ELM (language) Not

    javascript : ➜ Reactive-banana, Frappuccino, React4j, ReactiveCocoa, Sodium Other FRP libraries
  15. CREDITS Special thanks to all the people who made and

    released these awesome resources for free: ➜ Simple line icons by Mirko Monti ➜ E-commerce icons by Virgil Pana ➜ Streamline iconset by Webalys ➜ Presentation template by SlidesCarnival ➜ Photographs by Death to the Stock Photo (license)