Proposal for Adding `Atom.tap` Utility for Side Effects

Thoughts on adding an Atom.tap utility for triggering arbitrary side effects from atoms, without needing to create and mount a dedicated atom? Aka. something like the following:

import { Atom } from "@effect-atom/atom-react"
import { BrowserKeyValueStore } from "@effect/platform-browser"
import { Schema as S } from "effect"

export const themeAtom = Atom.kvs({
  runtime: Atom.runtime(BrowserKeyValueStore.layerLocalStorage),
  key: "theme",
  schema: S.Literal("dark", "light"),
  defaultValue: () => matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light",
}).pipe(
  Atom.keepAlive,
  Atom.tap((_get, theme) => {
    const { classList } = document.documentElement
    classList.remove(theme === "light" ? "dark" : "light")
    classList.add(theme)
  }),
)
Was this page helpful?