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

LiveQueryObserver.md

158 lines3.5 KB

1---
2id: LiveQueryObserver
3title: LiveQueryObserver
4---
5​
6Defined in: [packages/db/src/live-query-observer.ts:72](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L72)
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:92](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L92)
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:94](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L94)
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:90](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L90)
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:79](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L79)
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:77](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L77)
106​
107Stable per-revision snapshot for wholesale materialization.
108​
109#### Returns
110​
111[`LiveQuerySnapshot`](LiveQuerySnapshot.md)\<`T`, `TKey`\>
112​
113***
114​
115### preload()
116​
117```ts
118preload: () => Promise<void>;
119```
120​
121Defined in: [packages/db/src/live-query-observer.ts:88](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L88)
122​
123Resolve once the collection has loaded its first data.
124​
125#### Returns
126​
127`Promise`\<`void`\>
128​
129***
130​
131### subscribe()
132​
133```ts
134subscribe: (listener) => () => void;
135```
136​
137Defined in: [packages/db/src/live-query-observer.ts:86](https://github.com/TanStack/db/blob/main/packages/db/src/live-query-observer.ts#L86)
138​
139Subscribe to changes. The listener receives the change set (or `undefined`
140for the synthetic notify a ready collection emits on attach). Granular
141adapters apply the changes; wholesale adapters can ignore them and re-read
142`getSnapshot()`. Returns an unsubscribe function.
143​
144#### Parameters
145​
146##### listener
147​
148[`LiveQueryObserverListener`](../type-aliases/LiveQueryObserverListener.md)\<`T`, `TKey`\>
149​
150#### Returns
151​
152```ts
153(): void;
154```
155​
156##### Returns
157​
158`void`