Skip to content
Source
Sign inSign up

threadForkNavigation.ts

20 lines779 Bytes

1import type { ScopedThreadRef } from "@t3tools/contracts";
2​
3import { appAtomRegistry } from "../../state/atom-registry";
4import { environmentThreadShells } from "../../state/threads";
5​
6const TIMEOUT_MS = 2_000;
7const POLL_INTERVAL_MS = 40;
8​
9/** Resolves true once a thread this client just created, such as a fork, reaches its state. */
10export async function waitForThreadShell(ref: ScopedThreadRef): Promise<boolean> {
11 const atom = environmentThreadShells.threadShellAtom(ref);
12 const read = () => appAtomRegistry.get(atom) !== null;
13 const deadline = Date.now() + TIMEOUT_MS;
14 while (!read() && Date.now() < deadline) {
15 await new Promise<void>((resolve) => {
16 setTimeout(resolve, Math.min(POLL_INTERVAL_MS, deadline - Date.now()));
17 });
18 }
19 return read();