woutdp / live_svelte

Svelte inside Phoenix LiveView with seamless end-to-end reactivity
https://hexdocs.pm/live_svelte
MIT License
1.2k stars 46 forks source link

Optimistic updates #135

Closed fullofcaffeine closed 4 months ago

fullofcaffeine commented 4 months ago

Hi!

I'm new to Svelte and Liveview, and I'm wondering if the current architecture of live_svelte would allow for easy handling of optimistic updates (which, from what I read, the lack of is one of the main downsides of pure live view due to high latency risks affecting the UX).

Thanks!

woutdp commented 4 months ago

Yes it's possible

Let's say you have a variable x that's synced with LiveView. So you'd have export let x. Then you can update that variable in the client by doing x = 1. But note that won't be synced to the server. So you'd have to update it with by sending a message to liveview. For example: live.pushEvent("set_x", {x: 1})

The first step, updating it in the client, would be the optimistic update. After pushEvent is handled by LiveView, it would override the client again with the new variable, which hopefully would be the same as you updated in the client so the user doesn't see it change.

Hopefully that makes sense

fullofcaffeine commented 4 months ago

Thanks for elaborating!