Skip to content

Svelte example is broken - store initialization does not work #779

@joepio

Description

@joepio

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!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions