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

Subscription.md

280 lines4.0 KB

1---
2id: Subscription
3title: Subscription
4---
5​
6# Interface: Subscription
7​
8Defined in: [packages/db/src/types.ts:254](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L254)
9​
10Public interface for a collection subscription
11Used by sync implementations to track subscription lifecycle
12​
13## Extends
14​
15- `EventEmitter`\<[`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)\>
16​
17## Properties
18​
19### status
20​
21```ts
22readonly status: SubscriptionStatus;
23```
24​
25Defined in: [packages/db/src/types.ts:256](https://github.com/TanStack/db/blob/main/packages/db/src/types.ts#L256)
26​
27Current status of the subscription
28​
29## Methods
30​
31### clearListeners()
32​
33```ts
34protected clearListeners(): void;
35```
36​
37Defined in: [packages/db/src/event-emitter.ts:115](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L115)
38​
39Clear all listeners
40​
41#### Returns
42​
43`void`
44​
45#### Inherited from
46​
47```ts
48EventEmitter.clearListeners
49```
50​
51***
52​
53### emitInner()
54​
55```ts
56protected emitInner<T>(event, eventPayload): void;
57```
58​
59Defined in: [packages/db/src/event-emitter.ts:96](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L96)
60​
61**`Internal`**
62​
63Emit an event to all listeners
64​
65#### Type Parameters
66​
67##### T
68​
69`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
70​
71#### Parameters
72​
73##### event
74​
75`T`
76​
77Event name to emit
78​
79##### eventPayload
80​
81[`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)\[`T`\]
82​
83Event payload
84 For use by subclasses - subclasses should wrap this with a public emit if needed
85​
86#### Returns
87​
88`void`
89​
90#### Inherited from
91​
92```ts
93EventEmitter.emitInner
94```
95​
96***
97​
98### off()
99​
100```ts
101off<T>(event, callback): void;
102```
103​
104Defined in: [packages/db/src/event-emitter.ts:53](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L53)
105​
106Unsubscribe from an event
107​
108#### Type Parameters
109​
110##### T
111​
112`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
113​
114#### Parameters
115​
116##### event
117​
118`T`
119​
120Event name to stop listening for
121​
122##### callback
123​
124(`event`) => `void`
125​
126Function to remove
127​
128#### Returns
129​
130`void`
131​
132#### Inherited from
133​
134```ts
135EventEmitter.off
136```
137​
138***
139​
140### on()
141​
142```ts
143on<T>(event, callback): () => void;
144```
145​
146Defined in: [packages/db/src/event-emitter.ts:17](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L17)
147​
148Subscribe to an event
149​
150#### Type Parameters
151​
152##### T
153​
154`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
155​
156#### Parameters
157​
158##### event
159​
160`T`
161​
162Event name to listen for
163​
164##### callback
165​
166(`event`) => `void`
167​
168Function to call when event is emitted
169​
170#### Returns
171​
172Unsubscribe function
173​
174```ts
175(): void;
176```
177​
178##### Returns
179​
180`void`
181​
182#### Inherited from
183​
184```ts
185EventEmitter.on
186```
187​
188***
189​
190### once()
191​
192```ts
193once<T>(event, callback): () => void;
194```
195​
196Defined in: [packages/db/src/event-emitter.ts:37](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L37)
197​
198Subscribe to an event once (automatically unsubscribes after first emission)
199​
200#### Type Parameters
201​
202##### T
203​
204`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
205​
206#### Parameters
207​
208##### event
209​
210`T`
211​
212Event name to listen for
213​
214##### callback
215​
216(`event`) => `void`
217​
218Function to call when event is emitted
219​
220#### Returns
221​
222Unsubscribe function
223​
224```ts
225(): void;
226```
227​
228##### Returns
229​
230`void`
231​
232#### Inherited from
233​
234```ts
235EventEmitter.once
236```
237​
238***
239​
240### waitFor()
241​
242```ts
243waitFor<T>(event, timeout?): Promise<SubscriptionEvents[T]>;
244```
245​
246Defined in: [packages/db/src/event-emitter.ts:66](https://github.com/TanStack/db/blob/main/packages/db/src/event-emitter.ts#L66)
247​
248Wait for an event to be emitted
249​
250#### Type Parameters
251​
252##### T
253​
254`T` *extends* keyof [`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)
255​
256#### Parameters
257​
258##### event
259​
260`T`
261​
262Event name to wait for
263​
264##### timeout?
265​
266`number`
267​
268Optional timeout in milliseconds
269​
270#### Returns
271​
272`Promise`\<[`SubscriptionEvents`](../type-aliases/SubscriptionEvents.md)\[`T`\]\>
273​
274Promise that resolves with the event payload
275​
276#### Inherited from
277​
278```ts
279EventEmitter.waitFor
280```