All files / app/store plugin.ts

87.09% Statements 27/31
63.15% Branches 12/19
77.77% Functions 7/9
95.45% Lines 21/22

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  10x                       10x   3x 3x 3x         3x   3x       3x       5x 5x 5x 5x       1x       1x 1x 1x       1x 1x 1x   1x 1x            
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { isEqual, omit } from 'lodash-es';
import { makeAutoObservable, observable, runInAction } from 'mobx';
import { catchError, EMPTY, Subscription } from 'rxjs';
import { Page } from '../model/page';
import { Plugin } from '../model/plugin';
import { TagPageArgs } from '../model/tag';
import { PluginService } from '../service/api/plugin.service';
 
@Injectable({
  providedIn: 'root'
})
export class PluginStore {
 
  args?: TagPageArgs = {} as any;
  page?: Page<Plugin> = {} as any;
  error?: HttpErrorResponse = {} as any;
 
  private running?: Subscription;
 
  constructor(
    private plugins: PluginService,
  ) {
    makeAutoObservable(this, {
      args: observable.struct,
      page: observable.ref,
    });
    this.clear(); // Initial observables may not be null for MobX
  }
 
  clear() {
    this.args = undefined;
    this.page = undefined;
    this.error = undefined;
    this.running?.unsubscribe();
  }
 
  close() {
    Iif (this.running && !this.running.closed) this.clear();
  }
 
  setArgs(args: TagPageArgs) {
    Eif (!isEqual(omit(this.args, 'search'), omit(args, 'search'))) this.clear();
    this.args = args;
    this.refresh();
  }
 
  refresh() {
    Iif (!this.args) return;
    this.running?.unsubscribe();
    this.running = this.plugins.page(this.args || {}).pipe(
      catchError((err: HttpErrorResponse) => {
        runInAction(() => this.error = err);
        return EMPTY;
      }),
    ).subscribe(p => runInAction(() => this.page = p));
  }
 
}