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

LiveQueryObserver.md

160 lines3.6 KB

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