-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use this with Svelte 5 runes? #54
Comments
Hello @kristianmandrup , I'm not very familiar with Svelte 5 and I plan to wait until it's actually released to do any work specific to it. That being said, in Svelte 4 you would just have to do this <script>
import { source } from 'sveltekit-sse'
import { messageStore } from 'message-store'
const connection = source('/events/app')
const message = connection.select('message')
$: messageStore.addMessage($message)
</script>
// Your markup goes here.
Because I'm assuming the equivalent in Svelte 5 is something like this <script>
import { source } from 'sveltekit-sse'
import { messageStore } from 'message-store'
const connection = source('/events/app')
const message = connection.select('message')
$effect(function run() {
messageStore.addMessage($message)
})
</script>
// Your markup goes here.
I haven't tested it though. Edit: from a quick search I found this https://svelte-5-preview.vercel.app/docs/faq#breaking-changes-and-migration Let me know if this answers your question. |
Thanks a lot for your answer. For now I seem to have it working using the following pattern: const transformed = channel.transform(function run(data) {
if (data === '') return;
// TODO: parse json
return `${data}`;
});
let projects = $state<ProjectPayload[]>([]);
let lastMessage = $state<string>('');
transformed.subscribe((value: string) => {
if (!value) return;
lastMessage = value;
// TODO: move to transform function
const json = JSON.parse(value);
const { source, model, action, data } = json;
if (model !== 'project') {
console.log('not a project event');
return;
}
const project = data;
// TODO: depending on the event, add, remove or update the project
projects = [...projects, project];
});
import { getToastState } from '$lib/toast-state.svelte';
const toastState = getToastState();
const toastMap = new Map<string, unknown>();
$effect(() => {
const message = lastMessage;
if (!message) return;
const json = JSON.parse(message);
const { data } = json;
if (!data) {
console.error('missing data', json);
}
const { name, description } = data;
if (!name) {
console.log('missing name', data);
return;
}
// already processed
if (toastMap.get(name)) {
console.log('already made toast for', name);
return;
}
toastMap.set(name, data);
toastState.add(name, description);
}); Currently overly verbose and complicated, but it gets the job done. The key was to simply subscribe to the readvalue store returned by transform transformed.subscribe((value: string) => { ... }) Then from there I could set a |
That is expected behavior, the first value is always empty because it takes time to actually open the connection to the server. It's either that or force userland to deal with a A third solution would be initializing the value in SSR but that would mean your server would have to open a connection to itself to read the value, which is a waste of resources and will probably slow down the SSR itself by a lot, especially in a cloud environment where you're not 100% the node doing the SSR will actually connect to itself. It could connect to a different twin node and slow down things even more. |
Any update on a new major version that uses runes instead of stores? It's not blocking but it'll be a great improvement |
Hello @WarningImHack3r , I've had a few attempts at trying to convert the usage of stores into runes since Svelte 5 came out. I wasn't really happy with the outcome, specifically the api is quite more clunky when it comes down to observing new incoming data outside of the Svelte Template. To achieve the same results it is required to use the Just because you reminded me of this, I'll give it another shot this weekend, I'll try come up with something, but stores are pretty damn good at this type of stuff, so it'll be hard to come up with something better, imo. I'll update you, have a good day. |
@razshare thanks for your reply! Good luck with your attempts, take your time! (side-note: I think this issue should be closed as "won't fix"/"not planned" instead of "solved" for the time being, even though it'll eventually be solved when you drop a rune-powered version) |
I would like aggregate the SSE events in a messages state/store. Would I run the source in an onMount or $effect?
It is unclear how to use it with stores
The text was updated successfully, but these errors were encountered: