This document provides an overview of ReactiveX (Rx), which is an API for asynchronous programming using observable streams. It discusses key Rx concepts like Observables, which represent sets of values over time that can be subscribed to; Operators, which allow transforming, combining, and manipulating observable streams; and Subjects, which act as both Observable and Observer and are useful for multicasting streams. The document provides examples of using Rx in autocomplete applications and discusses Schedulers, which define the execution context for asynchronous operations in Rx. Overall, the document serves as a high-level introduction to the reactive programming paradigm and Rx library.
22. Observable
● An Observable emits items or sends notifications to its observers by calling
the observers’ methods.
● An observer subscribes to an Observable.
next()
attach()
24. Observable
const observer = {
// on each new emitted value by the observable
next: value => {},
// if some error occurs inside the observable
error: error => {},
// the observable has ran out of items
complete: () => {},
};
25. const observer1 = {
next: value => console.log(`[Observer 1]: Received ${value}!`),
error: error => console.log(`[Observer 1]: ${error}!`),
complete: () => console.log(`[Observer 1]: No more items! `),
};
foo.subscribe(observer1);
foo.subscribe(
value => console.log(`[Observer 2]: Received ${value}!`),
error => console.log(`[Observer 2]: ${error}!`),
);
foo.subscribe(value => console.log(`[Observer 3]: Received ${value}!`));
26. Cold
● The data is produced by the
Observable itself.
● It begins to emit items only after
the observer subscribes to it.
● Each observer (subscriber) gets
its own unique set of items.
Hot
● The data is produced outside
the Observable itself.
● It may begin emitting items as
soon as it is created.
● The emitted items are shared
between the subscribers
(multicasting).
vs
https://stackblitz.com/edit/hot-vs-cold
29. ● Multicast observable (plain observables are unicast)
○ It manages an internal list of observers
● It implements the observer interface
○ next(), error() and complete()
● Useful for
○ Multicast items to observers
○ “Convert” plain unicast observable to multicast
Subject
https://stackblitz.com/edit/learn-subjects
31. ● Composition + Declarative = The power of ReactiveX!
● It allows you to transform, combine, manipulate, and work with the
sequences of emitted items.
● .pipe() method.
● You can create your own custom operators!
Operators
42. Scheduler
● Multithreading!
○ By default, observables, its operators chain and observers run in the same thread.
● Defines the execution context
○ Where and when the task is executed.
● May vary on the implementation
43. References
● 2 minute introduction to Rx
● ReactiveX site
● Learn RxJS
● Hot vs Cold Observables
● RxJS docs