82 lines · 2.4 KB · TypeScript
RawDownload
import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";import type { EnvironmentId } from "@t3tools/contracts";import {  isWorkspaceBrowserPreviewPath,  isWorkspaceImagePreviewPath,} from "@t3tools/shared/filePreview"; import { appAtomRegistry } from "../../state/atom-registry";import { projectEnvironment } from "../../state/projects";import { isVideoPreviewFile } from "./filePath";import { prepareSourceFileDocument } from "./source-file-document";import { sourceHighlightAtom } from "./sourceHighlightingState";import type { ReviewDiffTheme } from "../review/shikiReviewHighlighter"; const inFlightPreloads = new Map<string, Promise<void>>();const MAX_HIGHLIGHT_PRELOAD_CHARACTERS = 256 * 1024; function preloadKey(input: {  readonly cwd: string;  readonly environmentId: EnvironmentId;  readonly relativePath: string;}): string {  return JSON.stringify([input.environmentId, input.cwd, input.relativePath]);} export function preloadWorkspaceFileContents(input: {  readonly cwd: string;  readonly environmentId: EnvironmentId;  readonly relativePath: string;  readonly theme: ReviewDiffTheme;}): void {  if (    isWorkspaceBrowserPreviewPath(input.relativePath) ||    isWorkspaceImagePreviewPath(input.relativePath) ||    isVideoPreviewFile(input.relativePath)  ) {    return;  }   const key = preloadKey(input);  if (inFlightPreloads.has(key)) {    return;  }   const preload = executeAtomQuery(    appAtomRegistry,    projectEnvironment.readFile({      environmentId: input.environmentId,      input: { cwd: input.cwd, relativePath: input.relativePath },    }),    {      label: "workspace file preload",      reportDefect: false,      reportFailure: false,    },  )    .then(async (result) => {      if (result._tag === "Success") {        const document = prepareSourceFileDocument(result.value.contents);        if (document.contents.length <= MAX_HIGHLIGHT_PRELOAD_CHARACTERS) {          await executeAtomQuery(            appAtomRegistry,            sourceHighlightAtom({              path: input.relativePath,              contents: document.contents,              theme: input.theme,            }),            {              label: "workspace source highlight preload",              reportDefect: false,              reportFailure: false,            },          );        }      }    })    .finally(() => {      inFlightPreloads.delete(key);    });   inFlightPreloads.set(key, preload);}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t