Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change parameter in Observable (RxJs)

I use HTTP provider in Angular 2 for data loading from API.

return this.http.post(url, urlSearchParams.toString(), {
        headers: this.getHttpHeaders()
    })
    .retryWhen((error) => {
        return this.handleRetryError(error);
    })

When there is no or old session I create new one in this.handleRetryError(error) and fill headers with it. (method getHttpHeaders() returns array with headers)

RetryWhen tries to do this post again, but there is unchanged (old) headers from first round.

Is there any chance to change headers parameter for http.post from .readyWhen?

Thank you very much for help :)

like image 273
Lumix Avatar asked Sep 08 '16 12:09

Lumix


People also ask

How do you assign a value to an observable variable?

To create an observable, assign the ko. observable function to the variable. A default value can be specified in the constructor of the call. Knockout then converts your variable into a function and tracks when the value changes, in order to notify the UI elements associated with the variable.

How do I create a custom observable in RxJS?

Creating Observableslink The following example creates an Observable to emit the string 'hi' every second to a subscriber. import { Observable } from 'rxjs'; const observable = new Observable(function subscribe(subscriber) { const id = setInterval(() => { subscriber. next('hi'); }, 1000); });

What is of () RxJS?

RxJS' of() is a creational operator that allows you to create an RxJS Observable from a sequence of values. According to the official docs: of() converts the arguments to an observable sequence. In Angular, you can use the of() operator to implement many use cases.


1 Answers

You need to wrap the Observable and retry the resulting outer Observable so each time it executes again.

Observable.of(1).mergeMap(x=> {
  return this.http.get('data.json', this.configObject())
  .do(x => throw(x))
  .map(res=>res.json());
})
.retryWhen(e => e.delay(2000))
.subscribe();

for your code

return Observable.of(1).mergeMap(x => { 
    return this.http.post(url, urlSearchParams.toString(), {
      headers: this.getHttpHeaders()
    });
  })
  .retryWhen(e => this.handleRetryError(e))
like image 114
Gerard Sans Avatar answered Oct 09 '22 21:10

Gerard Sans