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

Manager.ts

514 lines18.1 KB

1/**
2 * In-memory PreviewManager implementation.
3 *
4 * Sessions are keyed by `(threadId, tabId)`; a single thread can host
5 * multiple tabs (browser-style). `open` always creates a new tab — tab
6 * lifecycle is owned by the renderer.
7 *
8 * Events are published via Effect's `PubSub`, so subscriber failures are
9 * isolated from the publishing call (a closed WS subscriber queue cannot
10 * fail an in-progress `navigate()`).
11 */
12import {
13 type PreviewCloseInput,
14 type PreviewEvent,
15 type PreviewError,
16 PreviewInvalidUrlError,
17 type PreviewListInput,
18 type PreviewListResult,
19 type PreviewNavigateInput,
20 type PreviewOpenInput,
21 type PreviewRefreshInput,
22 type PreviewReportStatusInput,
23 type PreviewResizeInput,
24 type PreviewAdjustInput,
25 FILL_PREVIEW_VIEWPORT,
26 PreviewSessionLookupError,
27 type PreviewSessionSnapshot,
28} from "@t3tools/contracts";
29import {
30 isPreviewUrlNormalizationError,
31 newPreviewTabId,
32 normalizePreviewUrl,
33} from "@t3tools/shared/preview";
34import * as Context from "effect/Context";
35import * as Crypto from "effect/Crypto";
36import * as DateTime from "effect/DateTime";
37import * as Effect from "effect/Effect";
38import * as Layer from "effect/Layer";
39import * as PubSub from "effect/PubSub";
40import * as Scope from "effect/Scope";
41import * as Stream from "effect/Stream";
42import * as SynchronizedRef from "effect/SynchronizedRef";
43import { PreviewControlRequiredError } from "@t3tools/contracts";
44​
45export class PreviewManager extends Context.Service<
46 PreviewManager,
47 {
48 readonly open: (
49 input: PreviewOpenInput & {
50 readonly automationOwner?: string;
51 /** Runs before the `opened` event publishes, so subscribers find state keyed by the tab. */
52 readonly beforePublish?: (snapshot: PreviewSessionSnapshot) => void;
53 },
54 ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
55 readonly navigate: (
56 input: PreviewNavigateInput,
57 ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
58 readonly reportStatus: (
59 input: PreviewReportStatusInput & { readonly serverControlled?: boolean },
60 ) => Effect.Effect<void, PreviewError>;
61 readonly requestReveal: (
62 input: PreviewCloseInput & { readonly tabId: string; readonly force: boolean },
63 ) => Effect.Effect<void, PreviewError>;
64 readonly resize: (
65 input: PreviewResizeInput,
66 ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
67 /**
68 * Records a server tab's appearance or zoom and publishes it; the server's
69 * browser applies it to the page. Any client may send it, as it changes how
70 * the page renders, not what it does.
71 */
72 readonly adjust: (
73 input: PreviewAdjustInput,
74 ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
75 readonly refresh: (input: PreviewRefreshInput) => Effect.Effect<void, PreviewError>;
76 readonly close: (input: PreviewCloseInput) => Effect.Effect<void, PreviewError>;
77 readonly list: (input: PreviewListInput) => Effect.Effect<PreviewListResult>;
78 readonly events: Stream.Stream<PreviewEvent>;
79 readonly subscribeEvents: Effect.Effect<PubSub.Subscription<PreviewEvent>, never, Scope.Scope>;
80 }
81>()("t3/preview/Manager/PreviewManager") {}
82​
83interface PreviewSessionState {
84 readonly threadId: string;
85 readonly tabId: string;
86 readonly snapshot: PreviewSessionSnapshot;
88​
89interface ManagerState {
90 /** All sessions across every thread, keyed by `${threadId}\u0000${tabId}`. */
91 readonly sessions: ReadonlyMap<string, PreviewSessionState>;
92 /** Global monotonic revision establishing list/event ordering. */
93 readonly revision: number;
95​
96const initialState: ManagerState = { sessions: new Map(), revision: 0 };
97​
98type PreviewEventDraft = PreviewEvent extends infer Event
99 ? Event extends { readonly revision: number }
100 ? Omit<Event, "revision" | "serverEpoch">
101 : never
102 : never;
103​
104const compositeKey = (threadId: string, tabId: string): string => `${threadId}\u0000${tabId}`;
105​
106const sessionsForThread = (
107 state: ManagerState,
108 threadId: string,
109): ReadonlyArray<PreviewSessionState> => {
110 const out: PreviewSessionState[] = [];
111 for (const session of state.sessions.values()) {
112 if (session.threadId === threadId) out.push(session);
113 }
114 return out;
115};
116​
117const normalizeUrl = (rawUrl: string): Effect.Effect<string, PreviewInvalidUrlError> =>
118 Effect.try({
119 try: () => normalizePreviewUrl(rawUrl),
120 catch: (cause) => {
121 if (isPreviewUrlNormalizationError(cause)) {
122 return new PreviewInvalidUrlError({
123 inputLength: cause.inputLength,
124 reason: cause.reason,
125 protocol: cause.protocol,
126 cause,
127 });
128 }
129​
130 return new PreviewInvalidUrlError({
131 inputLength: rawUrl.length,
132 reason: "unexpected",
133 cause,
134 });
135 },
136 });
137​
138const currentIsoTimestamp = DateTime.now.pipe(Effect.map(DateTime.formatIso));
139​
140/** @public Service construction is part of the canonical Effect module API. */
141export const make = Effect.gen(function* PreviewManagerMake() {
142 const crypto = yield* Crypto.Crypto;
143 const serverEpoch = yield* crypto.randomUUIDv4.pipe(Effect.orDie);
144 const stateRef = yield* SynchronizedRef.make<ManagerState>(initialState);
145 // Unbounded PubSub is fine here — events are tiny and we don't want to
146 // block publishers if a subscriber is slow. WS clients backpressure on
147 // their own queues downstream.
148 const eventsPubSub = yield* PubSub.unbounded<PreviewEvent>();
149 const events: Stream.Stream<PreviewEvent> = Stream.fromPubSub(eventsPubSub);
150​
151 /**
152 * Atomic read-modify-write over the session for `(threadId, tabId)`. The
153 * mutator runs under the SynchronizedRef so concurrent writers cannot
154 * interleave. Lookup failures travel through the modify result so both
155 * branches yield the same `[A, S]` shape `modifyEffect` requires.
156 *
157 * The event is published INSIDE the lock so observers see events in the
158 * same order as the underlying state transitions. Publishing an unbounded
159 * PubSub is non-blocking, so this is cheap.
160 */
161 const mutateExistingSession = <R, E>(
162 threadId: string,
163 tabId: string,
164 mutator: (
165 session: PreviewSessionState,
166 ) => Effect.Effect<{ next: PreviewSessionState; emit: PreviewEventDraft | null; result: R }, E>,
167 ): Effect.Effect<R, E | PreviewSessionLookupError> => {
168 type ModifyResult =
169 | { kind: "fail"; error: PreviewSessionLookupError }
170 | { kind: "ok"; result: R };
171​
172 return SynchronizedRef.modifyEffect(stateRef, (state) => {
173 const session = state.sessions.get(compositeKey(threadId, tabId));
174 if (!session) {
175 return Effect.succeed([
176 { kind: "fail", error: new PreviewSessionLookupError({ threadId, tabId }) },
177 state,
178 ] as readonly [ModifyResult, ManagerState]);
179 }
180 return mutator(session).pipe(
181 Effect.flatMap(
182 Effect.fn("PreviewManager.commitMutation")(function* ({ next, emit, result }) {
183 const revision = emit ? state.revision + 1 : state.revision;
184 if (emit) {
185 yield* PubSub.publish(eventsPubSub, {
186 ...emit,
187 revision,
188 serverEpoch,
189 } as PreviewEvent);
190 }
191 const sessions = new Map(state.sessions);
192 sessions.set(compositeKey(threadId, tabId), next);
193 return [{ kind: "ok", result } as ModifyResult, { sessions, revision }] as readonly [
194 ModifyResult,
195 ManagerState,
196 ];
197 }),
198 ),
199 );
200 }).pipe(
201 Effect.flatMap((modify) =>
202 modify.kind === "fail" ? Effect.fail(modify.error) : Effect.succeed(modify.result),
203 ),
204 );
205 };
206​
207 const open: PreviewManager["Service"]["open"] = Effect.fn("PreviewManager.open")(
208 function* (input) {
209 const runtime = input.runtime;
210 // Persisted client surfaces must not bind to a different tab after a server restart.
211 const tabId = `${newPreviewTabId()}${runtime === "server" ? `_${serverEpoch}` : ""}`;
212 const updatedAt = yield* currentIsoTimestamp;
213 const snapshot: PreviewSessionSnapshot = {
214 threadId: input.threadId,
215 tabId,
216 navStatus: input.url
217 ? { _tag: "Loading", url: yield* normalizeUrl(input.url), title: "" }
218 : { _tag: "Idle" },
219 canGoBack: false,
220 canGoForward: false,
221 viewport: input.viewport ?? FILL_PREVIEW_VIEWPORT,
222 ...(input.profileId === undefined ? {} : { profileId: input.profileId }),
223 ...(runtime === undefined ? {} : { runtime }),
224 ...(runtime === "server" && input.automationOwner !== undefined
225 ? { automationOwner: input.automationOwner }
226 : {}),
227 ...(input.reveal === undefined ? {} : { reveal: input.reveal }),
228 updatedAt,
229 };
230 yield* SynchronizedRef.modifyEffect(stateRef, (state) =>
231 Effect.gen(function* () {
232 const revision = state.revision + 1;
233 const sessions = new Map(state.sessions);
234 sessions.set(compositeKey(input.threadId, tabId), {
235 threadId: input.threadId,
236 tabId,
237 snapshot,
238 });
239 input.beforePublish?.(snapshot);
240 yield* PubSub.publish(eventsPubSub, {
241 type: "opened",
242 threadId: input.threadId,
243 tabId,
244 createdAt: snapshot.updatedAt,
245 serverEpoch,
246 revision,
247 snapshot,
248 });
249 return [snapshot, { sessions, revision }] as const;
250 }),
251 );
252 return snapshot;
253 },
254 );
255​
256 const navigate: PreviewManager["Service"]["navigate"] = Effect.fn("PreviewManager.navigate")(
257 function* (input) {
258 const url = yield* normalizeUrl(input.url);
259 return yield* mutateExistingSession(
260 input.threadId,
261 input.tabId,
262 Effect.fn("PreviewManager.navigateSession")(function* (session) {
263 if (session.snapshot.runtime === "server")
264 return yield* new PreviewControlRequiredError({ tabId: input.tabId });
265 const updatedAt = yield* currentIsoTimestamp;
266 const previousTitle =
267 session.snapshot.navStatus._tag === "Idle" ? "" : session.snapshot.navStatus.title;
268 const resolvedTitle = input.resolvedTitle ?? previousTitle;
269 const snapshot: PreviewSessionSnapshot = {
270 ...session.snapshot,
271 navStatus: { _tag: "Success", url, title: resolvedTitle },
272 viewport: session.snapshot.viewport ?? FILL_PREVIEW_VIEWPORT,
273 updatedAt,
274 };
275 return {
276 next: { ...session, snapshot },
277 emit: {
278 type: "navigated",
279 threadId: session.threadId,
280 tabId: session.tabId,
281 createdAt: snapshot.updatedAt,
282 snapshot,
283 },
284 result: snapshot,
285 };
286 }),
287 );
288 },
289 );
290​
291 const reportStatus: PreviewManager["Service"]["reportStatus"] = Effect.fn(
292 "PreviewManager.reportStatus",
293 )(function* (input) {
294 yield* mutateExistingSession(
295 input.threadId,
296 input.tabId,
297 Effect.fn("PreviewManager.reportSessionStatus")(function* (session) {
298 if (session.snapshot.runtime === "server" && !input.serverControlled)
299 return yield* new PreviewControlRequiredError({ tabId: input.tabId });
300 const updatedAt = yield* currentIsoTimestamp;
301 const snapshot: PreviewSessionSnapshot = {
302 ...session.snapshot,
303 navStatus: input.navStatus,
304 canGoBack: input.canGoBack,
305 canGoForward: input.canGoForward,
306 viewport: session.snapshot.viewport ?? FILL_PREVIEW_VIEWPORT,
307 updatedAt,
308 };
309 const emit: PreviewEventDraft =
310 input.navStatus._tag === "LoadFailed"
311 ? {
312 type: "failed",
313 threadId: session.threadId,
314 tabId: session.tabId,
315 createdAt: snapshot.updatedAt,
316 url: input.navStatus.url,
317 title: input.navStatus.title,
318 code: input.navStatus.code,
319 description: input.navStatus.description,
320 }
321 : {
322 type: "navigated",
323 threadId: session.threadId,
324 tabId: session.tabId,
325 createdAt: snapshot.updatedAt,
326 snapshot,
327 };
328 return {
329 next: { ...session, snapshot },
330 emit,
331 result: undefined as void,
332 };
333 }),
334 );
335 });
336​
337 const resize: PreviewManager["Service"]["resize"] = Effect.fn("PreviewManager.resize")(
338 function* (input) {
339 return yield* mutateExistingSession(
340 input.threadId,
341 input.tabId,
342 // Any client may size a tab, as the desktop has always allowed; the
343 // server's browser follows the published setting for its own tabs.
344 Effect.fn("PreviewManager.resizeSession")(function* (session) {
345 const updatedAt = yield* currentIsoTimestamp;
346 const snapshot: PreviewSessionSnapshot = {
347 ...session.snapshot,
348 viewport: input.viewport,
349 updatedAt,
350 };
351 return {
352 next: { ...session, snapshot },
353 emit: {
354 type: "resized",
355 threadId: session.threadId,
356 tabId: session.tabId,
357 createdAt: snapshot.updatedAt,
358 snapshot,
359 },
360 result: snapshot,
361 };
362 }),
363 );
364 },
365 );
366​
367 const adjust: PreviewManager["Service"]["adjust"] = Effect.fn("PreviewManager.adjust")(
368 function* (input) {
369 return yield* mutateExistingSession(
370 input.threadId,
371 input.tabId,
372 Effect.fn("PreviewManager.adjustSession")(function* (session) {
373 const updatedAt = yield* currentIsoTimestamp;
374 const snapshot: PreviewSessionSnapshot = {
375 ...session.snapshot,
376 ...(input.colorScheme === undefined ? {} : { colorScheme: input.colorScheme }),
377 ...(input.zoomFactor === undefined ? {} : { zoomFactor: input.zoomFactor }),
378 updatedAt,
379 };
380 // One-off requests ride on the event for the server's browser to act on.
381 const request =
382 input.hardReload || input.clear
383 ? {
384 request: {
385 ...(input.hardReload ? { hardReload: true } : {}),
386 ...(input.clear ? { clear: input.clear } : {}),
387 },
388 }
389 : {};
390 return {
391 next: { ...session, snapshot },
392 emit: {
393 ...request,
394 type: "resized",
395 threadId: session.threadId,
396 tabId: session.tabId,
397 createdAt: snapshot.updatedAt,
398 snapshot,
399 },
400 result: snapshot,
401 };
402 }),
403 );
404 },
405 );
406​
407 const refresh: PreviewManager["Service"]["refresh"] = Effect.fn("PreviewManager.refresh")(
408 function* (input) {
409 // Verify the session exists; the desktop bridge handles the actual reload
410 // and will report progress back via `reportStatus`. No event emitted.
411 yield* mutateExistingSession(input.threadId, input.tabId, (session) =>
412 Effect.succeed({ next: session, emit: null, result: undefined as void }),
413 );
414 },
415 );
416​
417 const close: PreviewManager["Service"]["close"] = Effect.fn("PreviewManager.close")(
418 function* (input) {
419 const createdAt = yield* currentIsoTimestamp;
420 yield* SynchronizedRef.modifyEffect(stateRef, (state) => {
421 const eventsToEmit: PreviewEvent[] = [];
422 const sessions = new Map(state.sessions);
423 const targets = input.tabId
424 ? [state.sessions.get(compositeKey(input.threadId, input.tabId))].filter(
425 (entry): entry is PreviewSessionState => entry !== undefined,
426 )
427 : sessionsForThread(state, input.threadId);
428 let revision = state.revision;
429 for (const target of targets) {
430 revision += 1;
431 sessions.delete(compositeKey(target.threadId, target.tabId));
432 eventsToEmit.push({
433 type: "closed",
434 threadId: target.threadId,
435 tabId: target.tabId,
436 createdAt,
437 serverEpoch,
438 revision,
439 });
440 }
441 if (eventsToEmit.length === 0) {
442 return Effect.succeed([undefined, state] as const);
443 }
444 return Effect.as(
445 Effect.forEach(eventsToEmit, (event) => PubSub.publish(eventsPubSub, event), {
446 discard: true,
447 }),
448 [undefined, { sessions, revision }] as const,
449 );
450 });
451 },
452 );
453​
454 const list: PreviewManager["Service"]["list"] = Effect.fn("PreviewManager.list")(
455 function* (input) {
456 return yield* SynchronizedRef.get(stateRef).pipe(
457 Effect.map((state): PreviewListResult => ({
458 sessions: sessionsForThread(state, input.threadId)
459 .map((s) => s.snapshot)
460 .toSorted((a, b) => a.updatedAt.localeCompare(b.updatedAt)),
461 serverEpoch,
462 revision: state.revision,
463 })),
464 );
465 },
466 );
467​
468 const requestReveal: PreviewManager["Service"]["requestReveal"] = Effect.fn(
469 "PreviewManager.requestReveal",
470 )(function* (input) {
471 yield* mutateExistingSession(
472 input.threadId,
473 input.tabId,
474 Effect.fn("PreviewManager.revealSession")(function* (session) {
475 const snapshot = {
476 ...session.snapshot,
477 reveal: true,
478 revealRequest: {
479 id: yield* crypto.randomUUIDv4.pipe(Effect.orDie),
480 force: input.force,
481 },
482 updatedAt: yield* currentIsoTimestamp,
483 };
484 return {
485 next: { ...session, snapshot },
486 emit: {
487 type: "navigated" as const,
488 threadId: session.threadId,
489 tabId: session.tabId,
490 createdAt: snapshot.updatedAt,
491 snapshot,
492 },
493 result: undefined,
494 };
495 }),
496 );
497 });
498​
499 return PreviewManager.of({
500 open,
501 requestReveal,
502 navigate,
503 reportStatus,
504 resize,
505 adjust,
506 refresh,
507 close,
508 list,
509 events,
510 subscribeEvents: PubSub.subscribe(eventsPubSub),
511 });
512}).pipe(Effect.withSpan("PreviewManager.make"));
513​
514export const layer = Layer.effect(PreviewManager, make);