Observables RxJS
Last updated
Last updated
RxJS
is a library for composing asynchronous and event-based programs by using observable
sequences.
It combines the Observer pattern
with the Iterator pattern
and functional programming with collections
.
It provides:
One core type Observable
.
Satellite types Observer
, Schedulers
, Subjects
.
And operators inspired by Array
methods to allow handling asynchronous events as colletions.
Observable
Represents the ideia of an invokable collection or future values or events.
Observer
Is a collection of callbacks that knows how to listen to values delivered by the Observable.
Subscription
Represents the execution of an Observable, is primarily useful for cancelling the execution.
Operators
Are pure functions that enable a functional programming style of dealing with collections with operations like map
, filter
, concat
, reduce
, etc.
Subject
Is equivalent to an EventEmitter
, and the only way of multicasting a value or event to multiple Observers.
Schedulers
Are centralized dispatchers to control concurrency, allowing us to coordinate when computation happens on e.g setTimeout
or requestAnimationFrame
or others.
Normally you register event listeners.
With RxJS you create an observable instead.
Observable
They are Push collections of multiple values.
In an Observable you may call:
next()
: sends a value to observers.
error()
: sends a Javascript error or exception and finishes the Observable execution.
complete()
: does not send any value and finishes the Observable execution.
Since Observables may be infinite, when you subscribe a subscription
object is returned, and may be used to unsubscribe
for clean up purposes.
You can also return a custom unsubscribe
function.
Push and Pull are two different protocols that describe how a data Producer can communicate with a Consumer.
In Pull systems, the Consumer determines when it receives data from the Producer.
The Producer itself is unaware of when the data will be delivered to the Consumer.
Every Javascript function is a Pull System.
In Push systems, the Producer determines when to send data to the Consumer.
The Consumer is unaware of when it will receive that data.
Promises are the most common type of Push system.
Operators
Operators are functions.
Are the kind that can be piped, using the syntax observableInstance.pipe(operator)
.
With pipe you may create a chain of 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.
A Pipeable Operator is essentially a pure function which takes one Observable as input and generates another Observable as output.
Are another kind of operator, which can be called as standalone functions to create a new Observable
, for example of(1, 2, 3)
.
Subjects
It is a special type of Observable that allows values to be multicasted to many Observers.
They maintain a registry of many listeners.
Regular Observable
are unicast.
From the perpective of an Observer
, it cannot tell whether the Observable execution is coming from a unicast Obeservable or a Subject.
In an Observable
you call next()
from inside the Observable function, and in Subjects you can call it from outside. (They are both Observable
and Observer
at the same time)
They are similar to EventEmitters
, but are very useful to do cross-component event emitting.
There are some different variants os Subject
.
BehaviorSubject
It has a notion of "the current value".
It sotres the lastes value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the "current value".
ReplaySubject
Similar to BehaviorSubject
, in the way that you can send old values to new subscribers.
But instead of just the latest value, you can choose a buffer size of latests values.
You can also specify a window time in miliseconds, besides the buffer size, to determine how old the recored values can be.
AsyncSubject
Here, only the last value of the Observable is sent to its Observers, and only when the execution completes.
Void Subject
Sometimes the emitted value doesn't matter as much as the fact that a value was emitted.
Void subjects are used to emit signals, but it's value is not important.
Scheduler
It controls when a subscription starts and when notifications are delivered.
It consists of three components:
A scheduler is a data structure
, it knows how to store and queue tasks based on priority or other criteria.
A scheduler is an execution context
, it denotes where and when the task is executed.
A scheduler has a (virtual) clock
, it provides a notion of "time" but getter method now()
on the scheduler.
A scheduler lets you define in what execution context will an Observable deliver notifications to its Observer.
The observable is canceled when an error is thrown.
It does not execute when an error was thrown.