Svelte 5 bindings for Effect Atom.
This package is a pure AI port based on the other Effect Atom adapters. It aims to follow the same shape and behavior, but it has not had the same level of manual validation, so things can break.
This package connects effect/unstable/reactivity atoms to Svelte's reactivity model. It provides a registry provider for scoped atom lifetimes and a small set of hooks for reading, writing, subscribing to, and awaiting atom state inside Svelte components.
effect v4 with unstable/reactivitypnpm add @sproott/effect-atom-svelte effect svelte
<script lang="ts">
import { Atom } from 'effect/unstable/reactivity'
import { RegistryProvider, useAtom } from '@sproott/effect-atom-svelte'
const counter = Atom.make(0)
const [value, setValue] = useAtom(() => counter)
</script>
<RegistryProvider>
<button onclick={() => setValue((current) => current + 1)}>
{value.current}
</button>
</RegistryProvider>
useAtom() returns a reactive wrapper with a .current getter and a setter function. Reading .current inside markup or reactive code subscribes the component to atom updates.
RegistryProvider creates an AtomRegistry for the current component subtree and disposes it automatically when the provider is destroyed.
Use it when you want:
<script lang="ts">
import { Atom } from 'effect/unstable/reactivity'
import { RegistryProvider, useAtomValue } from '@sproott/effect-atom-svelte'
const greeting = Atom.make('hello')
const value = useAtomValue(() => greeting)
</script>
<RegistryProvider initialValues={[[greeting, 'ahoj']]}>
<p>{value.current}</p>
</RegistryProvider>
If no provider is present, hooks fall back to a default shared registry.
const value = useAtomValue(() => atom)
console.log(value.current)
const [value, setValue] = useAtom(() => writableAtom)
setValue(1)
setValue((current) => current + 1)
const setValue = useAtomSet(() => writableAtom)
useAtomSubscribe(() => atom, (value) => {
console.log('changed', value)
})
useAtomInitialValues([
[counter, 10],
[otherAtom, 'ready'],
])
For atoms that resolve to AsyncResult, use useAtomResource() to expose a promise suitable for Svelte async handling.
const resource = useAtomResource(() => asyncAtom, {
suspendOnWaiting: true,
})
const refValue = useAtomRef(() => ref)
const countRef = useAtomRefProp(() => ref, 'count')
const countValue = useAtomRefPropValue(() => ref, 'count')
RegistryProvider: creates and provides a registry to a component subtreegetRegistry: resolves the active registry from context or the default shared registrysetRegistry: creates a registry manually and sets it in Svelte contextuseAtomValue: subscribe to an atom's current valueuseAtom: get a reactive value and setter pair for writable atomsuseAtomSet: get only the setter for a writable atomuseAtomRefresh: refresh an atom manuallyuseAtomMount: mount an atom in the current registryuseAtomSubscribe: subscribe to atom updates with a callbackuseAtomInitialValues: apply initial atom values once per registryuseAtomResource: expose AsyncResult atoms as a promise-backed resourceuseAtomRef: subscribe to an AtomRefuseAtomRefProp: derive a property ref from an AtomRefuseAtomRefPropValue: subscribe to a property ref valuepnpm run build
pnpm run check
pnpm run test
Releases are published from GitHub Actions on v* tags using npm trusted publishing.