Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Rx.Observable.prototype.let operator?

Tags:

rxjs

rxjs5

The example and explanation of the let operator (https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/let.md) is not clear. Anyone has a good example/explanation how the let operator works, and when we should use it?

like image 522
Tuong Le Avatar asked Jul 12 '16 23:07

Tuong Le


People also ask

How do you call one Observable after another?

Make a single observable out of the several ones that need to be executed in parallel (i.e. the many deletions), using forkJoin. Use switchMap to execute one observable after another.

What is the name of the method you call on an Observable to apply operators?

Chaining Operators Most operators operate on an Observable and return an Observable. This allows you to apply these operators one after the other, in a chain. Each operator in the chain modifies the Observable that results from the operation of the previous operator.

Which one of the following Observable operators will you use to transform the values of an Observable by applying a function to each value?

RxJS map() operator is a transformation operator used to transform the items emitted by an Observable by applying a function to each item. It applies a given project function to each value emitted by the source Observable and then emits the resulting values as an Observable.

How do observables work internally?

Observables are data source wrappers and then the observer executes some instructions when there is a new value or a change in data values. The Observable is connected to the observer who does the execution through subscription, with a subscribe method the observer connects to the observable to execute a code block.


1 Answers

&tldr;

It is a convenience function for being able to compartmentalize logic and inject it into a pipeline.

Longer Explanation

The source is probably the most definitive explanation. It is really just passing a function which gets called with a source Observable.

Rx.Observable.prototype.let = function(fn) {
  return fn(this);
}

The utility of this is that we can create or pre-define a pipeline that you want to reuse for multiple sources. Consider a common trope for Rx, the reactive search bar:

// Listen to a key up event on the search bar 
// and emit the value of the search
Rx.Observable.fromEvent(searchBar, 'keyup', e => e.target.value)
  // Don't search too eagerly
  .filter(text => text.length > 3)
  .debounceTime(500)
  //Search logic
  .flatMap(text => $.getJSON(`my/search/api?q=${text}`))
  .flatMap({results} => results)
  //Handler
  .subscribe(appendToList);

The above should give a basic sense of the structure of how a pipeline might be created. If we wanted to try and abstract some of this logic either to clean up the code or to be able to use it elsewhere it can be a little tricky, because it usually means creating a new operator (and that has its own headaches).

The solution is a relatively simple approach of pulling common logic into a function that can be passed a source Observable and will return a new Observable with that logic applied.

So the above might become:

//Defined in pipelines.js
function filterBuilder(minText, debounceTime) {
  return (source) => 
    source.filter(text => text.length > minText)
          .debounce(debounceTime);
}

function queryBuilder(baseUrl) {
  return (source) => 
    source.flatMap(text => $.getJSON(`${baseUrl}?q=${text}`))
          .flatMap({results} => results);
}


//In your application code

Rx.Observable.fromEvent(searchBar, 'keyup', e => e.target.value)
  .let(filterBuilder(3, 500))
  .let(queryBuilder('my/search/api'))
  .subscribe(appendResults);
like image 118
paulpdaniels Avatar answered Oct 13 '22 03:10

paulpdaniels