All files / src/app/extensions/address-doctor/facades address-doctor.facade.ts

42.85% Statements 6/14
0% Branches 0/1
0% Functions 0/4
38.46% Lines 5/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x 1x 1x       1x     1x                                              
import { Injectable, inject } from '@angular/core';
import { isEqual } from 'lodash-es';
import { Observable, map, of, race, tap, timer } from 'rxjs';
 
import { Address } from 'ish-core/models/address/address.model';
 
import { AddressDoctorService } from '../services/address-doctor/address-doctor.service';
 
@Injectable({ providedIn: 'root' })
export class AddressDoctorFacade {
  private addressDoctorService = inject(AddressDoctorService);
 
  private lastAddressCheck: Address;
  private lastAddressCheckResult: Address[] = [];
 
  checkAddress(address: Address): Observable<Address[]> {
    Iif (isEqual(address, this.lastAddressCheck)) {
      return of(this.lastAddressCheckResult);
    }
 
    this.lastAddressCheck = address;
    return race(
      this.addressDoctorService.postAddress(address).pipe(
        tap(result => {
          this.lastAddressCheckResult = result;
        })
      ),
      // if the address check takes longer than 5 seconds return with no suggestions
      timer(5000).pipe(map(() => []))
    );
  }
}