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

threads.ts

84 lines3.0 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 * as Option from "effect/Option";
14import { AsyncResult, Atom } from "effect/reactivity";
15import { useEffect } from "react";
16​
17import { environmentCatalog } from "../connection/catalog";
18import { connectionAtomRuntime } from "../connection/runtime";
19import { appAtomRegistry } from "./atom-registry";
20import { environmentShell } from "./shell";
21​
22export const threadFamilies = createThreadFamilyAtoms(
23 connectionAtomRuntime,
24 environmentShell.stateAtom,
25);
26export const olderSettledThreads = createOlderSettledThreadAtoms(
27 connectionAtomRuntime,
28 environmentShell.stateAtom,
29);
30export const threadEnvironment = createThreadEnvironmentAtoms(
31 connectionAtomRuntime,
32 threadFamilies.snapshotAtom,
33);
34export const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
35export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
36 environmentThreads.stateAtom,
37);
38export const environmentThreadShells = createEnvironmentThreadShellAtoms({
39 catalogValueAtom: environmentCatalog.catalogValueAtom,
40 snapshotAtom: threadEnvironment.snapshotAtom,
41});
42​
43/** Keeps the subagent tree under `rootThreadId` in the thread list while the caller shows it. */
44export function useThreadFamily(
45 environmentId: EnvironmentId | null,
46 rootThreadId: ThreadId | null,
47): void {
48 useEffect(() => {
49 if (environmentId === null || rootThreadId === null) return;
50 return threadFamilies.mount(appAtomRegistry, environmentId, rootThreadId);
51 }, [environmentId, rootThreadId]);
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 const release = appAtomRegistry.mount(environmentThreads.stateAtom(environmentId, threadId));
63 setTimeout(release, OPENING_THREAD_HOLD_MS);
65​
66const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(
67 Atom.withLabel("mobile-environment-thread:empty"),
68);
69​
70export function useEnvironmentThread(
71 environmentId: EnvironmentId | null,
72 threadId: ThreadId | null,
73): EnvironmentThreadState {
74 const result = useAtomValue(
75 environmentId !== null && threadId !== null
76 ? environmentThreads.stateAtom(environmentId, threadId)
77 : EMPTY_THREAD_STATE_ATOM,
78 );
79 const state = Option.getOrElse(
80 AsyncResult.value(result),
81 () => EMPTY_ENVIRONMENT_THREAD_STATE,
82 ) as EnvironmentThreadState;
83 return state;