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

threads.ts

95 lines3.7 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 type { EnvironmentId, ThreadId } from "@t3tools/contracts";
14import { AsyncResult, Atom } from "effect/reactivity";
15​
16import { environmentCatalog } from "../connection/catalog";
17import { connectionAtomRuntime } from "../connection/runtime";
18import { scopedThreadKey } from "../lib/scopedEntities";
19import { appAtomRegistry } from "./atom-registry";
20import {
21 pendingThreadCreationOutcomesAtom,
22 selectThreadCreationDetail,
23} from "./pending-thread-creation";
24import { serverEnvironment } from "./server";
25import { environmentShell } from "./shell";
26import { threadOutboxManager } from "./thread-outbox";
27​
28export const threadFamilies = createThreadFamilyAtoms(
29 connectionAtomRuntime,
30 environmentShell.stateAtom,
31);
32export const olderSettledThreads = createOlderSettledThreadAtoms(
33 connectionAtomRuntime,
34 environmentShell.stateAtom,
35);
36export const threadEnvironment = createThreadEnvironmentAtoms(
37 connectionAtomRuntime,
38 threadFamilies.snapshotAtom,
39);
40export const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
41export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
42 environmentThreads.stateAtom,
43);
44export const environmentThreadShells = createEnvironmentThreadShellAtoms({
45 catalogValueAtom: environmentCatalog.catalogValueAtom,
46 snapshotAtom: threadEnvironment.snapshotAtom,
47 hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
48});
49export const childThreadApprovalsAtom = createChildThreadApprovalsAtom({
50 childThreadInputsAtom: environmentThreadShells.childThreadInputsAtom,
51 pendingRequestsAtom: environmentThreadDetails.pendingRequestsAtom,
52});
53​
54// Long enough for the opened screen to subscribe; its subscription keeps the thread after this.
55const OPENING_THREAD_HOLD_MS = 3_000;
56​
57/**
58 * Starts loading a thread being opened before its screen renders and subscribes, which
59 * otherwise waits for navigation to commit.
60 */
61export function startOpeningEnvironmentThread(environmentId: EnvironmentId, threadId: ThreadId) {
62 // A thread whose creation is still queued would load as not found and stay deleted;
63 // the opened screen loads it once creation is delivered.
64 const { detailRef } = selectThreadCreationDetail({
65 threadRef: { environmentId, threadId },
66 threadKey: scopedThreadKey(environmentId, threadId),
67 queuedMessagesByThreadKey: appAtomRegistry.get(
68 threadOutboxManager.queuedMessagesByThreadKeyAtom,
69 ),
70 outcomes: appAtomRegistry.get(pendingThreadCreationOutcomesAtom),
71 });
72 if (detailRef === null) return;
73 const release = appAtomRegistry.mount(environmentThreads.stateAtom(environmentId, threadId));
74 setTimeout(release, OPENING_THREAD_HOLD_MS);
76​
77const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
78 Atom.withLabel("mobile-environment-thread:empty"),
79);
80​
81export function useEnvironmentThread(
82 environmentId: EnvironmentId | null,
83 threadId: ThreadId | null,
84): EnvironmentThreadState {
85 const result = useAtomValue(
86 environmentId !== null && threadId !== null
87 ? environmentThreads.stateAtom(environmentId, threadId)
88 : EMPTY_THREAD_STATE_ATOM,
89 );
90 const state = AsyncResult.getOrElse(
91 result,
92 () => EMPTY_ENVIRONMENT_THREAD_STATE,
93 ) as EnvironmentThreadState;
94 return state;