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

Stay Reactive Stay Quiet with RxJS

Stay Reactive Stay Quiet with RxJS

Negli ultimi tempi si sente spesso parlare di Reactive Programming. Framework come Angular, l’hanno messo alla base del loro core, ma molti dei suoi utilizzatori vengono sopraffatti da essa. Facciamo allora un viaggio insieme alla scoperta della più famosa libreria di Reactive Programming per Javascript, RxJS. In questo talk navigheremo insieme su un mare mosso da Observers e Subscriptions, ci faremo spingere da qualche Operators e con l’aiuto di alcuni Subjects arriveremo a padroneggiare il famoso oceano degli Observables.

Luca Del Puppo

October 08, 2021
Tweet

More Decks by Luca Del Puppo

Other Decks in Programming

Transcript

  1. Agenda 1. Why this talk? 2. Observables 3. Observers 4.

    Subscriptions 5. Operators 6. Marble Diagram 7. Subjects Stay Reactive Stay Quiet with RxJS Luca Del Puppo
  2. Ego Slide Luca Del Puppo (aka Puppo) Full-Stack Developer in

    Flowing @puppo92 https://www.linkedin.com/in/lucadelpuppo/ [email protected]
  3. WE ❤ REMOTE WORKING Milan, Rome, Turin, Treviso, Bologna, Ancona,

    Catania and wherever you want! We are hiring ⮕ recruitment@flowing.it
  4. Core Observables concerns ➔ Creating Observables ➔ Subscribing to Observables

    ➔ Executing the Observable (next, error, complete) ➔ Disposing Observables Stay Reactive Stay Quiet with RxJS Luca Del Puppo
  5. What is an Observer? An Observer is a consumer of

    values delivered by an Observable. Observers are simply a set of callbacks, one for each type of notification delivered by the Observable: next, error, and complete. Stay Reactive Stay Quiet with RxJS Luca Del Puppo const observer: Observer<string> = { next: (value: any) => console.log('next', value), error: (error: any) => console.error('error', error), complete: () => console.log('complete') }
  6. What is a Subscription? A Subscription is an object that

    represents a disposable resource, usually the execution of an Observable. A Subscription has one important method, unsubscribe, that takes no argument and just disposes the resource held by the subscription. Stay Reactive Stay Quiet with RxJS Luca Del Puppo const subscription = observable.subscribe(observer); subscription.add(observable2.subscribe(observer)); subscription.unsubscribe();
  7. Operators Creation Creation operators are functions that can be used

    to create an Observable with some common predefined behavior or by joining other Observables Pipeable Pipeable Operator is a function that takes an Observable as its input and returns another Observable. It is a pure operation: the previous Observable stays unmodified. Syntax: observableInstance.pipe(operator(), operator2() ….) Stay Reactive Stay Quiet with RxJS Luca Del Puppo
  8. Creation Operator Creation Operator ➔ of ➔ from ➔ fromEvent

    ➔ interval ➔ timer ➔ EMPTY Join Creation Operator ➔ combineLatest ➔ forkJoin ➔ concat ➔ merge ➔ race ➔ zip Stay Reactive Stay Quiet with RxJS Luca Del Puppo
  9. Pipeable Operator ➔ Filtering Operator debounceTime, throttleTime, distinct, filter, first,

    last, skip, skipLast, skipUntil, SkipWhile, take, takeLast, takeUntil, takeWhile ➔ Transformation Operator concatMap, exhaustMap, map, mergeMap, pairwise, scan, switchMap ➔ Utility Operator delay, tap, timeout, timestamp, toArray ➔ Error Operator catchError, retry, retryWhen ➔ Join Operator combineLatestAll, concatAll, exhaustAll, mergeAll, startWith, switchAll, withLatestFrom ➔ Multicasting Operator share ➔ Conditional and Boolean Operators defaultIfEmpty, every, find, findIndex, isEmpty ➔ Mathematical and Aggregate Operators count, max, min, reduce Stay Reactive Stay Quiet with RxJS Luca Del Puppo
  10. Marble Diagrams are visual representations of how operators work, and

    include the input Observable(s), the operator and its parameters, and the output Observable. Marble Diagrams
  11. A Subject is like an Observable, but can multicast to

    many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners. What is a Subject? Characteristics • Every Subject is an Observable • Every Subject is an Observer.
  12. Which are they? ➔ Subject ➔ BehaviorSubject ➔ ReplaySubject ➔

    AsyncSubject Stay Reactive Stay Quiet with RxJS Luca Del Puppo