function tryReadFromCache()

in src/rosetta-translator.ts [239:269]


function tryReadFromCache(
  sourceSnippet: TypeScriptSnippet,
  cache: LanguageTablet,
  fingerprinter: TypeFingerprinter,
  compiledOnly: boolean,
): CacheHit {
  const fromCache = cache.tryGetSnippet(snippetKey(sourceSnippet));

  if (!fromCache) {
    return { type: 'miss' };
  }

  // infused snippets won't pass the full source check or the fingerprinter
  // but there is no reason to try to recompile it, so return cached snippet
  // if there exists one.
  if (isInfused(sourceSnippet)) {
    return { type: 'hit', snippet: fromCache, infused: true };
  }

  const dirtySource = completeSource(sourceSnippet) !== fromCache.snippet.fullSource;
  const dirtyTranslator = !Object.entries(TARGET_LANGUAGES).every(
    ([lang, translator]) => fromCache.snippet.translations?.[lang]?.version === translator.version,
  );
  const dirtyTypes = fingerprinter.fingerprintAll(fromCache.fqnsReferenced()) !== fromCache.snippet.fqnsFingerprint;
  const dirtyDidntCompile = compiledOnly && !fromCache.snippet.didCompile;

  if (dirtySource || dirtyTranslator || dirtyTypes || dirtyDidntCompile) {
    return { type: 'dirty', translation: fromCache, dirtySource, dirtyTranslator, dirtyTypes, dirtyDidntCompile };
  }
  return { type: 'hit', snippet: fromCache, infused: false };
}