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

LiveQueryObserver.md

164 lines3.6 KB

1---
2id: LiveQueryObserver
3title: LiveQueryObserver
4---
5​
6Defined in: [packages/db/src/live-query-observer.ts:93](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L93)
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:113](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L113)
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:115](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L115)
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:111](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L111)
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: (preloadRequested?) => LiveQuerySnapshot<T, TKey>;
87```
88​
89Defined in: [packages/db/src/live-query-observer.ts:100](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L100)
90​
91Server snapshot. preloadRequested marks an explicit streaming Suspense load.
92​
93#### Parameters
94​
95##### preloadRequested?
96​
97`boolean`
98​
99#### Returns
100​
101[`LiveQuerySnapshot`](LiveQuerySnapshot.md)\<`T`, `TKey`\>
102​
103***
104​
105### getSnapshot()
106​
107```ts
108getSnapshot: () => LiveQuerySnapshot<T, TKey>;
109```
110​
111Defined in: [packages/db/src/live-query-observer.ts:98](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L98)
112​
113Stable per-revision snapshot for wholesale materialization.
114​
115#### Returns
116​
117[`LiveQuerySnapshot`](LiveQuerySnapshot.md)\<`T`, `TKey`\>
118​
119***
120​
121### preload()
122​
123```ts
124preload: () => Promise<void>;
125```
126​
127Defined in: [packages/db/src/live-query-observer.ts:109](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L109)
128​
129Resolve once the collection has loaded its first data.
130​
131#### Returns
132​
133`Promise`\<`void`\>
134​
135***
136​
137### subscribe()
138​
139```ts
140subscribe: (listener) => () => void;
141```
142​
143Defined in: [packages/db/src/live-query-observer.ts:107](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L107)
144​
145Subscribe to changes. The listener receives the change set (or `undefined`
146for the synthetic notify a ready collection emits on attach). Granular
147adapters apply the changes; wholesale adapters can ignore them and re-read
148`getSnapshot()`. Returns an unsubscribe function.
149​
150#### Parameters
151​
152##### listener
153​
154[`LiveQueryObserverListener`](../type-aliases/LiveQueryObserverListener.md)\<`T`, `TKey`\>
155​
156#### Returns
157​
158```ts
159(): void;
160```
161​
162##### Returns
163​
164`void`