Open History to see the last commit
1<script lang="ts">
2 import { useLiveQuery } from '../src/useLiveQuery.svelte.js'
3 import type { CollectionOptions } from '@tanstack/db'
4 import type { SsrHookFlags } from './ssr-test-utils'
5
6 type Person = {
7 id: string
8 name: string
9 sourcePayload: string
10 }
11
12 let {
13 descriptor,
14 onInitial,
15 onSnapshot,
16 }: {
17 onInitial?: (
18 rows: Array<{ id: string; name: string }>,
19 status: string,
20 flags: SsrHookFlags,
21 ) => void
22 onSnapshot?: (
23 rows: Array<{ id: string; name: string }>,
24 status: string,
25 flags: SsrHookFlags,
26 ) => void
27 descriptor: CollectionOptions<Person, string>
28 } = $props()
29
30 const people = useLiveQuery({
31 query: (q) =>
32 q.from({ people: descriptor }).select(({ people }) => ({
33 id: people.id,
34 name: people.name,
35 })),
36 })
37 function reportInitial() {
38 onInitial?.(
39 people.data.map(({ id, name }) => ({ id, name })),
40 people.status,
41 {
42 isReady: people.isReady,
43 isLoadingError: people.isLoadingError,
44 isCrashed: people.isCrashed,
45 isLoading: people.isLoading,
46 isIdle: people.isIdle,
47 },
48 )
49 }
50 reportInitial()
51 $effect.pre(() => {
52 onSnapshot?.(
53 people.data.map(({ id, name }) => ({ id, name })),
54 people.status,
55 {
56 isReady: people.isReady,
57 isLoadingError: people.isLoadingError,
58 isCrashed: people.isCrashed,
59 isLoading: people.isLoading,
60 isIdle: people.isIdle,
61 },
62 )
63 })
64</script>
65
66<output data-status={people.status}>
67 {#each people.data as person (person.id)}
68 <span>{person.name}</span>
69 {/each}
70</output>