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

threads.ts

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