Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

preview.ts

115 lines4.4 KB

1import { HtmlPreviewColor, WS_METHODS, type ScopedThreadRef } from "@t3tools/contracts";
2import type { HtmlRenderTheme } from "@t3tools/shared/htmlRender";
3import * as Schema from "effect/Schema";
4import * as Stream from "effect/Stream";
5import { Atom } from "effect/reactivity";
6​
7import type { EnvironmentRegistry } from "../connection/registry.ts";
8import {
9 createAtomCommandScheduler,
10 createEnvironmentRpcCommand,
11 createEnvironmentRpcQueryAtomFamily,
12 createEnvironmentRpcSubscriptionAtomFamily,
13} from "./runtime.ts";
14​
15const idleHtmlPreviewTheme = Atom.make(null);
16const isHtmlPreviewColor = Schema.is(HtmlPreviewColor);
17​
18export function createPreviewEnvironmentAtoms<R, E>(
19 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
20) {
21 const lifecycleScheduler = createAtomCommandScheduler();
22 const statusScheduler = createAtomCommandScheduler();
23 const htmlPreviewTheme = createEnvironmentRpcSubscriptionAtomFamily(runtime, {
24 label: "environment-data:preview:html-theme",
25 tag: WS_METHODS.subscribeHtmlPreviewTheme,
26 idleTtlMs: 0,
27 });
28 const lifecycleConcurrency = {
29 mode: "serial" as const,
30 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
31 JSON.stringify([environmentId, input.threadId]),
32 };
33 return {
34 htmlPreviewTheme: (ref: ScopedThreadRef | null, theme: HtmlRenderTheme) =>
35 ref === null
36 ? idleHtmlPreviewTheme
37 : htmlPreviewTheme({
38 environmentId: ref.environmentId,
39 input: {
40 threadId: ref.threadId,
41 theme: {
42 appearance: theme.appearance,
43 colors: Object.fromEntries(
44 Object.entries(theme.variables).filter(([, value]) => isHtmlPreviewColor(value)),
45 ),
46 },
47 },
48 }),
49 list: createEnvironmentRpcQueryAtomFamily(runtime, {
50 label: "environment-data:preview:list",
51 tag: WS_METHODS.previewList,
52 staleTimeMs: 5_000,
53 }),
54 events: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
55 label: "environment-data:preview:events",
56 tag: WS_METHODS.subscribePreviewEvents,
57 // Stream atoms publish only the last value per chunk; every preview event must arrive.
58 transform: (stream) => stream.pipe(Stream.rechunk(1)),
59 }),
60 discoveredServers: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
61 label: "environment-data:preview:discovered-servers",
62 tag: WS_METHODS.subscribeDiscoveredLocalServers,
63 // Configured URLs are part of this atom's key. Dispose immediately so
64 // unmounted projects stop contributing probe candidates on the server.
65 idleTtlMs: 0,
66 }),
67 open: createEnvironmentRpcCommand(runtime, {
68 label: "environment-data:preview:open",
69 tag: WS_METHODS.previewOpen,
70 scheduler: lifecycleScheduler,
71 concurrency: lifecycleConcurrency,
72 }),
73 resize: createEnvironmentRpcCommand(runtime, {
74 label: "environment-data:preview:resize",
75 tag: WS_METHODS.previewResize,
76 scheduler: lifecycleScheduler,
77 concurrency: lifecycleConcurrency,
78 }),
79 adjust: createEnvironmentRpcCommand(runtime, {
80 label: "environment-data:preview:adjust",
81 tag: WS_METHODS.previewAdjust,
82 scheduler: lifecycleScheduler,
83 concurrency: lifecycleConcurrency,
84 }),
85 close: createEnvironmentRpcCommand(runtime, {
86 label: "environment-data:preview:close",
87 tag: WS_METHODS.previewClose,
88 scheduler: lifecycleScheduler,
89 concurrency: lifecycleConcurrency,
90 }),
91 clearProfile: createEnvironmentRpcCommand(runtime, {
92 label: "environment-data:preview:clear-profile",
93 tag: WS_METHODS.previewClearProfile,
94 }),
95 reportProfiles: createEnvironmentRpcCommand(runtime, {
96 label: "environment-data:preview:report-profiles",
97 tag: WS_METHODS.previewReportProfiles,
98 concurrency: { mode: "latest", key: ({ environmentId }) => environmentId },
99 }),
100 stopLocalServer: createEnvironmentRpcCommand(runtime, {
101 label: "environment-data:preview:stop-local-server",
102 tag: WS_METHODS.previewStopLocalServer,
103 }),
104 reportStatus: createEnvironmentRpcCommand(runtime, {
105 label: "environment-data:preview:report-status",
106 tag: WS_METHODS.previewReportStatus,
107 scheduler: statusScheduler,
108 concurrency: {
109 mode: "latest",
110 key: ({ environmentId, input }) =>
111 JSON.stringify([environmentId, input.threadId, input.tabId]),
112 },
113 }),
114 };