All files / app/store query.ts

82.89% Statements 63/76
79.31% Branches 23/29
66.66% Functions 16/24
84.37% Lines 54/64

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133  88x             88x                     88x   38x 38x 38x 38x 38x               38x   38x         38x       88x 88x 88x 88x 88x 88x 88x 88x 88x       19x 19x       30x 30x 30x 30x       2x 2x               1x 1x 1x 1x 1x   1x       1x 1x         1x         30x 30x 30x   20x 20x   8x 30x 30x 1x   1x   30x 30x                 30x 30x 30x 1x       1x      
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { isEqual, omit } from 'lodash-es';
import { action, makeAutoObservable, observableRef, observableStruct, runInAction } from 'mobx';
import { catchError, EMPTY, Observable, Subscription } from 'rxjs';
import { Page } from '../model/page';
import { Ref, RefPageArgs } from '../model/ref';
import { RefService } from '../service/api/ref.service';
import { withStableDateSort } from '../util/query';
 
interface PendingCursor {
  args: RefPageArgs;
  target: number;
  request: Observable<Page<Ref>>;
}
 
@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;
  private pendingCursor?: PendingCursor;
 
  constructor(
    private refs: RefService,
  ) {
    makeAutoObservable(this, {
      args: observableStruct,
      page: observableRef,
      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();
    this.pendingCursor = undefined;
  }
 
  close() {
    this.pendingCursor = undefined;
    if (this.running && !this.running.closed) this.clear()
  }
 
  setArgs(args: RefPageArgs) {
    const cursorRequest = this.takeCursor(args);
    if (!isEqual(omit(this.args, 'search'), omit(args, 'search'))) this.clear();
    this.args = args;
    this.refresh(cursorRequest);
  }
 
  queueCursorPage(target: number, request: Observable<Page<Ref>>) {
    Iif (!this.args) return;
    this.pendingCursor = {
      args: { ...this.args },
      target,
      request,
    };
  }
 
  setRelatedArgs(args: RefPageArgs) {
    this.pendingCursor = undefined;
    this.args = args;
    this.runningSources?.unsubscribe();
    if (args.sources) {
      this.runningSources = this.refs.getCurrent(args.sources).pipe(
        catchError(() => EMPTY),
      ).subscribe(ref => runInAction(() => this.sourcesOf = ref));
    } else E{
      this.sourcesOf = undefined;
    }
    this.runningResponses?.unsubscribe();
    Iif (args.responses) {
      this.runningResponses = this.refs.getCurrent(args.responses).pipe(
        catchError(() => EMPTY),
      ).subscribe(ref => runInAction(() => this.responseOf = ref));
    } else {
      this.responseOf = undefined;
    }
  }
 
  refresh(pageRequest?: Observable<Page<Ref>>) {
    Eif (this.args) {
      this.running?.unsubscribe();
      this.running = (pageRequest ?? this.refs.page(withStableDateSort(this.args))).pipe(
        catchError((err: HttpErrorResponse) => {
          runInAction(() => this.error = err);
          return EMPTY;
        }),
      ).subscribe(p => runInAction(() => this.page = p));
      this.runningSources?.unsubscribe();
      if (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));
      }
    }
  }
 
  private takeCursor(args: RefPageArgs): Observable<Page<Ref>> | undefined {
    const pending = this.pendingCursor;
    this.pendingCursor = undefined;
    if (pending?.target !== Number(args.page)) return undefined;
    Iif (!isEqual(
      omit(pending.args, 'page', 'obsolete'),
      omit(args, 'page', 'obsolete'),
    )) return undefined;
    return pending.request;
  }
}