All files / app/store thread.ts

42.62% Statements 26/61
22.22% Branches 6/27
22.22% Functions 4/18
46.8% Lines 22/47

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  67x           67x         67x   4x 4x 4x 4x 4x 4x         4x   4x                     4x       8x 8x       8x 8x 8x       2x 2x           2x                                       2x       2x                                                                        
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { action, makeAutoObservable, observable, runInAction } from 'mobx';
import { catchError, EMPTY, Subscription } from 'rxjs';
import { Page } from '../model/page';
import { Ref, RefPageArgs, RefSort } from '../model/ref';
import { RefService } from '../service/api/ref.service';
import { getArgs, UrlFilter } from '../util/query';
 
@Injectable({
  providedIn: 'root'
})
export class ThreadStore {
 
  defaultBatchSize = 500;
  args?: RefPageArgs = {} as any;
  pages: Page<Ref>[] = [];
  error?: HttpErrorResponse = {} as any;
  cache = new Map<string | undefined, Ref[]>();
  latest: Ref[] = [];
 
  private loading?: Subscription;
 
  constructor(
    private refs: RefService,
  ) {
    makeAutoObservable(this, {
      args: observable.struct,
      cache: observable.ref,
      pages: observable.ref,
      latest: observable.ref,
      clear: action,
      setArgs: action,
      add: action,
      addPage: action,
      loadMore: action,
    });
    this.clear(); // Initial observables may not be null for MobX
  }
 
  clear() {
    this.error = undefined;
    this.args = {
      size: this.defaultBatchSize,
      page: 0,
    };
    this.pages = [];
    this.cache.clear();
    this.loading?.unsubscribe();
  }
 
  setArgs(top?: string, sort?: RefSort | RefSort[], filters?: UrlFilter[], search?: string) {
    this.clear();
    this.args = {
      ...getArgs('plugin/comment', sort, filters, search),
      responses: top,
      size: this.defaultBatchSize,
      page: 0,
    };
    this.loadMore();
  }
 
  add(ref: Ref) {
    if (this.cache.has(ref.sources?.[0])) {
      const arr = this.cache.get(ref.sources?.[0])!;
      if (!arr.find(x => x.url === ref.url)) arr.push(ref);
    } else {
      this.cache.set(ref.sources?.[0], [ref]);
    }
  }
 
  addPage(page: Page<Ref>) {
    if (!page.content.length) return;
    this.pages.push(page);
    for (const r of page.content) this.add(r);
    this.latest = page.content;
  }
 
  loadMore() {
    this.args = {
      ...this.args,
      page: this.pages.length,
    };
    this.loading = this.refs.page(this.args).pipe(
      catchError((err: HttpErrorResponse) => {
        runInAction(() => this.error = err);
        return EMPTY;
      }),
    ).subscribe(page => runInAction(() => this.addPage(page)));
  }
 
  loadAdHoc(source?: string) {
    const args = {
      ...this.args,
      responses: source,
    };
    const existing = this.cache.get(source)?.length;
    if (existing) {
      args.size = 20;
      args.page = Math.floor(existing / 20);
    }
    this.loading = this.refs.page(args).pipe(
      catchError((err: HttpErrorResponse) => {
        runInAction(() => this.error = err);
        return EMPTY;
      }),
    ).subscribe(page => runInAction(() => {
      if (source) {
        for (const ref of page.content) this.add(ref);
        runInAction(() => this.latest = page.content);
      }
    }));
  }
 
  get hasMore() {
    if (!this.pages.length) return false;
    return this.pages.length < this.pages[0].page.totalPages;
  }
}