All files / app/store graph.ts

29.76% Statements 25/84
25% Branches 7/28
25% Functions 8/32
35.71% Lines 25/70

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155158x       158x   158x   158x   279x 279x 279x 279x 279x 279x 279x     279x   279x               1x       3x               1x       2x       2x       1x       1x 1x 1x 1x 1x   1x                                                                                                                                                                                      
import { assign, difference, max, min, pull, pullAll, remove } from 'lodash-es';
import { DateTime } from 'luxon';
import { makeAutoObservable, observable } from 'mobx';
import { RouterStore } from 'mobx-angular';
import { Page } from '../model/page';
import { RefNode } from '../model/ref';
import { findNode, graphable, GraphLink, GraphNode, links, linkSources, unloadedReferences } from '../util/graph';
 
export class GraphStore {
 
  selected: GraphNode[] = [];
  nodes: GraphNode[] = [];
  links: GraphLink[] = [];
  loading: string[] = [];
  timeline = true;
  arrows = false;
  showUnloaded = true;
 
  constructor(
    public route: RouterStore,
  ) {
    makeAutoObservable(this, {
      selected: observable.shallow,
      nodes: observable.shallow,
      links: observable.shallow,
    });
  }
 
  get unloaded(): string[] {
    return this.nodes.filter(n => n.unloaded).map(n => n.url);
  }
 
  get graphable(): GraphNode[] {
    return graphable(...this.nodes);
  }
 
  get unloadedNotLoading(): string[] {
    return difference(this.unloaded, this.loading);
  }
 
  get selectedPage() {
    return Page.of(this.selected.filter(s => !s.unloaded));
  }
 
  get minPublished(): DateTime {
    return min(this.graphable.map(r => r.published).filter(p => !!p)) || DateTime.now().minus({ day: 1 });
  }
 
  get maxPublished(): DateTime {
    return max(this.graphable.map(r => r.published).filter(p => !!p)) || DateTime.now();
  }
 
  get publishedDiff() {
    return this.maxPublished?.diff(this.minPublished).milliseconds || 0;
  }
 
  set(refs: RefNode[]) {
    this.loading = [];
    this.nodes = [...refs];
    this.selected = [...refs];
    Eif (this.showUnloaded) {
      this.nodes.push(...unloadedReferences(this.nodes, ...refs).map(url => ({ url, unloaded: true })));
    }
    this.links = links(this.nodes, ...this.nodes);
  }
 
  load(...refs: RefNode[]) {
    for (const ref of refs) {
      const found = findNode(this.nodes, ref.url);
      if (found) {
        assign(found, ref);
        found.unloaded = false;
      } else {
        this.nodes.push(ref);
      }
    }
    // Trigger shallow observable
    this.nodes = [...this.nodes];
    this.selected = [...this.selected];
    if (this.showUnloaded) {
      this.nodes.push(...difference(unloadedReferences(this.nodes, ...refs), this.unloaded).map(url => ({ url, unloaded: true })));
    }
    this.links.push(...links(this.nodes, ...refs));
    pullAll(this.loading, refs.map(r => r.url));
  }
 
  remove(refs: RefNode[]) {
    pullAll(this.nodes, refs);
    pullAll(this.selected, refs);
    for (const ref of refs) {
      remove(this.links, l => l.target === ref || l.source === ref);
    }
  }
 
  toggleShowUnloaded() {
    this.showUnloaded = !this.showUnloaded;
    if (this.showUnloaded) {
      if (this.showUnloaded) {
        this.nodes.push(...unloadedReferences(this.nodes, ...this.nodes).map(url => ({ url, unloaded: true })));
      }
    } else {
      remove(this.nodes, n => n.unloaded);
    }
    this.links = links(this.nodes, ...this.nodes);
  }
 
  select(...refs: GraphNode[]) {
    this.selected = [...refs];
  }
 
  selectAll() {
    this.selected = [...this.nodes];
  }
 
  clearSelection() {
    this.selected.length = 0;
  }
 
  getLoading(number: number) {
    if (this.unloadedNotLoading.length === 0) return [];
    const more = this.unloadedNotLoading.slice(0, number);
    this.loading.push(...more);
    return more;
  }
 
  startLoading(...url: string[]) {
    this.loading.push(...url);
  }
 
  notFound(url: string) {
    let ref = findNode(this.nodes, url);
    if (!ref) {
      ref = { url, notFound: true };
      this.nodes.push(ref);
    }
    ref.notFound = true;
    ref.unloaded = false;
    pull(this.loading, url);
    if (!this.showUnloaded) {
      this.links.push(...linkSources(this.nodes, url));
    }
    // Trigger shallow observable
    this.selected = [...this.selected];
    return ref;
  }
 
  grabNodeOrSelection(ref: RefNode) {
    if (!this.selected.includes(ref)) {
      this.selected = [ref];
    }
    return [...this.selected];
  }
}