pool.tsx
This file
Path
apps/web/src/features/code/pool.tsx
109 lines · 3.7 KiB · TypeScript
Last changed by
Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
History of this fileTake it
Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 1–12why, one step back1/**2 * Highlighting off the main thread: Pierre's worker pool, which every3 * drawing of code under `CodePool` shares (`File`, `CodeView`). The pool is4 * made once for the tab, the first time a page draws code, and kept: the5 * next code page highlights in workers already warm, and a file drawn once6 * is not highlighted again (each drawing keys its file by blob id).7 *8 * The worker is Pierre's own module, bundled by Vite as a module worker.9 * Only the browser loads this file (through the lazy drawings).10 */11import { WorkerPoolContext } from "@pierre/diffs/react";12import { getOrCreateWorkerPoolSingleton } from "@pierre/diffs/worker";Fix the finish review's findings: drafted code draws, counts agree, commands hangoscabrielbefore adoptionf7f6e49
line 13why, one step back13import { Effect } from "effect";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 14why, one step back14import type { ReactNode } from "react";Fix the finish review's findings: drafted code draws, counts agree, commands hangoscabrielbefore adoptionf7f6e49
line 15why, one step back15import { use, useMemo } from "react";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 16–47why, one step back1617import { diffsOptions, registerCodeTheme } from "./pierre.ts";1819/** Two workers: a page draws one file or one diff at a time, and grammars load once per worker. */20const POOL_SIZE = 2;2122/** The tab's pool: made the first time code is drawn or warmed, the same after. */23export const workerPool = () => {24 registerCodeTheme();2526 return getOrCreateWorkerPoolSingleton({27 highlighterOptions: {28 lineDiffType: "word-line",29 maxLineDiffLength: diffsOptions.maxLineDiffLength,30 preferredHighlighter: diffsOptions.preferredHighlighter,31 theme: diffsOptions.theme,32 tokenizeMaxLineLength: diffsOptions.tokenizeMaxLineLength,33 },34 poolOptions: {35 onWorkerError: () => {36 // A worker that fails is Pierre's to replace; the code it was37 // drawing stays as plain text, which is what the page sent.38 },39 poolSize: POOL_SIZE,40 workerFactory: () =>41 new Worker(new URL("@pierre/diffs/worker/worker.js", import.meta.url), {42 type: "module",43 }),44 },45 });46};47Fix the finish review's findings: drafted code draws, counts agree, commands hangoscabrielbefore adoptionf7f6e49
lines 48–66why, one step back48type Pool = ReturnType<typeof workerPool>;4950/** One promise per pool, so `use` sees the same one on every render. */51const readiness = new WeakMap<Pool, Promise<void>>();5253/**54 * Settles once the pool's own highlighter is up (or has failed, when55 * Pierre draws on the main thread instead). Before that, Pierre draws a56 * file it would not highlight (a `.vm`, plain text) as nothing, and never57 * draws it again.58 */59const readyOf = (pool: Pool) => {60 const known = readiness.get(pool);6162 if (known !== undefined) {63 return known;64 }6566 // A pool already up is marked settled the way React reads a promise itMerge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 67–68why, one step back67 // has seen, so `use` does not hold the drawing back for a turn; one68 // still starting is marked so once it is up, whoever awaited it first.Fix the finish review's findings: drafted code draws, counts agree, commands hangoscabrielbefore adoptionf7f6e49
lines 69–74why, one step back69 const made = pool.isInitialized()70 ? Object.assign(Promise.resolve(), { status: "fulfilled" })71 : Effect.runPromise(72 Effect.ignore(Effect.tryPromise(() => pool.initialize()))73 );74Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 75–79why, one step back75 Effect.runFork(76 Effect.promise(() => made).pipe(77 Effect.map(() => Object.assign(made, { status: "fulfilled" }))78 )79 );Fix the finish review's findings: drafted code draws, counts agree, commands hangoscabrielbefore adoptionf7f6e49
lines 80–83why, one step back80 readiness.set(pool, made);8182 return made;83};Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 84–90why, one step back8485/**86 * Settles once the tab's pool is up (at once where there are no workers):87 * a drawing awaited on this then mounts with nothing left to wait for.88 */89export const poolReady = () =>90 "Worker" in globalThis ? readyOf(workerPool()) : Promise.resolve();Fix the finish review's findings: drafted code draws, counts agree, commands hangoscabrielbefore adoptionf7f6e49
lines 91–96why, one step back9192/**93 * The pool, for the drawings under it; none outside a browser. Until the94 * pool is up the drawings wait in their `Suspense`, whose fallback is the95 * server's plain text.96 */Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 97–102why, one step back97export const CodePool = ({ children }: { readonly children: ReactNode }) => {98 // Pierre keeps one pool for the tab: asking again hands back the same.99 const pool = useMemo(100 () => ("Worker" in globalThis ? workerPool() : undefined),101 []102 );Fix the finish review's findings: drafted code draws, counts agree, commands hangoscabrielbefore adoptionf7f6e49
lines 103–106why, one step back103104 if (pool !== undefined) {105 use(readyOf(pool));106 }Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 107–109why, one step back107108 return <WorkerPoolContext value={pool}>{children}</WorkerPoolContext>;109};