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

freshSessionArms.ts

30 lines1.3 KB

1import { useAtomValue } from "@effect/atom-react";
2import { Atom } from "effect/reactivity";
3​
4import { appAtomRegistry } from "../../rpc/atomRegistry";
5​
6/** Threads whose next message starts a fresh provider session, keyed by thread, valued by when it was armed. */
7const freshSessionArmsAtom = Atom.make<Readonly<Record<string, number>>>({}).pipe(Atom.keepAlive);
8​
9export function armFreshSession(threadKey: string): void {
10 appAtomRegistry.set(freshSessionArmsAtom, {
11 ...appAtomRegistry.get(freshSessionArmsAtom),
12 [threadKey]: Date.now(),
13 });
15​
16/** With `arm`, clears only that arm, so a send never consumes one made while it was in flight. */
17export function disarmFreshSession(threadKey: string, arm?: number): void {
18 const current = appAtomRegistry.get(freshSessionArmsAtom);
19 if (current[threadKey] === undefined || (arm !== undefined && current[threadKey] !== arm)) return;
20 const { [threadKey]: _removed, ...rest } = current;
21 appAtomRegistry.set(freshSessionArmsAtom, rest);
23​
24export function readFreshSessionArm(threadKey: string): number | undefined {
25 return appAtomRegistry.get(freshSessionArmsAtom)[threadKey];
27​
28export function useFreshSessionArmed(threadKey: string): boolean {
29 return useAtomValue(freshSessionArmsAtom)[threadKey] !== undefined;