MCPcopy Index your code
hub / github.com/TanStack/db / injectLiveQuery

Function injectLiveQuery

packages/angular-db/src/index.ts:136–293  ·  view source on GitHub ↗
(opts: any)

Source from the content-addressed store, hash-verified

134 liveQueryCollection: Collection<TResult, TKey, TUtils> & SingleResult,
135): InjectLiveQueryResultWithSingleResultCollection<TResult, TKey, TUtils>
136export function injectLiveQuery(opts: any) {
137 assertInInjectionContext(injectLiveQuery)
138 const destroyRef = inject(DestroyRef)
139
140 const collection = computed(() => {
141 // Check if it's an existing collection
142 const isExistingCollection =
143 opts &&
144 typeof opts === `object` &&
145 typeof opts.subscribeChanges === `function` &&
146 typeof opts.startSyncImmediate === `function` &&
147 typeof opts.id === `string`
148
149 if (isExistingCollection) {
150 return opts
151 }
152
153 if (typeof opts === `function`) {
154 // Check if query function returns null/undefined (disabled query)
155 const queryBuilder = new BaseQueryBuilder() as InitialQueryBuilder
156 const result = opts(queryBuilder)
157
158 if (result === undefined || result === null) {
159 // Disabled query - return null
160 return null
161 }
162
163 return createLiveQueryCollection({
164 query: opts,
165 startSync: true,
166 gcTime: 0,
167 })
168 }
169
170 // Check if it's reactive query options
171 const isReactiveQueryOptions =
172 opts &&
173 typeof opts === `object` &&
174 typeof opts.query === `function` &&
175 typeof opts.params === `function`
176
177 if (isReactiveQueryOptions) {
178 const { params, query } = opts
179 const currentParams = params()
180
181 // Check if query function returns null/undefined (disabled query)
182 const queryBuilder = new BaseQueryBuilder() as InitialQueryBuilder
183 const result = query({ params: currentParams, q: queryBuilder })
184
185 if (result === undefined || result === null) {
186 // Disabled query - return null
187 return null
188 }
189
190 return createLiveQueryCollection({
191 query: () => result,
192 startSync: true,
193 gcTime: 0,

Callers 3

AppClass · 0.90

Calls 10

queryFunction · 0.85
collectionFunction · 0.85
startSyncImmediateMethod · 0.80
cleanupFunction · 0.70
syncDataFromCollectionFunction · 0.70
statusFunction · 0.50
setMethod · 0.45
subscribeChangesMethod · 0.45
onFirstReadyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…