admin管理员组文章数量:1287246
I'm building a real-time transcript viewer using Svelte 5 and Supabase. I need to subscribe to changes in a JSONB column that contains conversation transcripts.
Here's my current approach:
let transcripts = $state([])
let subscription
onMount(() => {
subscription = supabase
.from('demos')
.on('UPDATE', payload => {
transcripts = payload.new.transcripts
})
.subscribe()
})
However, I'm unsure if this is the optimal way to handle subscriptions with the new runes system. Should I be using $derived or other runes? How do I properly clean up the subscription?
First Attempt:
<script>
let transcripts = $state([]);
let subscription;
onMount(() => {
subscription = supabase
.from('demos')
.on('UPDATE', payload => {
transcripts = payload.new.transcripts;
})
.subscribe();
});
onDestroy(() => {
subscription?.unsubscribe();
});
</script>
This worked but didn't feel idiomatic with Svelte 5's runes. I also tried using $derived
:
<script>
let transcripts = $state([]);
let latestPayload = $state(null);
$derived(() => {
if (latestPayload) {
transcripts = latestPayload.new.transcripts;
}
});
onMount(() => {
subscription = supabase
.from('demos')
.on('UPDATE', payload => {
latestPayload = payload;
})
.subscribe();
});
</script>
本文标签: How to properly handle realtime Supabase subscriptions using Svelte 5 runesStack Overflow
版权声明:本文标题:How to properly handle real-time Supabase subscriptions using Svelte 5 runes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741293385a2370676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论