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

preview.ts

107 lines4.1 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 function createPreviewEnvironmentAtoms<R, E>(
13 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
14) {
15 const lifecycleScheduler = createAtomCommandScheduler();
16 const statusScheduler = createAtomCommandScheduler();
17 const automationScheduler = createAtomCommandScheduler();
18 const lifecycleConcurrency = {
19 mode: "serial" as const,
20 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
21 JSON.stringify([environmentId, input.threadId]),
22 };
23 return {
24 list: createEnvironmentRpcQueryAtomFamily(runtime, {
25 label: "environment-data:preview:list",
26 tag: WS_METHODS.previewList,
27 staleTimeMs: 5_000,
28 }),
29 events: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
30 label: "environment-data:preview:events",
31 tag: WS_METHODS.subscribePreviewEvents,
32 }),
33 discoveredServers: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
34 label: "environment-data:preview:discovered-servers",
35 tag: WS_METHODS.subscribeDiscoveredLocalServers,
36 }),
37 automationRequests: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
38 label: "environment-data:preview:automation-requests",
39 tag: WS_METHODS.previewAutomationConnect,
40 // Automation requests are commands, not cached query data. Dispose the
41 // stream immediately with its owner so stale requests cannot replay when
42 // a thread remounts and the server can clear disconnected hosts promptly.
43 idleTtlMs: 0,
44 }),
45 open: createEnvironmentRpcCommand(runtime, {
46 label: "environment-data:preview:open",
47 tag: WS_METHODS.previewOpen,
48 scheduler: lifecycleScheduler,
49 concurrency: lifecycleConcurrency,
50 }),
51 navigate: createEnvironmentRpcCommand(runtime, {
52 label: "environment-data:preview:navigate",
53 tag: WS_METHODS.previewNavigate,
54 scheduler: lifecycleScheduler,
55 concurrency: lifecycleConcurrency,
56 }),
57 refresh: createEnvironmentRpcCommand(runtime, {
58 label: "environment-data:preview:refresh",
59 tag: WS_METHODS.previewRefresh,
60 scheduler: lifecycleScheduler,
61 concurrency: lifecycleConcurrency,
62 }),
63 close: createEnvironmentRpcCommand(runtime, {
64 label: "environment-data:preview:close",
65 tag: WS_METHODS.previewClose,
66 scheduler: lifecycleScheduler,
67 concurrency: lifecycleConcurrency,
68 }),
69 reportStatus: createEnvironmentRpcCommand(runtime, {
70 label: "environment-data:preview:report-status",
71 tag: WS_METHODS.previewReportStatus,
72 scheduler: statusScheduler,
73 concurrency: {
74 mode: "latest",
75 key: ({ environmentId, input }) =>
76 JSON.stringify([environmentId, input.threadId, input.tabId]),
77 },
78 }),
79 respondToAutomation: createEnvironmentRpcCommand(runtime, {
80 label: "environment-data:preview:automation-respond",
81 tag: WS_METHODS.previewAutomationRespond,
82 scheduler: automationScheduler,
83 concurrency: {
84 mode: "singleFlight",
85 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.requestId]),
86 },
87 }),
88 reportAutomationOwner: createEnvironmentRpcCommand(runtime, {
89 label: "environment-data:preview:automation-report-owner",
90 tag: WS_METHODS.previewAutomationReportOwner,
91 scheduler: automationScheduler,
92 concurrency: {
93 mode: "serial",
94 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.clientId]),
95 },
96 }),
97 clearAutomationOwner: createEnvironmentRpcCommand(runtime, {
98 label: "environment-data:preview:automation-clear-owner",
99 tag: WS_METHODS.previewAutomationClearOwner,
100 scheduler: automationScheduler,
101 concurrency: {
102 mode: "serial",
103 key: ({ environmentId, input }) => JSON.stringify([environmentId, input.clientId]),
104 },
105 }),
106 };