Skip to content

Module 04 RxJS

Kobi Hari edited this page Feb 16, 2020 · 1 revision

Module 04 - Reactive X in JS (RxJS)

Projects:

fun-with-rxjs Introduction to Observables, Subjects, And Reactive X
reactive-colors Demonstrates asynchronous operators on real life example
tv-maze-http Introduction to the HttpClient Module

Introduction to RxJS

  • We understood the meaning of a Stream
  • We defined what an Observer<T> is and understood that it has 3 methods
    • next(T)
    • complete()
    • error(err)
  • We understood that Observable<T> is an object that allows observers to subscribe
    • subscribe(Observer<T>)
  • We saw how to define an observer explicitly by supplying the 3 methods and their implementation
  • We saw how to create an observable using the interval operator
  • We saw that 2 observers that subscribe on different times, get different sets of events that are not synchronized
  • We understoof the difference between cold observables and warm observables
  • We saw how to create a custom observable using the observable constructor
  • We got familiar with the Subject object and understood that it is basically an event...
  • We learned about BehaviorSubject and understood that it is just a subject that sends the latest event to a new observer on the moment of subscription
  • We learned how to develop stateful services using BehaviorSubject and expose it as an observable using the .asObservable() method
  • We understood that if we subscribe to an observable we must also unsubscribe from it when we are done with it to avoid memory leaks
  • We saw that we can bind directly to an observable using the async pipe in the html

Introduction to RxJS operators

  • We talked about the concept of operators in math, strings and arrays
  • We understood that RxJS operators create observables.
  • We saw 3 documentation web sites that serve as reference guide for reactive operators
  • We covered some simple operators
  • We created an example that uses observable to convert user search keyword into a list of matching results (Colors)
  • We Understood that the map operator cannot be used when we apply an asyncronous function on each event because it creates an Observable of Promises
  • We saw how to use mergeMap and SwitchMap as asynchronous alternative for map when using async projections
  • We understood the difference between mergeMap and switchMap

Introduction to Http in Angular

  • We discussed internet protocols: IP, TCP, UDP, HTTP, FTP
  • We talked about the structure of Http Request - Verb, URL, Header, Body
  • We talked about the various types of verbs:
    • GET to query a single or a list of objects
    • POST to add a new object
    • PUT to update an entire existing object
    • DELETE to delete an existing object or list of objects
    • PATCH to modify a part of an existing object (several fields only)
  • We saw how to use Postman To test web APIs
  • We used HttpClientModule to provide our services with the HttpClient service
  • We used the HttpClient Service to create web requests to an online api called TV Maze
  • We saw that the HttpClient methods all return Observables
    • Which means that no request happens until we subscribe
    • Which also means that if we subscribe 3 times, we run the request 3 times...
  • We saw that it is preferable to use the toPromise operator, to convert the observable to promise.
  • We saw that the async pipe also works with promises...
  • We saw how to use sites like QuickType to automatically generate model for json data

Next week, we will

  • Introduce the Router - a tool used to navigate between pages in an angular application
  • Understand how to divide the application into Modules and the various types of modules
  • Talk about advanced routing features: Guards, Nested Routing, and lazy loading