Open History to see the last commit
1/**
2 * Initial-query readiness exposure witness.
3 *
4 * Law (packages/db/src/query/live/ARCHITECTURE.md, law 10): a live query is
5 * initially ready when its requests to an on-demand source settle. That
6 * source's own Collection readiness does not gate it. This adapter must report
7 * readiness at that point, with the rows present, and stay ready after the
8 * source becomes ready or changes. Model D in
9 * `packages/db/tests/query/includes-temporal-oracle.test.ts` owns the law.
10 */
11import { describe, expect, it } from 'vitest'
12import { render, renderHook, waitFor } from '@solidjs/testing-library'
13import { createCollection, eq } from '@tanstack/db'
14import { Suspense } from 'solid-js'
15import { useLiveQuery } from '../src/useLiveQuery'
16
17const flush = () => new Promise((resolve) => setTimeout(resolve, 0))
18
19type Post = { id: string; title: string }
20
21/**
22 * An on-demand source whose subset load waits for the test to settle it.
23 * Rows written before `settle` are visible but not yet settled evidence. The
24 * source stays `loading` until `markReady`.
25 */
26function createLaggingSource(id: string) {
27 // Every reader's request waits until `settle`.
28 const pendingLoads: Array<() => void> = []
29 let controls:
30 | {
31 write: (type: `insert` | `update`, row: Post) => void
32 markReady: () => void
33 }
34 | undefined
35 const collection = createCollection<Post>({
36 id,
37 getKey: (post) => post.id,
38 syncMode: `on-demand`,
39 sync: {
40 sync: ({ begin, write, commit, markReady }) => {
41 controls = {
42 write: (type, row) => {
43 begin()
44 write({ type, value: row })
45 commit()
46 },
47 markReady,
48 }
49 return {
50 loadSubset: () =>
51 new Promise<void>((resolve) => {
52 pendingLoads.push(resolve)
53 }),
54 }
55 },
56 },
57 })
58 return {
59 collection,
60 loadStarted: () => pendingLoads.length > 0,
61 deliver: (row: Post) => controls!.write(`insert`, row),
62 change: (row: Post) => controls!.write(`update`, row),
63 settle: () => pendingLoads.splice(0).forEach((resolve) => resolve()),
64 markReady: () => controls!.markReady(),
65 }
66}
67
68describe(`useLiveQuery initial readiness over a lagging on-demand source`, () => {
69 it(`releases isReady and Suspense when the subset settles and stays ready`, async () => {
70 const source = createLaggingSource(`solid-exposure-lagging`)
71 const statusView = renderHook(() =>
72 useLiveQuery((q) =>
73 q
74 .from({ post: source.collection })
75 .where(({ post }) => eq(post.id, `p1`)),
76 ),
77 )
78 let cleanupSuspended: (() => Promise<void>) | undefined
79 function SuspendedTitle() {
80 const query = useLiveQuery((q) =>
81 q
82 .from({ post: source.collection })
83 .where(({ post }) => eq(post.id, `p1`)),
84 )
85 cleanupSuspended = () => query.collection.cleanup()
86 return <div data-testid="title">{query()[0]?.title}</div>
87 }
88 const view = render(() => (
89 <Suspense fallback={<div data-testid="waiting">Waiting</div>}>
90 <SuspendedTitle />
91 </Suspense>
92 ))
93 try {
94 await waitFor(() => expect(source.loadStarted()).toBe(true))
95 source.deliver({ id: `p1`, title: `one` })
96 await flush()
97 expect(statusView.result.isReady).toBe(false)
98 expect(view.getByTestId(`waiting`)).toBeTruthy()
99
100 source.settle()
101 await flush()
102 expect(source.collection.status).toBe(`loading`)
103 expect(statusView.result.isReady).toBe(true)
104 expect(statusView.result().map((post) => post.id)).toEqual([`p1`])
105 const title = await view.findByTestId(`title`)
106 expect(title.textContent).toBe(`one`)
107
108 source.markReady()
109 source.change({ id: `p1`, title: `changed` })
110 await flush()
111 expect(statusView.result.isReady).toBe(true)
112 expect(view.getByTestId(`title`).textContent).toBe(`changed`)
113 expect(view.queryByTestId(`waiting`)).toBeNull()
114 } finally {
115 view.unmount()
116 statusView.cleanup()
117 await statusView.result.collection.cleanup()
118 await cleanupSuspended?.()
119 await source.collection.cleanup()
120 }
121 })
122})