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

threads.ts

117 lines4.8 KB

1import { createChildThreadApprovalsAtom } from "@t3tools/client-runtime/state/thread-requests";
2import { useAtomValue } from "@effect/atom-react";
3import {
4 createEnvironmentThreadDetailAtoms,
5 createEnvironmentThreadShellAtoms,
6 createEnvironmentThreadStateAtoms,
7 EMPTY_ENVIRONMENT_THREAD_STATE,
8 type EnvironmentThreadState,
9 createOlderSettledThreadAtoms,
10 createThreadEnvironmentAtoms,
11 createThreadFamilyAtoms,
12} from "@t3tools/client-runtime/state/threads";
13import { createSteadyShellAtoms } from "@t3tools/client-runtime/state/shell";
14import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
15import { AsyncResult, Atom } from "effect/reactivity";
16​
17import { environmentCatalog } from "../connection/catalog";
18import { connectionAtomRuntime } from "../connection/runtime";
19import { scopedThreadKey } from "../lib/scopedEntities";
20import { appAtomRegistry } from "./atom-registry";
21import {
22 pendingThreadCreationOutcomesAtom,
23 selectThreadCreationDetail,
24} from "./pending-thread-creation";
25import { serverEnvironment } from "./server";
26import { environmentShell } from "./shell";
27import { environmentPresentations } from "./presentation";
28import { threadOutboxManager } from "./thread-outbox";
29​
30export const threadFamilies = createThreadFamilyAtoms(
31 connectionAtomRuntime,
32 environmentShell.stateAtom,
33);
34export const olderSettledThreads = createOlderSettledThreadAtoms(
35 connectionAtomRuntime,
36 environmentShell.stateAtom,
37);
38export const threadEnvironment = createThreadEnvironmentAtoms(
39 connectionAtomRuntime,
40 threadFamilies.snapshotAtom,
41 environmentShell.statusAtom,
42);
43export const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
44export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
45 environmentThreads.stateAtom,
46);
47export const environmentThreadShells = createEnvironmentThreadShellAtoms({
48 catalogValueAtom: environmentCatalog.catalogValueAtom,
49 connectionPhaseAtom: environmentPresentations.connectionPhaseAtom,
50 snapshotAtom: threadEnvironment.snapshotAtom,
51 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
52});
53const steadyShells = createSteadyShellAtoms({
54 catalogValueAtom: environmentCatalog.catalogValueAtom,
55 snapshotAtom: threadEnvironment.snapshotAtom,
56 shellStateAtom: environmentShell.stateValueAtom,
57 rowActionsAtom: threadEnvironment.rowActionsAtom,
58 connectionStateAtom: environmentCatalog.stateAtom,
59});
60/**
61 * The threads that lists arrange. Rows wait while their environment synchronizes, so a list
62 * changes once when environments connect, not once per environment.
63 */
64export const steadyShellsHoldingAtom = steadyShells.holdingAtom;
65export const steadyNavigationThreadShellsAtom = createEnvironmentThreadShellAtoms({
66 catalogValueAtom: environmentCatalog.catalogValueAtom,
67 connectionPhaseAtom: environmentPresentations.connectionPhaseAtom,
68 snapshotAtom: steadyShells.snapshotAtom,
69 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
70}).navigationThreadShellsAtom;
71export const childThreadApprovalsAtom = createChildThreadApprovalsAtom({
72 childThreadInputsAtom: environmentThreadShells.childThreadInputsAtom,
73 pendingRequestsAtom: environmentThreadDetails.pendingRequestsAtom,
74});
75​
76// Long enough for the opened screen to subscribe; its subscription keeps the thread after this.
77const OPENING_THREAD_HOLD_MS = 3_000;
78​
79/**
80 * Starts loading a thread being opened before its screen renders and subscribes, which
81 * otherwise waits for navigation to commit.
82 */
83export function startOpeningEnvironmentThread(environmentId: EnvironmentId, threadId: ThreadId) {
84 // A thread whose creation is still queued would load as not found and stay deleted;
85 // the opened screen loads it once creation is delivered.
86 const { detailRef } = selectThreadCreationDetail({
87 threadRef: { environmentId, threadId },
88 threadKey: scopedThreadKey(environmentId, threadId),
89 queuedMessagesByThreadKey: appAtomRegistry.get(
90 threadOutboxManager.queuedMessagesByThreadKeyAtom,
91 ),
92 outcomes: appAtomRegistry.get(pendingThreadCreationOutcomesAtom),
93 });
94 if (detailRef === null) return;
95 const release = appAtomRegistry.mount(environmentThreads.stateAtom(environmentId, threadId));
96 setTimeout(release, OPENING_THREAD_HOLD_MS);
98​
99const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
100 Atom.withLabel("mobile-environment-thread:empty"),
101);
102​
103export function useEnvironmentThread(
104 environmentId: EnvironmentId | null,
105 threadId: ThreadId | null,
106): EnvironmentThreadState {
107 const result = useAtomValue(
108 environmentId !== null && threadId !== null
109 ? environmentThreads.stateAtom(environmentId, threadId)
110 : EMPTY_THREAD_STATE_ATOM,
111 );
112 const state = AsyncResult.getOrElse(
113 result,
114 () => EMPTY_ENVIRONMENT_THREAD_STATE,
115 ) as EnvironmentThreadState;
116 return state;