atomic-server icon indicating copy to clipboard operation
atomic-server copied to clipboard

Svelte example is broken - store initialization does not work

Open joepio opened this issue 1 year ago • 3 comments

this is from the docs:

// App.svelte

<script lang="ts">
  import { initStore } from '@tomic/svelte';
  import { Store } from '@tomic/lib';

  onMount(() => {
    // This is where you configure your atomic data store.
    const store = new Store();
    initStore(store);
  });
</script>

// do sveltey things

But it errors, according to @AlexMikhalev

We do something completely different in the atomic-sveltekit-demo:

import { get } from 'svelte/store';
import { initStore, store as atomicStore } from '@tomic/svelte';
import { Store } from '@tomic/lib';
import { currentSiteConfig } from '$lib/siteConfigs';

const init = () => {
	const serverUrl = new URL(currentSiteConfig.siteResource);
	serverUrl.pathname = '/';

	const atomicStore = new Store({
		serverUrl: serverUrl.toString(),
	});
	initStore(atomicStore);
};

export const getStore = () => {
	let store = get(atomicStore);

	if (store === undefined) {
		init();
		store = get(atomicStore);
	}

	return store;
};

We should update the svelte docs or better: fix the init code!

joepio avatar Jan 03 '24 12:01 joepio

Perhaps we need an initSvelteKit function?

joepio avatar Jan 08 '24 12:01 joepio

But perhaps solving this before the runes refactor isn't the best idea.

joepio avatar Jan 08 '24 12:01 joepio

Sample in a doc will be fine - I managed to get it working by adding code to main.ts and to App.svelte, I just don't know which code works.

AlexMikhalev avatar Jan 08 '24 22:01 AlexMikhalev