Open History to see the last commit
1import { RegistryContext } from "@effect/atom-react";
2import { type Atom, AtomRegistry } from "effect/reactivity";
3import { createElement } from "react";
4
5export const appAtomRegistry = AtomRegistry.make();
6
7export function AppAtomRegistryProvider({ children }: React.PropsWithChildren) {
8 return createElement(RegistryContext.Provider, { value: appAtomRegistry }, children);
9}
10
11/**
12 * Calls `listener` with each value after the current one. A bare `subscribe` never computes a
13 * derived atom nobody has read, so it never learns its parents and never fires.
14 */
15export function subscribeAtomChanges<A>(
16 atom: Atom.Atom<A>,
17 listener: (value: A) => void,
18): () => void {
19 let current = true;
20 return appAtomRegistry.subscribe(
21 atom,
22 (value) => {
23 if (current) {
24 current = false;
25 return;
26 }
27 listener(value);
28 },
29 { immediate: true },
30 );
31}