state-adapt icon indicating copy to clipboard operation
state-adapt copied to clipboard

[FEATURE]: Svelte Support

Open mfp22 opened this issue 5 months ago • 2 comments

Creating this issue to track progress and get feedback.

So far I see two potential ways to work with runes:

Option 1: Exposing getter properties

import { adapt } from '../state-adapt';
import { countAdapter } from './count.adapter';

const count = useAdapt(0, countAdapter);
count.double; // evaluated once
const triple = $derived(count.current * 3);
const quadruple = $derived(count.double * 2); // `double` selector

Option 2: Exposing plain functions

import { adapt } from '../state-adapt';
import { countAdapter } from './count.adapter';

const count = useAdapt(0, countAdapter);
count.double; // function that can be called anywhere for up-to-date values
const triple = $derived(count.state() * 3); // Original state selector name
const quadruple = $derived(count.double() * 2); // `double` selector

mfp22 avatar Jun 20 '25 10:06 mfp22

And I updated the Svelte starter example to include a component with this experiment: https://stackblitz.com/edit/vitejs-vite-szsd3d?file=src%2Flib%2FRuneCounter.svelte

No type-checking, and that's annoying. If anyone knows how to fix that, please comment.

mfp22 avatar Jun 20 '25 10:06 mfp22

Asking for feedback directly on X too https://x.com/mfpears/status/1936012137954213907

Option 1 seems more svelte

Image

I think I'll do store in components, and adapt for stores you can import. It's similar to the createSubscriber functionality. I don't want eager subs so I need 2 ways to creates stores, and a way to activate the shared ones in components.

mfp22 avatar Jun 20 '25 11:06 mfp22