Ask for a why and its file's text at once on the why page
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- The page read the why, then the file's text at the why's commit. Live on dev-oscabriel the platform spent 4.8-6.9 s on a cold why and then 1.5-1.7 s on the text on a cold isolate, one after the other. - Both are asked for at once at the page's revision; should it have moved between the two (main landing in between), the text is read again at the why's commit, so the two still agree. - Tests (red first, why.test.tsx): both calls under way at once and one file read; a moved main reads the text again at the why's commit.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+131 added−11 removed
- tests:+110 added−2 removed
- other:+21 added−9 removed
Provenance
- Commit
69fe19f- Tree
1524385- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
4d45855
2 files changed+131 added−11 removed
modified:apps/web/src/routes/$project/why/$rev/$.tsx+21 added-9 removed
18 unmodified lines1920212223242523242526272829303120 unmodified lines5253545554555656575859606162636465666768695859607071727374757644 unmodified lines18 unmodified linesimport { serverClient } from "../../../../server/client.ts";
// Why each line of a file at a revision is there, read on the server: the// blame's ranges with their commits and landings, and the file's text at// the commit the blame resolved, so the two always agree. The browser then// draws the file with a row above each range. An address naming no// project, revision or file is a 404 showing the platform's refusal.// the commit the blame resolved, so the two always agree (both asked for// at once, the text again only if the revision moved between them). The// browser then draws the file with a row above each range. An address// naming no project, revision or file is a 404 showing the platform's// refusal.
const WhyPageData = Schema.Struct({ project: ProjectName, rev: Schema.String,20 unmodified lines UnknownProject: notFoundFor, UnknownRevision: notFoundFor, };
const view = yield* client .why({ at: rev, path, project }) .pipe(Effect.catchTags(refused)); // Both at once: a cold why takes seconds, and the file's text needs // none of it. Should the revision have moved between the two reads, // the text is read again at the why's own commit. const [view, read] = yield* Effect.all( [ client.why({ at: rev, path, project }), client.readFile({ at: rev, path, project }), ], { concurrency: 2 } ).pipe(Effect.catchTags(refused));
const file = read.at === view.at ? read const file = yield* client .readFile({ at: view.at, path, project }) .pipe(Effect.catchTags(refused)); : yield* client .readFile({ at: view.at, path, project }) .pipe(Effect.catchTags(refused));
return yield* Schema.encodeEffect(WhyPageData)({ project, rev,44 unmodified linesmodified:apps/web/test/why.test.tsx+110 added-2 removed
5 unmodified lines6 * answer, its commit and its people; the browser's drawing is Pierre's7 * (`<diffs-container>`), loaded only there.8 */9 import { describe, expect, it } from "@effect/vitest";10+import { Client, Contracts } from "@repo/protocol/contracts";10-import { CommitView, WhyView } from "@repo/protocol/domain/code";11+import { CommitView, FileView, WhyView } from "@repo/protocol/domain/code";12 import { LandingView } from "@repo/protocol/domain/landing";13 import { Outcome } from "@repo/protocol/domain/outcome";14 import { LandedSha, ProjectName } from "@repo/protocol/ids";14-import { Schema } from "effect";15+import { asTester, unanswered } from "@repo/protocol/testing";16+import { Duration, Effect, Layer, Ref, Schema } from "effect";17+import { RpcTest } from "effect/rpc";18 import type { ReactElement } from "react";19 import { renderToStaticMarkup } from "react-dom/server";20 21 import { LandingDetails } from "../src/features/why/range-summary.tsx";22 import { WhyCode } from "../src/features/why/why-code.tsx";23 import { WhyLines } from "../src/features/why/why-view.tsx";24+import { readWhyPage } from "../src/routes/$project/why/$rev/$.tsx";25 26 const markupOf = (element: ReactElement) => renderToStaticMarkup(element);27 28 /** What a reader sees: the markup without its tags, entities decoded. */278 unmodified lines307 markupOf(<WhyCode project={datekit} text={TEXT} view={view} />)308 ).toContain("<diffs-container");309 });310 });311+312+/** What the platform was asked by one page read: each `readFile`'s `at`, and the most calls under way at once. */313+interface Asked {314+ readonly files: readonly string[];315+ readonly peak: number;316+ readonly under: number;317+}318+319+/**320+ * A platform answering `why` with `view` and `readFile` with the file at321+ * the commit `main` names (`mainAt`), each after a few milliseconds, every322+ * call noted in `asked`.323+ */324+const whyPlatform = (asked: Ref.Ref<Asked>, mainAt: string) => {325+ const during = <A, E>(answer: Effect.Effect<A, E>) =>326+ Effect.acquireUseRelease(327+ Ref.update(asked, (was) => ({328+ ...was,329+ peak: Math.max(was.peak, was.under + 1),330+ under: was.under + 1,331+ })),332+ () => Effect.andThen(Effect.sleep(Duration.millis(30)), answer),333+ () => Ref.update(asked, (was) => ({ ...was, under: was.under - 1 }))334+ );335+336+ return Layer.effect(Client, RpcTest.makeClient(Contracts)).pipe(337+ Layer.provide([338+ Layer.mergeAll(339+ unanswered,340+ Contracts.toLayerHandler("why", () => during(Effect.succeed(view))),341+ Contracts.toLayerHandler("readFile", (payload) =>342+ during(343+ Effect.gen(function* () {344+ yield* Ref.update(asked, (was) => ({345+ ...was,346+ files: [...was.files, payload.at ?? "main"],347+ }));348+349+ const at =350+ payload.at === undefined || payload.at === "main"351+ ? mainAt352+ : payload.at;353+354+ return yield* Schema.decodeEffect(FileView)({355+ at,356+ binary: false,357+ path: "src/format.ts",358+ sha: "5".repeat(40),359+ size: 6,360+ text: `at ${at.slice(0, 4)}\n`,361+ truncated: false,362+ }).pipe(Effect.orDie);363+ })364+ )365+ )366+ ),367+ asTester,368+ ])369+ );370+};371+372+const nothingAsked: Asked = { files: [], peak: 0, under: 0 };373+374+const whyAddress = { _splat: "src/format.ts", project: "datekit", rev: "main" };375+376+describe("the why page's read", () => {377+ it.live("asks for the why and the file's text at once", () =>378+ Effect.gen(function* () {379+ const asked = yield* Ref.make(nothingAsked);380+381+ const read = yield* readWhyPage(whyAddress).pipe(382+ // A not-found is TanStack's own error, typed `any`: neither case383+ // here reads one.384+ Effect.orDie,385+ Effect.provide(whyPlatform(asked, AT))386+ );387+388+ expect(read.text).toBe("at aaaa\n");389+ expect(yield* Ref.get(asked)).toStrictEqual({390+ files: ["main"],391+ peak: 2,392+ under: 0,393+ });394+ })395+ );396+397+ it.live(398+ "reads the file again at the why's commit when main moved between the two reads",399+ () =>400+ Effect.gen(function* () {401+ const asked = yield* Ref.make(nothingAsked);402+403+ const read = yield* readWhyPage(whyAddress).pipe(404+ // A not-found is TanStack's own error, typed `any`: neither case405+ // here reads one.406+ Effect.orDie,407+ Effect.provide(whyPlatform(asked, "b".repeat(40)))408+ );409+410+ expect(read.text).toBe("at aaaa\n");411+ expect((yield* Ref.get(asked)).files).toStrictEqual(["main", AT]);412+ })413+ );414+});