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

threads.ts

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