Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Observable.forkJoin() building

Based on the parameters of the function I want to build the forkJoin() method.

For example:

  • if parameter1 is empty => don't put a http request for it inside the forkJoin()
  • if parameter2 is empty => don't put a http request for it inside the forkJoin()

Code:

getAllByIds(parameter1: any, parameter2: any) {

    let itemList = new Array();

    return Observable.forkJoin(
         this.http.get('rest/fillin/ids/' + parameter1) // don't put this request for parameter1 if it is empty
         .map((res: Response) => res.json()),

         this.http.get('rest/textitem/ids/' + parameter2) // don't put this request for parameter2 if it is empty
         .map((res:Response) => res.json())
    ).map(
        data => {
            itemList.push(data[0]);
            itemList.push(data[1]);
            return itemList;
         }
     );
}

So, is it possible to build up the forkJoin() like this?

like image 976
Korki Korkig Avatar asked Jan 23 '17 17:01

Korki Korkig


People also ask

What is observable forkJoin?

forkJoin is an operator that takes any number of input observables which can be passed either as an array or a dictionary of input observables. If no input observables are provided (e.g. an empty array is passed), then the resulting stream will complete immediately.

Is forkJoin deprecated?

The forkJoin observable now takes a dictionary of sources: Moreover, there is one deprecation — forkJoin(a, b, c, d) should no longer be used; Instead, pass an array such as forkJoin([a, b, c, d]) .

What will happen if error happens in forkJoin?

“If any input observable errors at some point, forkJoin will error as well and all other observables will be immediately unsubscribed.”

What is forkJoin in typescript?

forkJoin takes a number of input observables and waits for all passed observables to complete. Once they are complete, it will then emit a group of the last values from corresponding observables. The resulting stream emits only one time when all of the inner streams complete.


1 Answers

Actually this depends on what do you expect to get when you skip some HTTP requests.

Should the output from forkJoin() contain null values or just ignore it completely?

function mockHTTPRequest(id) {
  return Observable.of(id).delay(100);
}

let parameter1 = 'a';
let parameter2 = false;

let sources = [];
if (parameter1) {
  sources.push(mockHTTPRequest('rest/fillin/ids/' + parameter1));
}
if (parameter2) {
  sources.push(mockHTTPRequest('rest/textitem/ids/' + parameter2));
}

Observable.forkJoin(...sources)
  .map(data => {
    console.log(data.length);
    return data;
  })
  .subscribe(values => console.log(values));

See live demo: https://jsbin.com/qorulel/5/edit?js,console

This solution just doesn't create source Observables if parameter1 or parameter2 is false. Notice, that console.log(data.length) can be from 0 to 2 depending on parameterX values.

Or you can just create Observable.of(null) instead of the HTTP requests.

function mockHTTPRequest(id) {
  return Observable.of(id).delay(100);
}

let parameter1 = 'a';
let parameter2 = false; 

let sources = [
  parameter1 ? mockHTTPRequest('rest/fillin/ids/' + parameter1) : Observable.of(null),
  parameter2 ? mockHTTPRequest('rest/textitem/ids/' + parameter2) : Observable.of(null)
];

Observable.forkJoin(...sources)
  .map(data => {
    console.log(data.length);
    return data;
  })
  .subscribe(values => console.log(values));

See live demo: https://jsbin.com/caheno/5/edit?js,console

Now the output has always 2 values. Just some of them are null.

like image 160
martin Avatar answered Oct 11 '22 10:10

martin