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

thread-order.ts

94 lines3.5 KB

1import { useAtomValue } from "@effect/atom-react";
2import { useEffect } from "react";
3import { Atom } from "effect/reactivity";
4​
5import {
6 reconcilePendingThreadOrder,
7 type PendingThreadOrder,
8} from "../features/threads/threadOrder";
9import { getThreadListV2OrderedSection } from "../features/threads/threadListV2";
10import { appAtomRegistry } from "./atom-registry";
11import { environmentServerConfigsAtom } from "./server";
12import { environmentThreadShells } from "./threads";
13import { queuedThreadKeysAtom } from "./use-thread-outbox";
14​
15export const threadArrangementOpenAtom = Atom.make(false).pipe(Atom.keepAlive);
16​
17export const threadDropBusyAtom = Atom.make(false).pipe(Atom.keepAlive);
18​
19export const pendingThreadOrderAtom = Atom.make<PendingThreadOrder | null>(null).pipe(
20 Atom.keepAlive,
21);
22​
23export function usePendingThreadOrder(nowMinute: string, snoozeWakeTick: number) {
24 const pending = useAtomValue(pendingThreadOrderAtom);
25 // Clock-driven wakes can change membership without shell events; retire order holds and re-enable moves then.
26 useEffect(() => {
27 getPendingThreadOrder();
28 }, [nowMinute, snoozeWakeTick]);
29 return pending;
31​
32let refreshPendingOrder: (() => void) | undefined;
33​
34export function getPendingThreadOrder(): PendingThreadOrder | null {
35 refreshPendingOrder?.();
36 return appAtomRegistry.get(pendingThreadOrderAtom);
38​
39export function beginPendingThreadOrder(pending: PendingThreadOrder) {
40 const unsubscribers: (() => void)[] = [];
41 const cancel = () => {
42 if (refreshPendingOrder !== refresh) return;
43 refreshPendingOrder = undefined;
44 for (const unsubscribe of unsubscribers) unsubscribe();
45 appAtomRegistry.set(pendingThreadOrderAtom, null);
46 };
47 const refresh = () => {
48 if (refreshPendingOrder !== refresh) return;
49 const current = appAtomRegistry.get(pendingThreadOrderAtom);
50 if (current === null) return;
51 const configs = appAtomRegistry.get(environmentServerConfigsAtom);
52 const ordered = getThreadListV2OrderedSection({
53 threads: appAtomRegistry.get(environmentThreadShells.threadShellsAtom),
54 section: current.section,
55 now: new Date().toISOString(),
56 queuedThreadKeys: appAtomRegistry.get(queuedThreadKeysAtom),
57 settlementEnvironmentIds: new Set(
58 [...configs].flatMap(([id, config]) =>
59 config.environment.capabilities.threadSettlement === true ? [id] : [],
60 ),
61 ),
62 snoozeEnvironmentIds: new Set(
63 [...configs].flatMap(([id, config]) =>
64 config.environment.capabilities.threadSnooze === true ? [id] : [],
65 ),
66 ),
67 });
68 const next = reconcilePendingThreadOrder(current, ordered);
69 if (next === null) cancel();
70 else if (next !== current) appAtomRegistry.set(pendingThreadOrderAtom, next);
71 };
72 refreshPendingOrder = refresh;
73 appAtomRegistry.set(pendingThreadOrderAtom, pending);
74 unsubscribers.push(
75 appAtomRegistry.subscribe(environmentThreadShells.threadShellsAtom, refresh),
76 appAtomRegistry.subscribe(environmentServerConfigsAtom, refresh),
77 appAtomRegistry.subscribe(queuedThreadKeysAtom, refresh),
78 );
79 return {
80 isPending: () => {
81 refresh();
82 return refreshPendingOrder === refresh;
83 },
84 complete: () => {
85 if (refreshPendingOrder !== refresh) return;
86 const current = appAtomRegistry.get(pendingThreadOrderAtom);
87 if (current !== null) {
88 appAtomRegistry.set(pendingThreadOrderAtom, { ...current, commandsComplete: true });
89 refresh();
90 }
91 },
92 cancel,
93 };