<commit_before>/*
We cache recently loaded PDF.js docs, so that:

- several frames on the same document only have to load it once
- hiding, then re-showing the document is much faster
- canvas and svg can share the same doc
*/

/*
MAX_PAGES is the maximum number of pages to store in the cache.
I just made this value up to avoid some weird case
where maybe we fail to remove stuff from the cache
and things just grow badly (user has tons of docs open).
*/
const MAX_PAGES = 1000;

import * as LRU from "lru-cache";

import { reuseInFlight } from "async-await-utils/hof";

import {
  getDocument as pdfjs_getDocument,
  PDFPromise,
  PDFDocumentProxy
} from "pdfjs-dist/webpack";

import { raw_url } from "../frame-tree/util";

import { pdf_path } from "./util";

import { encode_path } from "smc-util/misc2";

const options = {
  max: MAX_PAGES,
  length: function(doc: PDFDocumentProxy): number {
    return doc.numPages;
  }
};

export function url_to_pdf(
  project_id: string,
  path: string,
  reload: number
): string {
  return `${raw_url(project_id, encode_path(pdf_path(path)))}?param=${reload}`;
}

const doc_cache = LRU(options);

export const getDocument: (
  url: string
) => PDFPromise<PDFDocumentProxy> = reuseInFlight(async function(url) {
  let doc: PDFDocumentProxy | undefined = doc_cache.get(url);
  if (doc === undefined) {
    doc = await pdfjs_getDocument({
      url: url,
      disableStream: true,
      disableAutoFetch: true
    });
    doc_cache.set(url, doc);
  }
  return doc;
});

/*<commit_msg>webapp/latex: call LRU cache with new<commit_after>/*
We cache recently loaded PDF.js docs, so that:

- several frames on the same document only have to load it once
- hiding, then re-showing the document is much faster
- canvas and svg can share the same doc
*/

/*
MAX_PAGES is the maximum number of pages to store in the cache.
I just made this value up to avoid some weird case
where maybe we fail to remove stuff from the cache
and things just grow badly (user has tons of docs open).
*/
const MAX_PAGES = 1000;

import * as LRU from "lru-cache";

import { reuseInFlight } from "async-await-utils/hof";

import {
  getDocument as pdfjs_getDocument,
  PDFPromise,
  PDFDocumentProxy
} from "pdfjs-dist/webpack";

import { raw_url } from "../frame-tree/util";

import { pdf_path } from "./util";

import { encode_path } from "smc-util/misc2";

const options = {
  max: MAX_PAGES,
  length: function(doc: PDFDocumentProxy): number {
    return doc.numPages;
  }
};

export function url_to_pdf(
  project_id: string,
  path: string,
  reload: number
): string {
  return `${raw_url(project_id, encode_path(pdf_path(path)))}?param=${reload}`;
}

const doc_cache = new LRU(options);

export const getDocument: (
  url: string
) => PDFPromise<PDFDocumentProxy> = reuseInFlight(async function(url) {
  let doc: PDFDocumentProxy | undefined = doc_cache.get(url);
  if (doc === undefined) {
    doc = await pdfjs_getDocument({
      url: url,
      disableStream: true,
      disableAutoFetch: true
    });
    doc_cache.set(url, doc);
  }
  return doc;
});

/*
{'fraction_non_alphanumeric': 0.10894941634241245, 'fraction_numerical': 0.00324254215304799, 'mean_word_length': 3.835423197492163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6270483', 'n_tokens_mistral': 1018, 'n_tokens_neox': 1008, 'n_words': 342}