forked from TanStack/db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
234 lines (206 loc) · 6.79 KB
/
index.ts
File metadata and controls
234 lines (206 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import {
DestroyRef,
assertInInjectionContext,
computed,
effect,
inject,
signal,
} from '@angular/core'
import { BaseQueryBuilder, createLiveQueryCollection } from '@tanstack/db'
import type {
ChangeMessage,
Collection,
CollectionStatus,
Context,
GetResult,
InitialQueryBuilder,
LiveQueryCollectionConfig,
QueryBuilder,
} from '@tanstack/db'
import type { Signal } from '@angular/core'
/**
* The result of calling `injectLiveQuery`.
* Contains reactive signals for the query state and data.
*/
export interface InjectLiveQueryResult<
TResult extends object = any,
TKey extends string | number = string | number,
TUtils extends Record<string, any> = {},
> {
/** A signal containing the complete state map of results keyed by their ID */
state: Signal<Map<TKey, TResult>>
/** A signal containing the results as an array */
data: Signal<Array<TResult>>
/** A signal containing the underlying collection instance (null for disabled queries) */
collection: Signal<Collection<TResult, TKey, TUtils> | null>
/** A signal containing the current status of the collection */
status: Signal<CollectionStatus | `disabled`>
/** A signal indicating whether the collection is currently loading */
isLoading: Signal<boolean>
/** A signal indicating whether the collection is ready */
isReady: Signal<boolean>
/** A signal indicating whether the collection is idle */
isIdle: Signal<boolean>
/** A signal indicating whether the collection has an error */
isError: Signal<boolean>
/** A signal indicating whether the collection has been cleaned up */
isCleanedUp: Signal<boolean>
}
export function injectLiveQuery<
TContext extends Context,
TParams extends any,
>(options: {
params: () => TParams
query: (args: {
params: TParams
q: InitialQueryBuilder
}) => QueryBuilder<TContext>
}): InjectLiveQueryResult<GetResult<TContext>>
export function injectLiveQuery<
TContext extends Context,
TParams extends any,
>(options: {
params: () => TParams
query: (args: {
params: TParams
q: InitialQueryBuilder
}) => QueryBuilder<TContext> | undefined | null
}): InjectLiveQueryResult<GetResult<TContext>>
export function injectLiveQuery<TContext extends Context>(
queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,
): InjectLiveQueryResult<GetResult<TContext>>
export function injectLiveQuery<TContext extends Context>(
queryFn: (
q: InitialQueryBuilder,
) => QueryBuilder<TContext> | undefined | null,
): InjectLiveQueryResult<GetResult<TContext>>
export function injectLiveQuery<TContext extends Context>(
config: LiveQueryCollectionConfig<TContext>,
): InjectLiveQueryResult<GetResult<TContext>>
export function injectLiveQuery<
TResult extends object,
TKey extends string | number,
TUtils extends Record<string, any>,
>(
liveQueryCollection: Collection<TResult, TKey, TUtils>,
): InjectLiveQueryResult<TResult, TKey, TUtils>
export function injectLiveQuery(opts: any) {
assertInInjectionContext(injectLiveQuery)
const destroyRef = inject(DestroyRef)
const collection = computed(() => {
// Check if it's an existing collection
const isExistingCollection =
opts &&
typeof opts === `object` &&
typeof opts.subscribeChanges === `function` &&
typeof opts.startSyncImmediate === `function` &&
typeof opts.id === `string`
if (isExistingCollection) {
return opts
}
if (typeof opts === `function`) {
// Check if query function returns null/undefined (disabled query)
const queryBuilder = new BaseQueryBuilder() as InitialQueryBuilder
const result = opts(queryBuilder)
if (result === undefined || result === null) {
// Disabled query - return null
return null
}
return createLiveQueryCollection({
query: opts,
startSync: true,
gcTime: 0,
})
}
// Check if it's reactive query options
const isReactiveQueryOptions =
opts &&
typeof opts === `object` &&
typeof opts.query === `function` &&
typeof opts.params === `function`
if (isReactiveQueryOptions) {
const { params, query } = opts
const currentParams = params()
// Check if query function returns null/undefined (disabled query)
const queryBuilder = new BaseQueryBuilder() as InitialQueryBuilder
const result = query({ params: currentParams, q: queryBuilder })
if (result === undefined || result === null) {
// Disabled query - return null
return null
}
return createLiveQueryCollection({
query: (q) => query({ params: currentParams, q }),
startSync: true,
gcTime: 0,
})
}
// Handle LiveQueryCollectionConfig objects
if (opts && typeof opts === `object` && typeof opts.query === `function`) {
return createLiveQueryCollection(opts)
}
throw new Error(`Invalid options provided to injectLiveQuery`)
})
const state = signal(new Map<string | number, any>())
const data = signal<Array<any>>([])
const status = signal<CollectionStatus | `disabled`>(
collection() ? `idle` : `disabled`,
)
const syncDataFromCollection = (
currentCollection: Collection<any, any, any>,
) => {
const newState = new Map(currentCollection.entries())
const newData = Array.from(currentCollection.values())
state.set(newState)
data.set(newData)
status.set(currentCollection.status)
}
let unsub: (() => void) | null = null
const cleanup = () => {
unsub?.()
unsub = null
}
effect((onCleanup) => {
const currentCollection = collection()
// Handle null collection (disabled query)
if (!currentCollection) {
status.set(`disabled` as const)
state.set(new Map())
data.set([])
cleanup()
return
}
cleanup()
// Initialize immediately with current state
syncDataFromCollection(currentCollection)
// Start sync if idle
if (currentCollection.status === `idle`) {
currentCollection.startSyncImmediate()
// Update status after starting sync
status.set(currentCollection.status)
}
// Subscribe to changes
const subscription = currentCollection.subscribeChanges(
(_: Array<ChangeMessage<any>>) => {
syncDataFromCollection(currentCollection)
},
)
unsub = subscription.unsubscribe.bind(subscription)
// Handle ready state
currentCollection.onFirstReady(() => {
status.set(currentCollection.status)
})
onCleanup(cleanup)
})
destroyRef.onDestroy(cleanup)
return {
state,
data,
collection,
status,
isLoading: computed(() => status() === `loading`),
isReady: computed(() => status() === `ready` || status() === `disabled`),
isIdle: computed(() => status() === `idle`),
isError: computed(() => status() === `error`),
isCleanedUp: computed(() => status() === `cleaned-up`),
}
}