Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HTTP calls in pure pipe in angular 5

I am creating a pipe to convert one currency value to another. I am making HTTP call to convert value.

@Pipe({
    name: "currencyConverter"
})
export class CurrencyConverterPipe implements PipeTransform {

    transform(value: number, currencyPair: string): number {
        let sourceCurrency = currencyPair.split(":")[0];
        let destinationCurrency = currencyPair.split(":")[1];
        this.currencyConverterService.convert(sourceCurrency, destinationCurrency).subscribe(successData => {
            return successData['fiatValue'] * value;
        }, errorData => {
            console.error(errorData);
        })
    }
}

In HTML

<p> {{ value | currencyConverter:'BTC:USD'}}</p>

I am unable to see any value in UI. When i make my pipe impure, Then it works but it is making many HTTP calls to server. Is there any way to make HTTP call without using pure: false in pipe

like image 797
Ajit Soman Avatar asked Oct 28 '25 14:10

Ajit Soman


1 Answers

This pipe isn't pure by design.

In order to not cause multiple requests, it should return same observable that won't do additional requests until input is changed, something like:

@Pipe({
  name: "currencyConverter",
  pure: false
})
export class CurrencyConverterPipe  {
  private sourceCurrency;
  private destinationCurrency;
  private valueSubject = new Subject();
  private value$ = this.valueSubject.asObservable().distinctUntilChanged()
  .switchMap(value => {
     return this.currencyConverter
     .convert(sourceCurrency, destinationCurrency))
     .map((data) => data * value);
  });

  constructor(private currencyConverter: ...) {}

  transform(value: number, currencyPair: string): number {
    this.sourceCurrency = currencyPair.split(":")[0];
    this.destinationCurrency = currencyPair.split(":")[1];
    this.valueSubject.next(value);
    return this.value$;
  }
}

Since it is supposed to be used only in conjunction with async pipe, it makes sense to join them together and extend AsyncPipe:

@Pipe({
  name: "currencyConverterAsync",
  pure: false
})
export class CurrencyConverterPipe extends AsyncPipe {
  private sourceCurrency;
  private destinationCurrency;
  private valueSubject = new Subject();
  private value$ = this.valueSubject.asObservable().distinctUntilChanged()
  .switchMap(value => {
     return this.currencyConverter
     .convert(sourceCurrency, destinationCurrency))
     .map((data) => data * value);
  });

  constructor(cdRef: ChangeDetectorRef, private currencyConverter: ...) {
    super(cdRef);
  }

  transform(value: number, currencyPair: string): number {
    this.sourceCurrency = currencyPair.split(":")[0];
    this.destinationCurrency = currencyPair.split(":")[1];
    this.valueSubject.next(value);

    return super.transform(this.value$);
  }
}
like image 160
Estus Flask Avatar answered Oct 31 '25 05:10

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!