Deep Dive Into RxJS
Deep Dive Into RxJS
into RxJS
David Benson
Who Am I?
David Benson
Reactive Extensions is Cross Platform (available for Java, C#, C++, JavaScript,
Python, and more)
http://reactivex.io/
The Observer Pattern
The Observer Pattern is a software design pattern in which an object, called the subject,
maintains a list of its dependents, called observers, and notifies them automatically of any
state changes, usually by calling one of their methods.
The Iterator Pattern
The Iterator Pattern provides a way to access the elements of an object container
sequentially without exposing the underlying representation.
Functional Programming
Functional programming is a declarative style of building software by using pure functions,
avoiding shared state, mutable data, and side-effects.
What is ReactiveX?
Observable objects
ReplaySubject
A subject that stores the latest x values, and immediately sends those values to new subscribers.
AsyncSubject
A subject that waits until it is complete, then sends the final value to any subscribers (even latecomers).
ConnectableObservable
A wrapper that makes an observable behave like a subject, but with a .connect() function to start it.
Multicasting
To convert an Observable to a Subject, you can use the multicast operator:
let a = new Observable(observer => { 1
console.log(1); 2 first result
observer.next(2);
}).pipe(
2 second result
multicast(() => new Subject())
);
a.subscribe(result => console.log(result));
a.subscribe(result => console.log(result));
a.connect(); // fires the initial observable
Multicast creates a new ConnectableObservable, then subscribes to the original observable, passing the
events through.
zip
Begins once all observables have fired once
withLatestFrom (piped)
Begins when the target has been fired after the
Fires 1:1 when all observables have sent the next
source has been given a value.
event
Fires whenever any observable sends an event
https://rxviz.com/v/Ro3V5M98
Beverly Mitchell’s careers could
use combineLatest()
Joining Observables Into A Single Stream
merge race
Passes through values from multiple observables Watches all of the given observables until one
into a single observable emits a value, then passes through that
Passes through all values from any observable observable and discards the others.
concat (piped)
Calls a series of observables in order, waiting for
startWith (piped)
give a starting value to an observable
one to finish before calling the second
https://rxviz.com/v/qJwKvGpJ
Mapping Observables To New Observables
Mapping Observables To New Observables
mergeMap (piped) concatMap (piped)
Subscribe to every new observable that we Queues up observables, and then subscribes to
return from the map. a new observable when the previous observable
completes.
https://rxviz.com/v/dJB607gJ
Operator
A Custom adapts itself to
˅
expediency.
- Tacitus
Like the observable itself, pipe operations are not executed until the observable is
subscribed.
Custom Pipe operations
wrapper => function => new observable
const splitString = (splitOn = ',') => Create a wrapper
(source) => That returns a Function
new Observable(observer => { Which returns a new Observable
return source.subscribe({ Based on the input Observable
next(x) { observer.next(x.split(splitOn); },
error(err) { observer.error(err); }, Make sure to pass errors
complete() { observer.complete(); } And to clean up
});
});
getUsers('David').pipe(
thenCombineLatest(user => getKids(user)),
thenCombineLatest(([user,kids]) => getHumorStyle(kids.length))
).subscribe(([user,kids,humorStyle]) => {})
https://rxviz.com/v/qJyAE4EJ
Using RxJs with Frameworks
Framework Support
Angular: use the async pipe:
<div *ngIf="product | async; let product">
<h1>{{product.name}}</h1>
<p>{{product.description}}</p>
</div>
React: Use Subjects instead of Actions and Stores, while keeping a unidirectional
data flow. http://www.thepursuit.io/2018/02/why-to-use-rx-with-react/
● Just subscribe once (use pipes to modify subscriptions and handle nested
subscriptions)
http://rxmarbles.com
https://medium.com/@benlesh