Skip to content
Source
Sign inSign up

environmentAtomList.ts

17 lines614 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/** Key by environment ids so equal input lists reuse the atom and snapshot. */
8export function environmentAtomList<A>(
9 label: string,
10 atom: (environmentId: EnvironmentId) => Atom.Atom<A>,
11) {
12 return Atom.family((key: string) =>
13 Atom.make((get) =>
14 key === "" ? [] : (key.split("\n") as EnvironmentId[]).map((id) => get(atom(id))),
15 ).pipe(Atom.withLabel(`${label}:${key}`)),
16 );