client-code.tsx
This file
Path
apps/web/src/features/code/client-code.tsx
133 lines · 4.0 KiB · TypeScript
Last changed by
Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 1why, one step back1/**Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 2–16why, one step back2 * A file and a commit's diff, drawn in the browser over what the server3 * sent. The server renders `fallback`: the code it highlighted4 * (`ServerCode`), or numbered lines or the plain-text diff for code it5 * does not highlight. After hydration the browser loads the drawing,6 * starts the worker pool and highlights the code there first7 * (`prepareFile`, `prepareDiffs`); only then does the drawing take the8 * fallback's place, in one render, drawing the highlighted code at once.9 * So code the server highlighted never visibly changes, and plain code10 * changes once, to highlighted.11 *12 * The fallback stays where it is until then (no `ClientOnly`, whose13 * switch would mount a second copy of it without the server's drawing).14 * `import.meta.env.SSR` is `true` in the Worker's build, so the dynamic15 * imports below are dead code there and the Worker bundles none of the16 * drawings (`bytes:check`), as `features/finish/client-diff.tsx` does.Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 17why, one step back17 */Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 18–20why, one step back18import { Effect, Fiber } from "effect";19import { Suspense, useEffect, useRef, useState } from "react";20import type { ComponentType, ReactNode } from "react";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 21–25why, one step back2122// Type-only imports are erased, so the server bundles nothing of these.23import type { FileCodeProps } from "./code-view.tsx";24import type { DiffCodeProps } from "./diff-view.tsx";25Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 26–27why, one step back26/** The file's drawing, once its module is loaded and the file highlighted in the pool; none on the server. */27const fileDrawing = import.meta.env.SSRMerge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 28why, one step back28 ? undefinedMerge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 29–33why, one step back29 : (file: FileCodeProps) =>30 Effect.gen(function* () {31 const { FileCode, prepareFile } = yield* Effect.tryPromise(32 () => import("./code-view.tsx")33 );Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 34why, one step back34Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 35–41why, one step back35 yield* prepareFile(file);3637 return FileCode;38 });3940/** The diff's drawing, once its module is loaded and its first files highlighted; none on the server. */41const diffDrawing = import.meta.env.SSRMerge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 42why, one step back42 ? undefinedMerge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 43–47why, one step back43 : (files: Pick<DiffCodeProps, "drawn" | "files">) =>44 Effect.gen(function* () {45 const { DiffCode, prepareDiffs } = yield* Effect.tryPromise(46 () => import("./diff-view.tsx")47 );Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 48why, one step back48Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 49–84why, one step back49 yield* prepareDiffs(files);5051 return DiffCode;52 });5354/** A drawing, held in state: a component is a function, which state would call. */55interface Ready<P> {56 readonly Draw: ComponentType<P>;57}5859/**60 * Runs `ready` (load the drawing, prepare what it draws) and hands its61 * drawing to `done`; the returned function stops it, so a page left in62 * the meantime is not drawn. A drawing that cannot load leaves the63 * fallback in place.64 */65const start = <P, E>(66 ready: Effect.Effect<ComponentType<P>, E>,67 done: (ready: Ready<P>) => void68) => {69 const fiber = Effect.runFork(70 ready.pipe(71 Effect.andThen((Draw) =>72 Effect.sync(() => {73 done({ Draw });74 })75 ),76 Effect.ignore77 )78 );7980 return () => {81 Effect.runFork(Fiber.interrupt(fiber));82 };83};84Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 85–87why, one step back85export const ClientFile = ({86 fallback,87 ...propsMerge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 88–100why, one step back88}: FileCodeProps & { readonly fallback: ReactNode }) => {89 const { path, sha, text } = props;90 const [ready, setReady] = useState<Ready<FileCodeProps>>();9192 useEffect(93 () =>94 fileDrawing === undefined95 ? undefined96 : start(fileDrawing({ path, sha, text }), setReady),97 [path, sha, text]98 );99100 return ready === undefined ? (Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 101–102why, one step back101 fallback102 ) : (Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 103–105why, one step back103 <Suspense fallback={fallback}>104 <ready.Draw {...props} />105 </Suspense>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 106why, one step back106 );Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
line 107why, one step back107};Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 108–111why, one step back108109export const ClientDiffs = ({110 fallback,111 ...propsMerge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 112–126why, one step back112}: DiffCodeProps & { readonly fallback: ReactNode }) => {113 // The files as they first came: the drawing, once ready, follows any114 // change itself, so preparing is done once.115 const first = useRef({ drawn: props.drawn, files: props.files });116 const [ready, setReady] = useState<Ready<DiffCodeProps>>();117118 useEffect(119 () =>120 diffDrawing === undefined121 ? undefined122 : start(diffDrawing(first.current), setReady),123 []124 );125126 return ready === undefined ? (Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 127–128why, one step back127 fallback128 ) : (Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 129–131why, one step back129 <Suspense fallback={fallback}>130 <ready.Draw {...props} />131 </Suspense>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 132why, one step back132 );Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
line 133why, one step back133};