Angular RxJS Flashcards
RxJS is Reactive Extension for Javascript is a library …
for composing asynchronous and event-based programs by using observable sequences
RxJs is so useful that angular implemented it in its features
Routing,
Reactive Forms,
HttpClient
Observer is an object that monitors a stream. Observer response to notifications:
- next() - handle next item emitted in the steam
- error() - handle errors
- complete() - perform final processing or cleanup when stream is complete
Observer is
a collection of callbacks that knows how to listen to values delivered by the observable
An observer is a
javascript object that defines the handlers for the notificatoins you receive
In RxJS an Observer is
also defined as an interface with next, error and complete methods
Var vs let vs const
var declarations are globally scoped or function scoped while let and const are block scoped.
var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared.
They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.
While var and let can be declared without being initialized, const must be initialized during declaration.
Observables are lazy and they
don’t execute when defined. Only when subscribed
Observer is
set of callbacks to observe handling next, error and complete
Observable is (or stream)
sequence of emitted items
In how main ways an observer can be stopped
- Call complete method
- Using completing operator (of, for, take)
- Throw an error, does not call complete
- Unsubscribe, does not call complete
Unsubscribing from observable that does not complete on it’s own helps us with
avoiding memory leaks in the application
In angular we often work with observables that angular create for us
Creating an observable (recommendet technique) is to use
a built-in creating function
Funciton of(‘Apple1’, ‘Apple2’) is
is creating an observable using a set of items emitting each value and then completing the stream
from([‘ads’, ‘we’]) does
creates an observable from an array or other data structure, emitting each individual value from that structure and then completing the stream
of and from are static functions
of vs from const apples = ['Apple1', 'Apple2'];
of(apples) // [Apple1, Apple2]
of(…apples) // Apple1, Apple2
from(apples) // Apple1, Apple2
few ways for Creating an observable
- fromEvent - creates an observable from any document, object model or or DOM event
- inverval(1000) - emits sequential number at a defined interval
Observable is
any stream of data
Observer is
Observers the stream
Methods to process notifications from the stream are next, error, complete
Subscriber is
an observer that can unsebscribe
Subscription is
represents the execution of an observable.
subscribe() returns a subscription and we then can ubsubscribe
Creating an observable can be done through
- Constructor - bad technique
- Creating functions - of, from ,from event, interval ….
- Create an observable form anything
- Returned from an angular feature
- Forms - valuechanges
- Routing: paramMap
- Http: get
- …….
To avoid memory leaks we should
always stop our observables. Call complete() of, from ... automatically unsubscribe and execute complete(). Throwing an error also unsubscribed sub.unsubscribe()
RxJS operator is a function that
transforms and manipulates items in an observable stream.
They are applied by using the observable pipe method
async pipe
Automatically subscribes to the observable when the component is initialized.
Returns each emitted value.
When a new item is emitted, component is marked to be checked for changes.
Unsubscribes automatically when component is destroyed
What is the difference between an Observable and a function?
Observables can “return” multiple values over time, something which functions cannot.