<commit_before>/**
 * Created by sam on 12.02.2015.
 */
/**
 * Created by <NAME> on 22.10.2014.
 */
import {IEvent} from '../event';
import GraphBase, {IGraphFactory, IGraphDataDescription} from './GraphBase';
import {GraphEdge, GraphNode, IGraph} from './graph';
import {resolveImmediately} from '../internal/promise';

export default class LocalStorageGraph extends GraphBase implements IGraph {

  private updateHandler = (event: IEvent) => {
    const s = event.target;
    if (s instanceof GraphNode) {
      this.updateNode(<GraphNode>s);
    }
    if (s instanceof GraphEdge) {
      this.updateEdge(<GraphEdge>s);
    }
  }

  constructor(desc: IGraphDataDescription, nodes: GraphNode[] = [], edges: GraphEdge[] = [], private storage: Storage = sessionStorage) {
    super(desc, nodes, edges);

    if (nodes.length > 0 || edges.length > 0) {
      const uid = this.uid;
      this.storage.setItem(`${uid}.nodes`, JSON.stringify(nodes.map((d) => d.id)));
      nodes.forEach((n) => {
        this.storage.setItem(uid + '.node.' + n.id, JSON.stringify(n.persist()));
        n.on('setAttr', this.updateHandler);
      });

      this.storage.setItem(`${uid}.edges`, JSON.stringify(edges.map((d) => d.id)));
      edges.forEach((e) => {
        this.storage.setItem(`${uid}.edge.${e.id}`, JSON.stringify(e.persist()));
        e.on('setAttr', this.updateHandler);
      });
    }
  }

  static migrate(graph: GraphBase, storage = sessionStorage) {
    return resolveImmediately(graph.migrate()).then(({nodes, edges}) => {
      return new LocalStorageGraph(graph.desc, nodes, edges, storage);
    });
  }

  migrate() {
    this.nodes.forEach((n) => n.off('setAttr', this.updateHandler));
    this.edges.forEach((n) => n.off('setAttr', this.updateHandler));
    return super.migrate();
  }

  static load(desc: IGraphDataDescription, factory: IGraphFactory, storage: Storage = sessionStorage, reset = false) {
    const r = new LocalStorageGraph(desc, [], [], storage);
    if (!reset) {
      r.load(factory);
    }
    return r;
  }

  static clone(graph: GraphBase, factory: IGraphFactory, storage: Storage = sessionStorage) {
    const r = new LocalStorageGraph(graph.desc, [], [], storage);
    r.restoreDump(graph.persist(), factory);
    return r;<commit_msg>default setting edges and nodes local storage key<commit_after>/**
 * Created by sam on 12.02.2015.
 */
/**
 * Created by <NAME> on 22.10.2014.
 */
import {IEvent} from '../event';
import GraphBase, {IGraphFactory, IGraphDataDescription} from './GraphBase';
import {GraphEdge, GraphNode, IGraph} from './graph';
import {resolveImmediately} from '../internal/promise';

export default class LocalStorageGraph extends GraphBase implements IGraph {

  private updateHandler = (event: IEvent) => {
    const s = event.target;
    if (s instanceof GraphNode) {
      this.updateNode(<GraphNode>s);
    }
    if (s instanceof GraphEdge) {
      this.updateEdge(<GraphEdge>s);
    }
  }

  constructor(desc: IGraphDataDescription, nodes: GraphNode[] = [], edges: GraphEdge[] = [], private storage: Storage = sessionStorage) {
    super(desc, nodes, edges);

    const uid = this.uid;
    this.storage.setItem(`${uid}.nodes`, JSON.stringify(nodes.map((d) => d.id)));
    nodes.forEach((n) => {
      this.storage.setItem(uid + '.node.' + n.id, JSON.stringify(n.persist()));
      n.on('setAttr', this.updateHandler);
    });

    this.storage.setItem(`${uid}.edges`, JSON.stringify(edges.map((d) => d.id)));
    edges.forEach((e) => {
      this.storage.setItem(`${uid}.edge.${e.id}`, JSON.stringify(e.persist()));
      e.on('setAttr', this.updateHandler);
    });
  }

  static migrate(graph: GraphBase, storage = sessionStorage) {
    return resolveImmediately(graph.migrate()).then(({nodes, edges}) => {
      return new LocalStorageGraph(graph.desc, nodes, edges, storage);
    });
  }

  migrate() {
    this.nodes.forEach((n) => n.off('setAttr', this.updateHandler));
    this.edges.forEach((n) => n.off('setAttr', this.updateHandler));
    return super.migrate();
  }

  static load(desc: IGraphDataDescription, factory: IGraphFactory, storage: Storage = sessionStorage, reset = false) {
    const r = new LocalStorageGraph(desc, [], [], storage);
    if (!reset) {
      r.load(factory);
    }
    return r;
  }

  static clone(graph: GraphBase, factory: IGraphFactory, storage: Storage = sessionStorage) {
    const r = new LocalStorageGraph(graph.desc, [], [], storage);
    r.restoreDump(graph.persist(), factory);
    return r;
{'fraction_non_alphanumeric': 0.1705150976909414, 'fraction_numerical': 0.007548845470692718, 'mean_word_length': 3.6252566735112937, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5084709', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1382, 'n_words': 311}