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

preview.ts

116 lines4.4 KB

1import { WS_METHODS } from "@t3tools/contracts";
2import { Atom } from "effect/unstable/reactivity";
3​
4import type { EnvironmentRegistry } from "../connection/registry.ts";
5import {
6 createAtomCommandScheduler,
7 createEnvironmentRpcCommand,
8 createEnvironmentRpcQueryAtomFamily,
9 createEnvironmentRpcSubscriptionAtomFamily,
10} from "./runtime.ts";
11​
12export const previewAutomationHostFocusConcurrencyKey = (value: {
13 readonly environmentId: string;
14 readonly input: {
15 readonly clientId: string;
16 readonly connectionId: string;
17 };
18}): string => JSON.stringify([value.environmentId, value.input.clientId, value.input.connectionId]);
19​
20export function createPreviewEnvironmentAtoms<R, E>(
21 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
22) {
23 const lifecycleScheduler = createAtomCommandScheduler();
24 const statusScheduler = createAtomCommandScheduler();
25 const automationScheduler = createAtomCommandScheduler();
26 const lifecycleConcurrency = {
27 mode: "serial" as const,
28 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
29 JSON.stringify([environmentId, input.threadId]),
30 };
31 return {
32 list: createEnvironmentRpcQueryAtomFamily(runtime, {
33 label: "environment-data:preview:list",
34 tag: WS_METHODS.previewList,
35 staleTimeMs: 5_000,
36 }),
37 events: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
38 label: "environment-data:preview:events",
39 tag: WS_METHODS.subscribePreviewEvents,
40 }),
41 discoveredServers: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
42 label: "environment-data:preview:discovered-servers",
43 tag: WS_METHODS.subscribeDiscoveredLocalServers,
44 // Configured URLs are part of this atom's key. Dispose immediately so
45 // unmounted projects stop contributing probe candidates on the server.
46 idleTtlMs: 0,
47 }),
48 automationRequests: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
49 label: "environment-data:preview:automation-requests",
50 tag: WS_METHODS.previewAutomationConnect,
51 // Automation requests are commands, not cached query data. Dispose the
52 // stream immediately with its owner so stale requests cannot replay when
53 // a thread remounts and the server can clear disconnected hosts promptly.
54 idleTtlMs: 0,
55 }),
56 open: createEnvironmentRpcCommand(runtime, {
57 label: "environment-data:preview:open",
58 tag: WS_METHODS.previewOpen,
59 scheduler: lifecycleScheduler,
60 concurrency: lifecycleConcurrency,
61 }),
62 navigate: createEnvironmentRpcCommand(runtime, {
63 label: "environment-data:preview:navigate",
64 tag: WS_METHODS.previewNavigate,
65 scheduler: lifecycleScheduler,
66 concurrency: lifecycleConcurrency,
67 }),
68 resize: createEnvironmentRpcCommand(runtime, {
69 label: "environment-data:preview:resize",
70 tag: WS_METHODS.previewResize,
71 scheduler: lifecycleScheduler,
72 concurrency: lifecycleConcurrency,
73 }),
74 refresh: createEnvironmentRpcCommand(runtime, {
75 label: "environment-data:preview:refresh",
76 tag: WS_METHODS.previewRefresh,
77 scheduler: lifecycleScheduler,
78 concurrency: lifecycleConcurrency,
79 }),
80 close: createEnvironmentRpcCommand(runtime, {
81 label: "environment-data:preview:close",
82 tag: WS_METHODS.previewClose,
83 scheduler: lifecycleScheduler,
84 concurrency: lifecycleConcurrency,
85 }),
86 reportStatus: createEnvironmentRpcCommand(runtime, {
87 label: "environment-data:preview:report-status",
88 tag: WS_METHODS.previewReportStatus,
89 scheduler: statusScheduler,
90 concurrency: {
91 mode: "latest",
92 key: ({ environmentId, input }) =>
93 JSON.stringify([environmentId, input.threadId, input.tabId]),
94 },
95 }),
96 respondToAutomation: createEnvironmentRpcCommand(runtime, {
97 label: "environment-data:preview:automation-respond",
98 tag: WS_METHODS.previewAutomationRespond,
99 scheduler: automationScheduler,
100 concurrency: {
101 mode: "singleFlight",
102 key: ({ environmentId, input }) =>
103 JSON.stringify([environmentId, input.connectionId, input.requestId]),
104 },
105 }),
106 focusAutomationHost: createEnvironmentRpcCommand(runtime, {
107 label: "environment-data:preview:automation-focus-host",
108 tag: WS_METHODS.previewAutomationFocusHost,
109 scheduler: automationScheduler,
110 concurrency: {
111 mode: "latest",
112 key: previewAutomationHostFocusConcurrencyKey,
113 },
114 }),
115 };