# RXJS ppp <small>by [Dmitry Podgorniy](https://podgorniy.net)</small> --- ## Today: * What is RXJS * Why it's good * RXJS at the Metafactory --- ## Note on RX implementation RX ideas have cross-language implementations. [JS](https://github.com/ReactiveX/rxjs) and of course [java](https://github.com/ReactiveX/RxJava). We will talk tabout RXJS --- ## RXJS self-description RXJS is a library for composing asynchronous and event-based programs by using observable sequences --- ## RXJS RXJS is a <span style="background:magenta;display:inline-block;border-radius:0.25em;">library</span> for composing asynchronous and event-based programs by using observable sequences. --- ## It's a library I.e. you use provided building blocks: [few classes](https://rxjs-dev.firebaseapp.com/guide/overview) and [many many functions](https://www.learnrxjs.io/learn-rxjs/operators) to model desired behaviour. --- ## Look-and-feel of imports ``` import { BehaviorSubject, combineLatest, forkJoin, Observable, of } from 'rxjs'; ``` <small>taken from a real-life project</small> --- ## RXJS RXJS is a library for <span style="background:magenta;display:inline-block;border-radius:0.25em;">composing</span> asynchronous and event-based programs by using observable sequences --- ## Regular day of composing ```js combineLatest([this.selectedFilters, this.refreshRequest]).pipe( debounceTime(16), distinctUntilChanged((a, b) => { return JSON.stringify(a) === JSON.stringify(b); }), switchMap(([filters]) => { this.isLoading.next(true); return forkJoin([ this.ccddAPI.getCarConfigurations(filters), this.ccddAPI.getFilters(filters) ]); }), tap(() => { this.isLoading.next(false); }), shareReplay({ bufferSize: 1, refCount: true, }) ); ``` --- ## RXJS RXJS is a library for composing asynchronous and event-based programs by using <span style="background:magenta;display:inline-block;border-radius:0.25em;">observable sequences</span> --- ## Similarities with arrays * Transform with join, split, filter, map, reduce and combine. --- ## Similarities with promises * act a "proxy" to a value * describe what to do with value before having one * async processing * combine into chains --- ## Differences from arrays and promises * represents future/past values (i.e. state) * lazy * values can be pushed to the consumer (i.e. weak coupling) --- ## RXJS RXJS is a library for composing <span style="background:magenta;display:inline-block;border-radius:0.25em;">asynchronous and event-based programs</span> by using observable sequences --- ## Async and event based programs * User interfaces * High performant (separating computation from IO) --- ## RX is a wide metaphor Includes values, arrays, promises, streams, scheduling --- ## What you can model with RXJS observable * sequence processing * event handling * time modelling * combining/transforming streams --- ## Angular APIs that use RXJS * request module * template data binding * notifications about changes * user events streams * state and state changes handling --- ## Why RXJS is good --- ## Composability <small>because of uniformity</small> All operators and techniques are applicable to the RX observables --- ## Time is not an issue Notion of time is built in the observable abstraction --- ## Small set of rules covers all known<sup>*</sup> cases <small>* to me</small> --- ## What's the price? Learn new paradigm --- ## RXJS at the Metafactory <a target="_blank" href="https://podgorniy.net/file/463c57f5-146b-4c5e-b6ac-d1709a28ed91.png"> <img src="https://podgorniy.net/file/463c57f5-146b-4c5e-b6ac-d1709a28ed91.png" /> </a> --- ## Not all capabilities are used equally Most used observables are [Observable](https://rxjs-dev.firebaseapp.com/guide/observable) and [BehaviorSubject](https://rxjs-dev.firebaseapp.com/guide/subject#behaviorsubject). --- ## Сare about subscribtion / unsubscribtion * `async` pipe in the views * manual unsubscription * `SubSink` class to bulk subscribe/unsubscribe --- ## Angular view pipes <small>note a trick with a variable</small> <a href="https://podgorniy.net/file/4471efc2-eefc-4ef0-96c9-584b70fa1dcc.png" target="_blank"> <img src="https://podgorniy.net/file/4471efc2-eefc-4ef0-96c9-584b70fa1dcc.png" /> </a> --- ## SubSink ``` subs = new SubSink(); //... this.subs.add( SOME_OBSERVABLE.subscribe((data) => { // DO SOMETHING WITH THE DATA }) ) //... ngOnDestroy(): void { this.subs.unsubscribe(); } ``` --- ## Most used operators `combineLatest`, `from`, `debounceTime`, `distinctUntilChanged`, `map`, `startWith`, `switchMap`, `catchError`, `shareReplay`, `tap`, `merge`, `forkJoin` --- ## [`merge`](https://rxjs-dev.firebaseapp.com/api/index/function/merge) "Equal rights" source of data <img src="https://rxjs-dev.firebaseapp.com/assets/images/marble-diagrams/merge.png" /> --- ## [`combineLatest`](https://rxjs-dev.firebaseapp.com/api/index/function/combineLatest) <img src="https://rxjs-dev.firebaseapp.com/assets/images/marble-diagrams/combineLatest.png"> --- ## [`tap`](https://rxjs-dev.firebaseapp.com/api/operators/tap) Add a callback without changing a stream. * Debugging * Side effects --- ## [`switchMap`](switchMaphttps://rxjs-dev.firebaseapp.com/api) * Cancellation * Must return an Observable --- ## [`shareReplay`](https://www.learnrxjs.io/learn-rxjs/operators/multicasting/sharereplay) Execute computations only once regardless how many subscribers --- ## [`forkJoin`](https://rxjs-dev.firebaseapp.com/api/index/function/forkJoin) Parallel http requests <img src="https://rxjs-dev.firebaseapp.com/assets/images/marble-diagrams/forkJoin.png" /> --- ## More resources: * [Official guide](https://rxjs-dev.firebaseapp.com/guide/overview) * [Operators decision tree](https://rxjs-dev.firebaseapp.com/operator-decision-tree) * [More and better examples](https://www.learnrxjs.io/) * Courses used by [Marnix](https://www.udemy.com/course/rxjs-reactive-angular-course/learn/lecture/18610542#overview) and [Kim](https://ultimatecourses.com/courses/rxjs) --- ## Thank you <small>Questions and discussion</small>
← Пiдгорний онлайн