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

threads.ts

88 lines3.3 KB

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