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

environmentAtomList.ts

21 lines770 Bytes

1import type { EnvironmentId } from "@t3tools/contracts";
2import { Atom } from "effect/reactivity";
3​
4export const environmentIdsKey = (environmentIds: ReadonlyArray<EnvironmentId>) =>
5 environmentIds.join("\n");
6​
7/**
8 * Reads one per-environment atom for a list of environments. The list is keyed by its ids, so
9 * renders that rebuild an equal list read the same atom instead of a new one whose fresh
10 * snapshot would schedule another render.
11 */
12export function environmentAtomList<A>(
13 label: string,
14 atom: (environmentId: EnvironmentId) => Atom.Atom<A>,
15) {
16 return Atom.family((key: string) =>
17 Atom.make((get) =>
18 key === "" ? [] : (key.split("\n") as EnvironmentId[]).map((id) => get(atom(id))),
19 ).pipe(Atom.withLabel(`${label}:${key}`)),
20 );