Open History to see the last commit
1/**
2 * Solid driver for the shared live-query conformance suite.
3 *
4 * Each mount runs inside a `createRoot` so `unmount` disposes via the captured
5 * dispose fn; the root stays alive between mount and reads so Solid's reactive
6 * getters stay current. Solid auto-tracks signals, so controllable inputs use a
7 * signal read inside the query fn (no deps array). Collection/config inputs are
8 * passed as accessors, per Solid's arity-based input detection.
9 *
10 * `knownGaps` is populated empirically from the run below.
11 */
12import {
13 coalesce,
14 count,
15 createCollection,
16 createLiveQueryCollection,
17 createOptimisticAction,
18 eq,
19 gt,
20 sum,
21} from '@tanstack/db'
22import { createRoot, createSignal } from 'solid-js'
23import {
24 mockSyncCollectionOptions,
25 mockSyncCollectionOptionsNoInitialState,
26} from '../../db/tests/utils'
27import { useLiveQuery } from '../src/useLiveQuery'
28import { runSuite } from '../../db/tests/conformance/suite'
29import type {
30 ConformanceResult,
31 ControllableHandle,
32 DeferredSourceHandle,
33 LiveQueryDriver,
34 LiveQueryHandle,
35 QueryBuild,
36 SourceHandle,
37} from '../../db/tests/conformance/contract'
38
39let sourceSeq = 0
40
41function writer<T extends { id: string }>(collection: any) {
42 return (type: `insert` | `update` | `delete`, value: T) => {
43 collection.utils.begin()
44 collection.utils.write({ type, value })
45 collection.utils.commit()
46 }
47}
48
49function makeSource<T extends { id: string }>(
50 initialData: ReadonlyArray<T>,
51): SourceHandle<T> {
52 const collection = createCollection(
53 mockSyncCollectionOptions<T>({
54 id: `conformance-solid-${sourceSeq++}`,
55 getKey: (r) => r.id,
56 initialData: [...initialData],
57 }),
58 )
59 const write = writer<T>(collection)
60 return {
61 collection,
62 insert: (row) => write(`insert`, row),
63 update: (row) => write(`update`, row),
64 remove: (row) => write(`delete`, row),
65 }
66}
67
68function makeDeferredSource<
69 T extends { id: string },
70>(): DeferredSourceHandle<T> {
71 const collection = createCollection(
72 mockSyncCollectionOptionsNoInitialState<T>({
73 id: `conformance-solid-${sourceSeq++}`,
74 getKey: (r) => r.id,
75 }),
76 )
77 collection.startSyncImmediate()
78 const write = writer<T>(collection)
79 return {
80 collection,
81 insert: (row) => write(`insert`, row),
82 update: (row) => write(`update`, row),
83 remove: (row) => write(`delete`, row),
84 emit: (rows) => {
85 collection.utils.begin()
86 rows.forEach((value) => collection.utils.write({ type: `insert`, value }))
87 collection.utils.commit()
88 },
89 markReady: () => collection.utils.markReady(),
90 }
91}
92
93function makePrecreated(build: QueryBuild, opts?: { startSync?: boolean }) {
94 const collection = createLiveQueryCollection({
95 query: build as any,
96 startSync: opts?.startSync ?? true,
97 })
98 return { collection }
99}
100
101function makeErrorSource() {
102 const collection = createCollection<{ id: string }>({
103 id: `conformance-solid-err-${sourceSeq++}`,
104 getKey: (r) => r.id,
105 startSync: false,
106 sync: {
107 sync: () => {
108 throw new Error(`conformance: sync failure`)
109 },
110 },
111 })
112 try {
113 collection.startSyncImmediate()
114 } catch {
115 // expected: engine catches the sync error and sets status to `error`
116 }
117 return { collection }
118}
119
120async function settle() {
121 await new Promise((resolve) => setTimeout(resolve, 10))
122}
123
124function makeHandle(
125 getResult: () => any,
126 dispose: () => void,
127): LiveQueryHandle {
128 return {
129 current(): ConformanceResult {
130 const result = getResult()
131 return {
132 data: result?.data,
133 status: result?.status ?? `idle`,
134 isReady: Boolean(result?.isReady),
135 isError: Boolean(result?.isError),
136 // solid-db exposes no `isEnabled`; derive it from status (status-derived).
137 isEnabled: result?.status !== `disabled`,
138 }
139 },
140 flush: settle,
141 async apply(fn: () => void) {
142 fn()
143 await settle()
144 },
145 unmount() {
146 dispose()
147 },
148 }
149}
150
151function inRoot(fn: () => any): { getResult: () => any; dispose: () => void } {
152 let result: any
153 let dispose!: () => void
154 createRoot((d) => {
155 dispose = d
156 result = fn()
157 })
158 return { getResult: () => result, dispose }
159}
160
161function mount(build: QueryBuild) {
162 const { getResult, dispose } = inRoot(() => useLiveQuery(build as any))
163 return makeHandle(getResult, dispose)
164}
165
166function mountCollection(collection: any) {
167 // Solid accepts a pre-created collection via an accessor.
168 const { getResult, dispose } = inRoot(() => useLiveQuery(() => collection))
169 return makeHandle(getResult, dispose)
170}
171
172function mountConfig(build: QueryBuild) {
173 // Solid accepts the config-object form via an accessor.
174 const { getResult, dispose } = inRoot(() =>
175 useLiveQuery(() => ({ query: build })),
176 )
177 return makeHandle(getResult, dispose)
178}
179
180function mountDisabled() {
181 // Disabled: an accessor returning null.
182 const { getResult, dispose } = inRoot(() => useLiveQuery(() => null))
183 return makeHandle(getResult, dispose)
184}
185
186function mountControllable<P>(
187 build: (q: any, param: P) => any,
188 initial: P,
189): ControllableHandle<P> {
190 const [param, setParam] = createSignal<P>(initial)
191 const { getResult, dispose } = inRoot(() =>
192 // Reading param() inside the query fn makes Solid recompute on change.
193 useLiveQuery((q: any) => build(q, param())),
194 )
195 const handle = makeHandle(getResult, dispose)
196 return {
197 ...handle,
198 async setParam(next: P) {
199 setParam(() => next)
200 await settle()
201 },
202 }
203}
204
205const solidDriver: LiveQueryDriver = {
206 name: `solid`,
207 ops: { eq, gt, count, sum, coalesce, createOptimisticAction },
208 makeSource,
209 makeDeferredSource,
210 makePrecreated,
211 makeErrorSource,
212 mount,
213 mountControllable,
214 mountCollection,
215 mountConfig,
216 mountDisabled,
217 // solid-db routes errors through its createResource/Suspense path: reading an
218 // errored query throws (CollectionStateError) for an <ErrorBoundary> to catch,
219 // rather than exposing a readable isError flag. That's a framework idiom, not a
220 // gap — the error-status scenario is parametrized to assert it via the boundary.
221 errorSurface: `throw`,
222 knownGaps: [],
223 features: { serverSnapshot: false, suspense: true },
224}
225
226runSuite(solidDriver)