Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

LiveQueryObserver.md

190 lines4.2 KB

1---
2id: LiveQueryObserver
3title: LiveQueryObserver
4---
5​
6Defined in: [packages/db/src/live-query-observer.ts:123](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L123)
7​
8**`Internal`**
9​
10Wraps a resolved live-query `Collection` (or `null` for a disabled query) with
11the shared lifecycle every framework adapter needs: start sync on first
12subscribe, subscribe to changes and status transitions, expose a stable
13snapshot for wholesale consumers, and deliver the raw change set for
14granular consumers.
15​
16Input resolution (query fn / config / collection / disabled) stays in the
17adapter — it is framework-reactive. The observer owns everything after the
18input is resolved to a concrete collection.
19​
20 Unstable contract for TanStack DB's official framework adapters —
21not a public extension point yet; may change in any release.
22​
23## Type Parameters
24​
25### T
26​
27`T` *extends* `object`
28​
29### TKey
30​
31`TKey` *extends* `string` \| `number`
32​
33## Properties
34​
35### dehydrate()
36​
37```ts
38dehydrate: () => DehydratedLiveQueryResult<T, TKey>;
39```
40​
41Defined in: [packages/db/src/live-query-observer.ts:147](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L147)
42​
43Capture the ordered query result without serializing its source collections.
44​
45#### Returns
46​
47[`DehydratedLiveQueryResult`](../type-aliases/DehydratedLiveQueryResult.md)\<`T`, `TKey`\>
48​
49***
50​
51### dispose()
52​
53```ts
54dispose: () => void;
55```
56​
57Defined in: [packages/db/src/live-query-observer.ts:149](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L149)
58​
59Idempotent teardown.
60​
61#### Returns
62​
63`void`
64​
65***
66​
67### getError()
68​
69```ts
70getError: () => unknown;
71```
72​
73Defined in: [packages/db/src/live-query-observer.ts:145](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L145)
74​
75The transport or preload error for this query, if it has not produced data.
76​
77#### Returns
78​
79`unknown`
80​
81***
82​
83### getServerSnapshot()
84​
85```ts
86getServerSnapshot: () => LiveQuerySnapshot<T, TKey>;
87```
88​
89Defined in: [packages/db/src/live-query-observer.ts:130](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L130)
90​
91Stable server snapshot used by useSyncExternalStore-style adapters.
92​
93#### Returns
94​
95[`LiveQuerySnapshot`](LiveQuerySnapshot.md)\<`T`, `TKey`\>
96​
97***
98​
99### getSnapshot()
100​
101```ts
102getSnapshot: () => LiveQuerySnapshot<T, TKey>;
103```
104​
105Defined in: [packages/db/src/live-query-observer.ts:128](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L128)
106​
107Stable per-revision snapshot for wholesale materialization.
108​
109#### Returns
110​
111[`LiveQuerySnapshot`](LiveQuerySnapshot.md)\<`T`, `TKey`\>
112​
113***
114​
115### isInitialRenderReady()
116​
117```ts
118isInitialRenderReady: () => boolean;
119```
120​
121Defined in: [packages/db/src/live-query-observer.ts:143](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L143)
122​
123Whether a persisted fallback has already passed the network-first gate.
124​
125#### Returns
126​
127`boolean`
128​
129***
130​
131### preload()
132​
133```ts
134preload: () => Promise<void>;
135```
136​
137Defined in: [packages/db/src/live-query-observer.ts:139](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L139)
138​
139Resolve once the collection has loaded its first data.
140​
141#### Returns
142​
143`Promise`\<`void`\>
144​
145***
146​
147### preloadForInitialRender()
148​
149```ts
150preloadForInitialRender: () => Promise<void>;
151```
152​
153Defined in: [packages/db/src/live-query-observer.ts:141](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L141)
154​
155Resolve at network readiness or a permitted persisted fallback.
156​
157#### Returns
158​
159`Promise`\<`void`\>
160​
161***
162​
163### subscribe()
164​
165```ts
166subscribe: (listener) => () => void;
167```
168​
169Defined in: [packages/db/src/live-query-observer.ts:137](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L137)
170​
171Subscribe to changes. The listener receives the change set (or `undefined`
172for the synthetic notify a ready collection emits on attach). Granular
173adapters apply the changes; wholesale adapters can ignore them and re-read
174`getSnapshot()`. Returns an unsubscribe function.
175​
176#### Parameters
177​
178##### listener
179​
180[`LiveQueryObserverListener`](../type-aliases/LiveQueryObserverListener.md)\<`T`, `TKey`\>
181​
182#### Returns
183​
184```ts
185(): void;
186```
187​
188##### Returns
189​
190`void`