All files / app/store query.ts

83.33% Statements 40/48
72.41% Branches 21/29
60% Functions 9/15
84.21% Lines 32/38

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81  70x                     70x   25x 25x 25x 25x 25x             25x   25x         25x       67x 67x 67x 67x 67x 67x 67x 67x       18x       23x 23x 23x       23x 23x 23x   20x 20x   2x 23x 23x         23x 23x                
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { isEqual, omit } from 'lodash-es';
import { action, makeAutoObservable, observable, runInAction } from 'mobx';
import { catchError, EMPTY, Subscription } from 'rxjs';
import { Page } from '../model/page';
import { Ref, RefPageArgs } from '../model/ref';
import { RefService } from '../service/api/ref.service';
 
@Injectable({
  providedIn: 'root'
})
export class QueryStore {
 
  args?: RefPageArgs = {} as any;
  sourcesOf?: Ref = {} as any;
  responseOf?: Ref = {} as any;
  page?: Page<Ref> = {} as any;
  error?: HttpErrorResponse = {} as any;
 
  private running?: Subscription;
  private runningSources?: Subscription;
  private runningResponses?: Subscription;
 
  constructor(
    private refs: RefService,
  ) {
    makeAutoObservable(this, {
      args: observable.struct,
      page: observable.ref,
      clear: action,
    });
    this.clear(); // Initial observables may not be null for MobX
  }
 
  clear() {
    this.args = undefined;
    this.page = undefined;
    this.error = undefined;
    this.sourcesOf = undefined;
    this.responseOf = undefined;
    this.running?.unsubscribe();
    this.runningSources?.unsubscribe();
    this.runningResponses?.unsubscribe();
  }
 
  close() {
    if (this.running && !this.running.closed) this.clear()
  }
 
  setArgs(args: RefPageArgs) {
    Eif (!isEqual(omit(this.args, 'search'), omit(args, 'search'))) this.clear();
    this.args = args;
    this.refresh();
  }
 
  refresh() {
    Eif (this.args) {
      this.running?.unsubscribe();
      this.running = this.refs.page(this.args).pipe(
        catchError((err: HttpErrorResponse) => {
          runInAction(() => this.error = err);
          return EMPTY;
        }),
      ).subscribe(p => runInAction(() => this.page = p));
      this.runningSources?.unsubscribe();
      Iif (this.args.sources) {
        this.runningSources = this.refs.getCurrent(this.args.sources).pipe(
          catchError(() => EMPTY),
        ).subscribe(ref => runInAction(() => this.sourcesOf = ref));
      }
      this.runningResponses?.unsubscribe();
      Iif (this.args.responses) {
        this.runningResponses = this.refs.getCurrent(this.args.responses).pipe(
          catchError(() => EMPTY),
        ).subscribe(ref => runInAction(() => this.responseOf = ref));
      }
    }
  }
}