<commit_before>/* eslint-disable prefer-promise-reject-errors */

// This file does not contain anything React-specific. If need JS client for
// another framework this file could be extracted into its own package and be
// re-used.

import unfetch from "isomorphic-unfetch";

import { applyUriDefaults, parseUri, stringifyUri } from "./uri";

const DOCUMENT_DOMAIN_REGEX = /\bdocument\.domain\s*=\s*(["'])([^'"\s]+)\1/;

/*
This class fetches and caches nodes, provides global options, and keeps
`window.DJEDI_NODES` up-to-date. See the docs for more information.

The admin sidebar expects the following mapping of all rendered nodes on the page:

    window.DJEDI_NODES = {
      "<uri>": "<default>",
    }
*/
export class Djedi {
  constructor() {
    this.options = makeDefaultOptions();

    this._nodes = new Map();
    this._prefetchableNodes = new Map();
    this._renderedNodes = new Map();
    this._lastPrefetch = {};
    this._batch = makeEmptyBatch();
    this._DJEDI_NODES = {};

    if (typeof window !== "undefined") {
      if (window.DJEDI_NODES == null) {
        window.DJEDI_NODES = {};
      }
      this._DJEDI_NODES = window.DJEDI_NODES;
    }
  }

  resetOptions() {
    this.options = makeDefaultOptions();
  }

  resetNodes() {
    if (this._batch.timeoutId != null) {
      clearTimeout(this._batch.timeoutId);
    }

    this._nodes = new Map();
    this._prefetchableNodes = new Map();
    this._renderedNodes = new Map();
    this._lastPrefetch = {};
    this._batch = makeEmptyBatch();
    this._DJEDI_NODES = {};

    if (typeof window !== "undefined") {
      window.DJEDI_NODES = this._DJEDI_NODES;
    }
  }

  get(passedNode, callback) {
    const node = this._normalizeNode(passedNode);
    const { uri } = node;
    const existingNode = this._nodes.get(uri);

    if (existingNode != null) {
      this._callback(callback, existingNode);
      return;
    }

    this._fetchMany({ [node.uri]: node.value }).then(
      () => {
        this._callback(callback, this._nodes.get(uri) || missingUriError(uri));
      },
      error => {
        this._callback(callback, error);
      }<commit_msg>Add comments for the state of the Djedi class<commit_after>/* eslint-disable prefer-promise-reject-errors */

// This file does not contain anything React-specific. If need JS client for
// another framework this file could be extracted into its own package and be
// re-used.

import unfetch from "isomorphic-unfetch";

import { applyUriDefaults, parseUri, stringifyUri } from "./uri";

const DOCUMENT_DOMAIN_REGEX = /\bdocument\.domain\s*=\s*(["'])([^'"\s]+)\1/;

/*
This class fetches and caches nodes, provides global options, and keeps
`window.DJEDI_NODES` up-to-date.
*/
export class Djedi {
  constructor() {
    this.options = makeDefaultOptions();

    // `Map<uri: string, Node>`. Cache of all fetched nodes.
    this._nodes = new Map();

    // `Map<uri: string, Node>`. Everything that `reportPrefetchableNode` has
    // reported. The nodes contain default values (if any).
    this._prefetchableNodes = new Map();

    // `{ [uri: string]: string }`. The return value of the last `prefetch` call.
    // Mutated by `get` and `getBatched`. The values come from fetched nodes.
    this._lastPrefetch = {};

    // Queue for `getBatched`.
    this._batch = makeEmptyBatch();

    // `Map<uri: string, Node>`. Everything that `reportRenderedNode` has
    // reported. The nodes contain default values (if any). Used to keep
    // `_DJEDI_NODES` up-to-date.
    this._renderedNodes = new Map();

    // `{ [uri: string]: string }`. The values are default values (if any). //
    // The admin sidebar expects the following mapping of all rendered nodes on
    // the page: `window.DJEDI_NODES = { "<uri>": "<default>" }`.
    this._DJEDI_NODES = {};

    if (typeof window !== "undefined") {
      if (window.DJEDI_NODES == null) {
        window.DJEDI_NODES = {};
      }
      this._DJEDI_NODES = window.DJEDI_NODES;
    }
  }

  resetOptions() {
    this.options = makeDefaultOptions();
  }

  resetNodes() {
    if (this._batch.timeoutId != null) {
      clearTimeout(this._batch.timeoutId);
    }

    this._nodes = new Map();
    this._prefetchableNodes = new Map();
    this._lastPrefetch = {};
    this._batch = makeEmptyBatch();
    this._renderedNodes = new Map();
    this._DJEDI_NODES = {};

    if (typeof window !== "undefined") {
      window.DJEDI_NODES = this._DJEDI_NODES;
    }
  }

  get(passedNode, callback) {
    const node = this._normalizeNode(passedNode);
    const { uri } = node;
    const existingNode = this._nodes.get(uri);

    if (existingNode != null) {
      this._callback(callback, existingNode);
      return;
    }

    this._fetchMany({ [node.uri]: node.value }).then(
      () => {
        this._callback(callback, this._nodes.get(uri) || missingUriError(uri));
      },
      error => {
        this._callback(callback, error);
      }
{'fraction_non_alphanumeric': 0.1404471544715447, 'fraction_numerical': 0.0004065040650406504, 'mean_word_length': 2.9086576648133438, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 14, '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': '84353', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1539, 'n_words': 402}