Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { deadlineStore, minuteTick } from "@t3tools/client-runtime/clock";
3import { Atom } from "effect/reactivity";
4
5import {
6 reconcilePendingThreadOrder,
7 type PendingThreadOrder,
8} from "../features/threads/threadOrder";
9import {
10 getThreadListV2OrderedSection,
11 nextSnoozeWakeAfter,
12} from "../features/threads/threadListV2";
13import { appAtomRegistry } from "./atom-registry";
14import { environmentServerConfigsAtom } from "./server";
15import { steadyNavigationThreadShellsAtom } from "./threads";
16import { queuedThreadKeysAtom } from "./use-thread-outbox";
17
18export const threadArrangementOpenAtom = Atom.make(false).pipe(Atom.keepAlive);
19
20export const threadDropBusyAtom = Atom.make(false).pipe(Atom.keepAlive);
21
22export const pendingThreadOrderAtom = Atom.make<PendingThreadOrder | null>(null).pipe(
23 Atom.keepAlive,
24);
25
26export function usePendingThreadOrder() {
27 return useAtomValue(pendingThreadOrderAtom);
28}
29
30let refreshPendingOrder: (() => void) | undefined;
31// Only one hold is live at a time; starting another tears down the previous
32// hold's subscriptions so its clocks and listeners do not outlive it.
33let teardownPendingOrder: (() => void) | undefined;
34
35export function getPendingThreadOrder(): PendingThreadOrder | null {
36 refreshPendingOrder?.();
37 return appAtomRegistry.get(pendingThreadOrderAtom);
38}
39
40export function beginPendingThreadOrder(pending: PendingThreadOrder) {
41 const unsubscribers: (() => void)[] = [];
42 // A timed wake can change section membership without a shell event, so the
43 // hold also re-checks on each minute and at the next snooze expiry.
44 let wake: { readonly atMs: number | null; readonly unsubscribe: () => void } | null = null;
45 const armWake = (atMs: number | null) => {
46 if (wake?.atMs === atMs) return;
47 wake?.unsubscribe();
48 wake = { atMs, unsubscribe: deadlineStore(atMs).subscribe(refresh) };
49 };
50 const teardown = () => {
51 for (const unsubscribe of unsubscribers.splice(0)) unsubscribe();
52 wake?.unsubscribe();
53 wake = null;
54 };
55 const cancel = () => {
56 if (refreshPendingOrder !== refresh) return;
57 refreshPendingOrder = undefined;
58 teardownPendingOrder = undefined;
59 teardown();
60 appAtomRegistry.set(pendingThreadOrderAtom, null);
61 };
62 const refresh = () => {
63 if (refreshPendingOrder !== refresh) return;
64 const current = appAtomRegistry.get(pendingThreadOrderAtom);
65 if (current === null) return;
66 const configs = appAtomRegistry.get(environmentServerConfigsAtom);
67 const threads = appAtomRegistry.get(steadyNavigationThreadShellsAtom);
68 const nowMs = Date.now();
69 const ordered = getThreadListV2OrderedSection({
70 threads,
71 section: current.section,
72 now: new Date(nowMs).toISOString(),
73 queuedThreadKeys: appAtomRegistry.get(queuedThreadKeysAtom),
74 settlementEnvironmentIds: new Set(configs.keys()),
75 snoozeEnvironmentIds: new Set(configs.keys()),
76 });
77 const next = reconcilePendingThreadOrder(current, ordered);
78 if (next === null) {
79 cancel();
80 return;
81 }
82 if (next !== current) appAtomRegistry.set(pendingThreadOrderAtom, next);
83 armWake(nextSnoozeWakeAfter(threads, nowMs));
84 };
85 teardownPendingOrder?.();
86 refreshPendingOrder = refresh;
87 teardownPendingOrder = teardown;
88 appAtomRegistry.set(pendingThreadOrderAtom, pending);
89 unsubscribers.push(
90 appAtomRegistry.subscribe(steadyNavigationThreadShellsAtom, refresh),
91 appAtomRegistry.subscribe(environmentServerConfigsAtom, refresh),
92 appAtomRegistry.subscribe(queuedThreadKeysAtom, refresh),
93 minuteTick.subscribe(refresh),
94 );
95 armWake(nextSnoozeWakeAfter(appAtomRegistry.get(steadyNavigationThreadShellsAtom), Date.now()));
96 return {
97 isPending: () => {
98 refresh();
99 return refreshPendingOrder === refresh;
100 },
101 complete: () => {
102 if (refreshPendingOrder !== refresh) return;
103 const current = appAtomRegistry.get(pendingThreadOrderAtom);
104 if (current !== null) {
105 appAtomRegistry.set(pendingThreadOrderAtom, { ...current, commandsComplete: true });
106 refresh();
107 }
108 },
109 cancel,
110 };
111}