Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

RXJS: Observables 101 By Maina Wycliffe www.mainawycliffe.dev

Slide 3

Slide 3 text

ReactiveX JavaScript (RXJS) ● RXJS is a JavaScript Implementation of ReactiveX Library ● ReactiveX is a library for composing async events using the observer pattern ● ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming ● The observer pattern is a software design pattern where a subject maintains a list of listeners (observers) and notifies them of any changes. ● ReactiveX is not specific to JavaScript and there are implementation for other languages such as RxJava, RxDart, RxPHP, RxGo, etc.

Slide 4

Slide 4 text

RXJS Concepts ● Observable – future values/events ● Observer – listener to observable ● Subscription – execution of observable ● Operators – pure function for transforming streams ● Subject – Allow multiple observers ● Schedulers – control concurrency, enable coordination

Slide 5

Slide 5 text

Why use Observables ● Use of Pure Functions ● Control the flow of streams/data/events ● Transform Values ● Support for Multiple Languages

Slide 6

Slide 6 text

Example Listening to a Button Click Event

Slide 7

Slide 7 text

Operators They are pure functions Allow easy composition in declarative approach Key to RXJS Kinds of Operators ● Pipeable Operators ● Creation Operators

Slide 8

Slide 8 text

Pipeable Operators A 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.

Slide 9

Slide 9 text

Examples of Pipeable Operators ● takeLast ● takeUntil ● takeWhile ● map ● mapTo ● mergeMap ● mergeMapTo ● tap ● catchError ● retry ● retryWhen ● combineLatest ● concat ● forkJoin ● merge ● race ● zip

Slide 10

Slide 10 text

Creation Operators This is a function that creates a new Observable with some common predefined behaviour or by joining other Observables.

Slide 11

Slide 11 text

Creation Operators Example 2: Interval Operator

Slide 12

Slide 12 text

Examples of Creation Operators ● ajax ● bindCallback ● bindNodeCallback ● defer ● empty ● from ● fromEvent ● fromEventPattern ● generate ● interval ● of ● range ● throwError ● timer ● iif

Slide 13

Slide 13 text

Subjects ● A Subject is like an Observable but can multicast to many Observers. ● They maintain a registry of many listeners. ● Every Subject is an Observable ● Observer can not distinguish between an Observable and a Subject ● Unlike Observables, subscribe doesn’t invoke execution, it simply registers the observer

Slide 14

Slide 14 text

Subjects ● Unlike an Observable, a subject has the following methods • next(v) – feed new value to the subject • error(e) – feed an error value to the subject • complete() – Close the subject

Slide 15

Slide 15 text

Example of a Subject

Slide 16

Slide 16 text

There are 3 variants of Subjects: • BehaviorSubject • ReplaySubject • AsyncSubject Types of Subjects

Slide 17

Slide 17 text

BehaviorSubject ● Stores the last value emitted to its consumers as the current value ● Emits the current value in store whenever a new observer subscribes ● Must be initialized by a value ● Example personal age or total vote count.

Slide 18

Slide 18 text

ReplaySubject ● Records multiple values from an observable execution ● Replays all those values to new subscribers ● The number of values recorded is specified during creating ● Records all values if none is specified

Slide 19

Slide 19 text

AsyncSubject ● Only the last value of the Observable execution is sent to the observables ● Only sent when the execution completes

Slide 20

Slide 20 text

bit.ly/2qqalg7 BehaviorSubject vs ReplaySubject

Slide 21

Slide 21 text

● http://reactivex.io/intro.html ● https://rxjs-dev.firebaseapp.com/guide/overview ● https://bit.ly/2qqalg7 ● https://www.learnrxjs.io/ Additional Resources

Slide 22

Slide 22 text

No content