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

Programação Reativa com RxJS

Programação Reativa com RxJS

Jonata Weber

May 27, 2017
Tweet

More Decks by Jonata Weber

Other Decks in Technology

Transcript

  1. Async em Web apps • AJAX • User Events (mouse

    clicks, mouvemoves, keyups, etc) • Animations • Sockets • Workers
  2. callback É a maneira mais primitiva de trabalhar com async

    no JavaScript getSomeData((data) => { console.log('data received', data); });
  3. Promises Provê uma solução simples getData(id) .then(data => { doStuff(data);

    return getSomeData(data.parentId); }) .then(data => { doStuff(data); return getSomeData(data.parentId); })
  4. Promises let promise = new Promise((resolve, reject) => { let

    success = doSomethingAsync(); if (success) { resolve("Stuff worked!"); } else { reject(Error("It broke")); } });
  5. Promises let promise = new Promise((resolve, reject) => { let

    success = doSomethingAsync(); if (success) { resolve("Stuff worked!"); } else { reject(Error("It broke")); } });
  6. Promises let promise = new Promise((resolve, reject) => { let

    success = doSomethingAsync(); if (success) { resolve("Stuff worked!"); } else { reject(Error("It broke")); } });
  7. ReactiveX is a combination of the best ideas from
 the

    Observer pattern, the Iterator pattern, and functional programming
  8. Java: RxJava JavaScript: RxJS C#: Rx.NET Python: RxPY Ruby: Rx.rb


    PHP: RxPHP Go: RxGo Kotlin: RxKotlin C++: RxCpp Lua: RxLua Groovy: RxGroovy JRuby: RxJRuby Swift: RxSwift Elixir: reaxive Dart: RxDart C# (Unity): UniRx
  9. Hello World Rx.Observable .of(1, 2, 3, 4, 5) .subscribe(n =>

    console.log(n)); http://jsbin.com/muxigepeju/edit?js,console
  10. const btn = document.querySelector('button'); Rx.Observable.fromEvent(btn, 'click') .throttleTime(250) .scan(count => count

    + 1, 0) .subscribe(count => console.log(`Clicked ${count} times`)); Contador de Cliques http://jsbin.com/qifumej/edit?js,console,output
  11. Transformation Operators • buffer • bufferCount • bufferTime • bufferToggle

    • bufferWhen • concatMap • concatMapTo • exhaustMap • expand • groupBy • map • mapTo • mergeMap • mergeMapTo • mergeScan • pairwise • partition • pluck • scan • switchMap • switchMapTo • window • windowCount • windowTime • windowToggle • windowWhen
  12. let observable = Rx.Observable.create((observer) => { observer.next(1); observer.next(2); observer.next(3); setTimeout(()

    => { observer.next(4); observer.complete(); }, 1000); }); console.log('just before subscribe'); observable.subscribe({ next: x => console.log('got value ' + x), error: err => console.error('something wrong occurred: ' + err), complete: () => console.log('done'), }); console.log('just after subscribe');