Appendix B. Choosing an operator

The following is a list of operators and a description of when to use them. We cover the operators (static and instance) used in this book. Some operators can act as both static and instance, and we indicate which ones. For a more complete list, please visit http://reactivex.io/rxjs/manual/overview.html#choose-an-operator.

All factory operators are called as static methods of the Rx.Observable type. See table B.1.

Table B.1. Static scope operators

Situation

Purpose

Operator

Creating a new instance With custom logic From a given value create
  From an observable-like object from
  From a range of numbers range
  With an exception catch
  From an ES6 promise From any event originated by an event emitter like WebSockets or the DOM fromPromise
  Wrapping a callback bindCallback
  Attach some recourse dependent on the lifecycle of an observable Spawn an AJAX request and merge the result into the observable sequence Conditionally create a stream with an if-then-else approach using ajax if
Creating a time-based instance Single timed interval Multiple timed intervals Run all observable sequences in parallel and collect their last elements timer interval forkJoin

All instance operators are called through an observable instance (Rx.Observable.prototype). See table B.2.

Table B.2. Instance scope operators

Situation

Purpose

Operator

Transforming a sequence Delaying or offsetting the entire emission of an event sequence delay
  Prepend an element onto the stream startWith
  Transform elements one-to-one map
  Fold entire event sequence into a single value reduce
  Just like reduce, but emit each subsequent fold as an event scan
  Extract object properties from emitted data pluck
  Buffer a certain amount of data and emit all at once bufferCount
  Buffer for a specific period of time and emit all at once bufferTime
  Buffer at the pace of when a subordinate observable emits a value buffer
  Like buffer, but use a function that creates a new observable to indicate when to close the buffer bufferWhen
Filtering sequences Remove events according to a predicate function filter
  Skip a certain number of events skip
  Grab the first N number of events take
  Emit the values of an observable until a subordinate observable emits; emit events until some other observable tells you not to. takeUntil
  Emit only the first event of a sequence first
  Emit only the last event of a sequence last
  Emit values only after a particular time span given by another observable has passed debounce
  Debounce for a fixed period of time debounceTime
  Emit event at most once every time period throttleTime
  Emit only items that are distinct by comparison (given by you) from the previous item distinctUntilChanged
  Like the previous operator, except now you compare the keys of the emitted items distinctUntilKeyChanged
Utilities Perform any sort of necessary side effect such as logging to the screen; useful for debugging purposes do
  Embellish the event object with the interval duration span; useful for computing time deltas between emission timeInterval
Error handling Catch an exception produced from any operator and replace it with a continuing observable catch
  Allow the sequence to halt and terminate into the observer’s error handlers throw
  Retry an operation for a certain amount of time retry
  Implement additional logic such as a backoff retry strategy retryWhen
  Call function when an observable completes or finishes with errors; good for cleanup tasks finally
Coordinating sequences Combine the latest values from a collection of streams when all of them have emitted; can be used as a static factory operator as well combineLatest
  Like the previous operator, except this will emit the latest values from each observable when the source observable emits withLatestFrom
Joining events of multiple observable sequences Forward events from multiple sequences in order of arrival; can be used in static form as well merge
  Append the events of one observable sequence after another (in order); can be used in static form as well concat
  Cancel a source observable midstream and replace it with a new one switch
  Merge a collection of observables using a selector function and emit when all the observable sequences have emitted at a corresponding index; useful for keeping a map of corresponding events. Can be used in static form as well zip
Projecting or branching other observables, making AJAX requests, DB lookups, and so on Merge an observable object into the source observable; flatten the result into a single observable (alias: flatMap) mergeMap
  Like mergeMap(), but dispose the source observable when it’s no longer needed (alias: flatMapLatest) switchMap
  Project and flatten a source observable but maintain order of events concatMap
Broadcasting the outcome of an observable sequence to multiple subscribers Create a lazy observable that you manage that multiple subscribers can connect to; use it to control when you want to let events flow with connect publish
  Create an observable that shares the outcome with multiple subscribers. RxJS manages the lifecycle of this observable internally through refCount. share
  Share the last N number of events with all subscribers publishReplay
  Share the last observable event with all subscribers publishLast
Cancel/dispose of the stream Unsubscribe from the stream unsubscribe()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset