evaluateCached

tcmts.cli.testing.evaluateCached · #atzd5q46 · term · runs on the server

uses process

Watch expression values, reused by hash. Like a test's, a watch expression's hash covers everything it evaluates, so a value is only stale if it depends on the outside world: one whose code reaches it (the clock, randomness, the network, files, the environment, other processes) is evaluated every time, and so is one that threw (as a test's failure, it can be the runner's). fresh re-runs them all.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { reaches } from "#codebase/tcmts/cli/effects";
import type { WatchRecord } from "#codebase/tcmts/core/analyze";
import { evaluateWatches } from "#codebase/tcmts/loader";
import type { DefSource, WatchResult } from "#codebase/tcmts/loader";

export function evaluateCached(
  cb: Codebase,
  watches: WatchRecord[],
  src: DefSource,
  fresh: boolean,
): Map<number, WatchResult & { cached: boolean }> {
  const node = process.version;
  const cached = new Map<string, { ok: boolean; value: string }>();
  const outside = reaches(
    cb,
    watches.map((w) => w.hash),
    src,
  );
  if (!fresh)
    for (const w of watches) {
      // (what it writes out is shown with it either way)
      if (outside.get(w.hash)?.some((e) => e !== "output")) continue;
      const r = cb.store.watchResult(w.hash, node);
      if (r?.ok) cached.set(w.hash, r);
    }
  const ran = evaluateWatches(
    watches.filter((w) => !cached.has(w.hash)),
    src,
    cb.lay,
  );
  const out = new Map<number, WatchResult & { cached: boolean }>();
  for (const w of watches) {
    const c = cached.get(w.hash);
    if (c) {
      out.set(w.line, { line: w.line, ...c, cached: true });
      continue;
    }
    const r = ran.find((x) => x.line === w.line);
    if (!r) continue;
    out.set(w.line, { ...r, cached: false });
    if (!r.infrastructure) cb.store.putWatchResult(w.hash, node, r);
  }
  return out;
}

uses

used by