Skip to content
Source
Sign inSign up

useDesktopLocalBootstraps.ts

26 lines1.0 KB

1import { useAtomValue } from "@effect/atom-react";
2import type { DesktopEnvironmentBootstrap } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4​
5import { readDesktopSecondaryBootstraps } from "./desktopLocal";
6​
7const DESKTOP_LOCAL_BOOTSTRAP_POLL_MS = 2_000;
8​
9/**
10 * The bridge exposes no change event, so this re-reads on an interval while
11 * anything reads it; failed reads retain the latest successful snapshot, while
12 * a successful empty read clears it. The reader returns the same array while
13 * nothing changed, so an unchanged poll notifies no one.
14 */
15const desktopLocalBootstrapsAtom = Atom.make((get) => {
16 const interval = setInterval(
17 () => get.setSelf(readDesktopSecondaryBootstraps()),
18 DESKTOP_LOCAL_BOOTSTRAP_POLL_MS,
19 );
20 get.addFinalizer(() => clearInterval(interval));
21 return readDesktopSecondaryBootstraps();
22}).pipe(Atom.withLabel("desktop-local-bootstraps"));
23​
24export function useDesktopLocalBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
25 return useAtomValue(desktopLocalBootstrapsAtom);