RxJS

https://pragprog.com/titles/rkrxjs/build-reactive-websites-with-rxjs/

Glossary

  • Observable: an asynchronous collection of events
  • Observer: an object with a next, error, and complete method. Passed to the callback function of new Observable(), allowing you to pass data to the observable.

Reference

Functions

import { exampleFunction } from 'rxjs';
  • fromEvent: create a stream of specific DOM events that happen
  • fromPromise: converts a promise to an observable for easy use in observable chains (sometimes not needed as you can just pass a Promise)
  • interval: issues events every x milliseconds
  • of: creates an observable out of a known sync data source
  • toPromise: creates a promise that resolves when an obserable completes; useful to pass a Promise into a library or integrate with legacy Promise-based code

Operators

import { exampleOperator } from 'rxjs/operators';
  • delay: by milliseconds
  • filter: only take some of the events further down the stream
  • map: transform the values
  • mapTo: replace the value with a new static value (previous value doesn’t matter)
  • mergeMap: transform the values and then “merge” as in “flatten” as in “returning an array turns into multiple events”
  • repeat: waits to complete then repeats all the events the number of times specified
  • retry: upon error, retries a set number of times. Useful for Ajax
  • take: unsubscribe after a limited number of values
  • takeUntil: unsubscribe after a different stream emits a value (like an event)
  • tap: allow observing values without changing them. Also, trigger side effects
  • toArray: waits for stream to complete then emits all events as a single array