effect icon indicating copy to clipboard operation
effect copied to clipboard

Add an `@effect/atom-svelte` package.

Open thomasfosterau opened this issue 4 months ago • 2 comments

What is the problem this feature would solve?

Currently there are @effect/atom-react, @effect/atom-solid, and @effect/atom-vue packages, but no equivalent for Svelte.

What is the feature you are proposing to solve the problem?

A new @effect/atom-svelte package which is analogous to the @effect/atom-react, @effect/atom-solid, and @effect/atom-vue packages.

The easiest implementation would be to use the createSubscriber function from svelte/reactivity, which was introduced in Svelte v5.7.0. This bypasses having to use Svelte 5's runes feature and appears to be intended for exactly this kind of integration. This would not require a .svelte.ts file, and could be implemented as plain TypeScript.

There may also be opportunities to integrate with Svelte's Context feature. Perhaps the createContext function?

What alternatives have you considered?

This would not be difficult to implement as a third party package, but obviously it would be preferable to have a first-party @effect/atom-svelte package.

Note: I previously opened a PR for an @effect/atom-svelte package in the Effect-TS/effect-smol repository before it way merged into this one.

thomasfosterau avatar Jul 18 '26 15:07 thomasfosterau

Willing to donate some inference for this! I am currently porting it for personal use with Astra but I can't attest the quality of the code.

cachesdev avatar Sep 06 '26 19:09 cachesdev

Hi all, I've built a Svelte adapter along these lines and wanted to share it, along with a small registry change it could benefit from.

effect-atom-svelte (docs, npm) works with effect 4.0 and Svelte 5.57.2+ with async mode on. It follows @effect/atom-react's API, but hooks return a reactive .current, as is usual in Svelte:

  • the usual hooks: useAtom, useAtomValue, useAtomSet, useAtomRef and so on
  • useAtomSuspense and useAtomResult, which let a component await an async atom
  • server rendering with a registry per request, and hydration
  • ScopedAtom and a few SvelteKit helpers

It's tested in Chromium, Firefox and WebKit, and most pages of the docs have a live example. Most of the code was written with Claude, and I've reviewed and tested it. Building it turned up a few bugs, now fixed or in review: #8771 and #8820 here, and sveltejs/svelte#18931, sveltejs/svelte#18933 and sveltejs/svelte#18927.

A registry PR

The adapter relies on two things that have no public API yet, so it uses the registry's internals and has to pin effect@~4.0.0:

  1. Initial values on an existing registry. AtomRegistry.make({ initialValues }) sets them when the registry is created, but useAtomInitialValues sets them later. On the server, the adapter also has to keep that atom alive and render its value without running its read, which is often browser-only.
  2. Dropping hydrated values that nothing read. They stay queued in the registry, so a registry shared between server requests would hand them to a later request.

It also works around hydrated wrapped atoms, such as withReactivity queries, fetching again (#8398), which #8399 should fix.

I've opened #8874 to add these to AtomRegistry. It stands on its own, whether or not a Svelte package ends up here, and I'm happy to reshape it however you prefer.

Upstreaming

If you'd want an official @effect/atom-svelte, I'd be glad to port a trimmed core of the library to the monorepo's conventions: the hooks, registry context, ScopedAtom, SSR and hydration. Happy to maintain it or hand it over if you'd rather someone else did. If it's better as a community package for now, that's fine too, and I welcome any contributors!

jarrednorrisdev avatar Oct 06 '26 23:10 jarrednorrisdev