@tanstack/lit-query 0.2.26 → 0.2.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,6 @@ let defaultClient;
21
21
  *
22
22
  * `QueryClientProvider` calls this automatically while it is connected. Prefer
23
23
  * passing an explicit client or rendering under a provider when possible.
24
- *
25
24
  * @param client - The query client to register as the current default.
26
25
  */
27
26
  function registerDefaultQueryClient(client) {
@@ -33,7 +32,6 @@ function registerDefaultQueryClient(client) {
33
32
  * `registerDefaultQueryClient`.
34
33
  *
35
34
  * `QueryClientProvider` calls this automatically when it disconnects.
36
- *
37
35
  * @param client - The query client registration to release.
38
36
  */
39
37
  function unregisterDefaultQueryClient(client) {
@@ -50,7 +48,6 @@ function unregisterDefaultQueryClient(client) {
50
48
  /**
51
49
  * Returns the registered default `QueryClient`, if exactly one default client is
52
50
  * available.
53
- *
54
51
  * @returns The default query client, or `undefined` when there is no registered
55
52
  * client or more than one registered client.
56
53
  */
@@ -58,9 +55,18 @@ function getDefaultQueryClient() {
58
55
  if (registeredClients.size > 1) return;
59
56
  return defaultClient;
60
57
  }
58
+ /**
59
+ * Creates the error thrown when no `QueryClient` is available.
60
+ * @returns A new `Error` explaining that no `QueryClient` is available.
61
+ */
61
62
  function createMissingQueryClientError() {
62
63
  return /* @__PURE__ */ new Error(missingQueryClientMessage);
63
64
  }
65
+ /**
66
+ * Creates the error thrown when multiple `QueryClient`s are mounted and the default one would be
67
+ * ambiguous.
68
+ * @returns A new `Error` explaining that the default `QueryClient` is ambiguous.
69
+ */
64
70
  function createAmbiguousQueryClientError() {
65
71
  return /* @__PURE__ */ new Error(ambiguousQueryClientMessage);
66
72
  }
@@ -71,8 +77,9 @@ function createAmbiguousQueryClientError() {
71
77
  * This helper is useful outside a Lit reactive controller when a single
72
78
  * provider is mounted. It throws if no client is registered or if multiple
73
79
  * clients are mounted and the default would be ambiguous.
74
- *
75
80
  * @returns The single registered query client.
81
+ * @throws {Error} If no `QueryClient` is registered, or if multiple are
82
+ * mounted and the default is ambiguous.
76
83
  */
77
84
  function useQueryClient() {
78
85
  const client = getDefaultQueryClient();
@@ -82,7 +89,6 @@ function useQueryClient() {
82
89
  }
83
90
  /**
84
91
  * Resolves an explicit `QueryClient` or falls back to `useQueryClient`.
85
- *
86
92
  * @param explicit - Optional client supplied by the caller.
87
93
  * @returns The explicit client when provided, otherwise the current default
88
94
  * client.
@@ -104,7 +110,6 @@ function resolveQueryClient(explicit) {
104
110
  * This class is not registered as a custom element by the package. Applications
105
111
  * must register either a subclass or the class itself with
106
112
  * `customElements.define`.
107
- *
108
113
  * @example
109
114
  * ```ts
110
115
  * import { html, LitElement } from 'lit'
@@ -127,7 +132,6 @@ function resolveQueryClient(explicit) {
127
132
  * }
128
133
  * }
129
134
  * ```
130
- *
131
135
  * @example
132
136
  * ```ts
133
137
  * import { html } from 'lit'
@@ -152,19 +156,32 @@ var QueryClientProvider = class extends LitElement {
152
156
  super();
153
157
  this.contextProvider = new ContextProvider(this, { context: queryClientContext });
154
158
  }
155
- /** @internal */
159
+ /**
160
+ * Provides the client to descendants and mounts it when the element is
161
+ * connected.
162
+ * @internal
163
+ */
156
164
  connectedCallback() {
157
165
  super.connectedCallback();
158
166
  const client = this.requireClient();
159
167
  this.contextProvider.setValue(client);
160
168
  this.mountClient(client);
161
169
  }
162
- /** @internal */
170
+ /**
171
+ * Unmounts the client when the element is disconnected.
172
+ * @internal
173
+ */
163
174
  disconnectedCallback() {
164
175
  this.unmountClient(this.mountedClient);
165
176
  super.disconnectedCallback();
166
177
  }
167
- /** @internal */
178
+ /**
179
+ * Swaps the provided and mounted client when `client` changes.
180
+ * @internal
181
+ * @param changedProperties - The properties that changed. When `client` changes, the previous
182
+ * client is unmounted and the new one is provided and mounted.
183
+ * @throws {Error} If `client` is unset while the provider is connected.
184
+ */
168
185
  willUpdate(changedProperties) {
169
186
  if (!changedProperties.has("client")) return;
170
187
  const nextClient = this.client;
@@ -181,7 +198,11 @@ var QueryClientProvider = class extends LitElement {
181
198
  this.contextProvider.setValue(nextClient);
182
199
  if (this.isConnected) this.mountClient(nextClient);
183
200
  }
184
- /** @internal */
201
+ /**
202
+ * Renders the provider's children.
203
+ * @internal
204
+ * @returns A `<slot>` that renders the provider's children.
205
+ */
185
206
  render() {
186
207
  return html`<slot></slot>`;
187
208
  }
@@ -205,9 +226,20 @@ var QueryClientProvider = class extends LitElement {
205
226
  };
206
227
  //#endregion
207
228
  //#region src/accessor.ts
229
+ /**
230
+ * Reads an {@link Accessor}: calls it if it is a function, otherwise returns it as is.
231
+ * @param value - The value, or a getter that returns it.
232
+ * @returns The value.
233
+ */
208
234
  function readAccessor(value) {
209
235
  return typeof value === "function" ? value() : value;
210
236
  }
237
+ /**
238
+ * Creates a {@link ValueAccessor} that reads its value from `getter`, both when called and through
239
+ * its `current` property.
240
+ * @param getter - Returns the latest value.
241
+ * @returns A function that returns the latest value, which is also available on `current`.
242
+ */
211
243
  function createValueAccessor(getter) {
212
244
  const accessor = (() => getter());
213
245
  Object.defineProperty(accessor, "current", {
@@ -218,6 +250,12 @@ function createValueAccessor(getter) {
218
250
  }
219
251
  //#endregion
220
252
  //#region src/controllers/BaseController.ts
253
+ /**
254
+ * Base class of the query controllers. It resolves the `QueryClient`, either
255
+ * passed explicitly or provided through context by a `QueryClientProvider`,
256
+ * follows the host's lifecycle, and requests a host update when the result
257
+ * changes.
258
+ */
221
259
  var BaseController = class {
222
260
  constructor(host, initialResult, queryClient) {
223
261
  this.host = host;
@@ -232,6 +270,11 @@ var BaseController = class {
232
270
  this.queryClientResolutionState = queryClient ? "bound" : "pre-connect";
233
271
  host.addController(this);
234
272
  }
273
+ /**
274
+ * Starts resolving the `QueryClient` from context, unless one was passed
275
+ * explicitly, and calls `onConnected` in a microtask so subclass fields are
276
+ * initialized first.
277
+ */
235
278
  hostConnected() {
236
279
  if (this.connected || this.destroyed) return;
237
280
  this.connected = true;
@@ -246,6 +289,10 @@ var BaseController = class {
246
289
  });
247
290
  if (contextResolutionAttempt !== void 0) this.queueContextResolution(contextResolutionAttempt);
248
291
  }
292
+ /**
293
+ * Releases the context `QueryClient`, unless one was passed explicitly, and
294
+ * calls `onDisconnected`.
295
+ */
249
296
  hostDisconnected() {
250
297
  if (!this.connected) return;
251
298
  this.connected = false;
@@ -256,6 +303,10 @@ var BaseController = class {
256
303
  }
257
304
  this.onDisconnected();
258
305
  }
306
+ /**
307
+ * Calls `onHostUpdate` before the host renders. Results set during this call
308
+ * don't request another update.
309
+ */
259
310
  hostUpdate() {
260
311
  if (this.destroyed) return;
261
312
  this.isHostUpdating = true;
@@ -265,6 +316,11 @@ var BaseController = class {
265
316
  this.isHostUpdating = false;
266
317
  }
267
318
  }
319
+ /**
320
+ * Permanently disconnects the controller and removes it from its host, if
321
+ * the host supports `removeController`. The controller ignores later
322
+ * lifecycle callbacks.
323
+ */
268
324
  destroy() {
269
325
  if (this.destroyed) return;
270
326
  this.destroyed = true;
@@ -275,26 +331,53 @@ var BaseController = class {
275
331
  this.onDisconnected();
276
332
  if ("removeController" in this.host) this.host.removeController(this);
277
333
  }
334
+ /**
335
+ * Returns the `QueryClient` without throwing when none is available.
336
+ * @returns The explicitly passed client, otherwise the client from context,
337
+ * or `undefined` if neither is available.
338
+ */
278
339
  tryGetQueryClient() {
279
340
  return this.explicitClient ?? this.contextClient;
280
341
  }
342
+ /**
343
+ * Returns the `QueryClient`, throwing when none is available.
344
+ * @returns The explicitly passed client, otherwise the client from context.
345
+ * @throws {Error} If no `QueryClient` is available.
346
+ */
281
347
  getQueryClient() {
282
348
  const client = this.tryGetQueryClient();
283
349
  if (!client) throw createMissingQueryClientError();
284
350
  return client;
285
351
  }
352
+ /**
353
+ * Stores a new result and requests a host update, unless it is the same
354
+ * result or the host is already updating.
355
+ * @param next - The new result.
356
+ */
286
357
  setResult(next) {
287
358
  if (Object.is(this.result, next)) return;
288
359
  this.result = next;
289
360
  if (!this.isHostUpdating) this.queueUpdate();
290
361
  }
362
+ /**
363
+ * The latest result of the controller.
364
+ * @returns The latest result.
365
+ * @throws {Error} If no `QueryClient` could be resolved from context.
366
+ */
291
367
  get current() {
292
368
  if (this.queryClientResolutionState === "missing") throw createMissingQueryClientError();
293
369
  return this.result;
294
370
  }
371
+ /**
372
+ * Whether the controller's host is connected.
373
+ * @returns `true` while the host is connected.
374
+ */
295
375
  get connectedState() {
296
376
  return this.connected;
297
377
  }
378
+ /**
379
+ * Requests a host update in a microtask, batching multiple calls into one.
380
+ */
298
381
  queueUpdate() {
299
382
  if (this.updateQueued) return;
300
383
  this.updateQueued = true;
@@ -363,14 +446,29 @@ var BaseController = class {
363
446
  };
364
447
  //#endregion
365
448
  //#region src/queryObserverResultTracker.ts
449
+ /**
450
+ * Wraps observer results with property tracking when `notifyOnChangeProps` is
451
+ * not set, so the observer only notifies about the properties the host reads.
452
+ */
366
453
  var QueryObserverResultTracker = class {
367
454
  constructor() {
368
455
  this.usesTracking = false;
369
456
  }
457
+ /**
458
+ * Forgets the last result, so the next `update` returns a result again.
459
+ */
370
460
  reset() {
371
461
  this.result = void 0;
372
462
  this.usesTracking = false;
373
463
  }
464
+ /**
465
+ * Returns the result to expose, tracked through the observer when it uses
466
+ * property tracking.
467
+ * @param observer - The observer the result comes from, if any.
468
+ * @param result - The latest result of the observer.
469
+ * @returns The result to expose, or `undefined` if neither the result nor
470
+ * the tracking mode changed since the last call.
471
+ */
374
472
  update(observer, result) {
375
473
  const usesTracking = !!observer && !observer.options.notifyOnChangeProps;
376
474
  if (Object.is(this.result, result) && this.usesTracking === usesTracking) return;
@@ -381,6 +479,11 @@ var QueryObserverResultTracker = class {
381
479
  };
382
480
  //#endregion
383
481
  //#region src/createInfiniteQueryController.ts
482
+ /**
483
+ * Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
484
+ * that reject with the missing client error.
485
+ * @returns A new result object in that state.
486
+ */
384
487
  function createPendingInfiniteQueryResult() {
385
488
  return {
386
489
  data: void 0,
@@ -535,7 +638,6 @@ var InfiniteQueryController = class extends BaseController {
535
638
  *
536
639
  * If `queryClient` is omitted, the controller resolves the client from the
537
640
  * nearest connected `QueryClientProvider`.
538
- *
539
641
  * @param host - The Lit reactive controller host that owns the infinite query
540
642
  * subscription.
541
643
  * @param options - Infinite query observer options, or a getter that returns
@@ -544,7 +646,6 @@ var InfiniteQueryController = class extends BaseController {
544
646
  * controllers that should not resolve a client from Lit context.
545
647
  * @returns An accessor for the latest infinite query result with page helper
546
648
  * methods.
547
- *
548
649
  * @example
549
650
  * ```ts
550
651
  * import { LitElement, html } from 'lit'
@@ -581,6 +682,11 @@ function createInfiniteQueryController(host, options, queryClient) {
581
682
  }
582
683
  //#endregion
583
684
  //#region src/createMutationController.ts
685
+ /**
686
+ * Returns the result used while no `QueryClient` is available: `'idle'`, with a `mutate` that
687
+ * rejects with the missing client error.
688
+ * @returns A new result object in that state.
689
+ */
584
690
  function createIdleMutationResult() {
585
691
  return {
586
692
  context: void 0,
@@ -692,7 +798,6 @@ var MutationController = class extends BaseController {
692
798
  *
693
799
  * If `queryClient` is omitted, the controller resolves the client from the
694
800
  * nearest connected `QueryClientProvider`.
695
- *
696
801
  * @param host - The Lit reactive controller host that owns the mutation
697
802
  * subscription.
698
803
  * @param options - Mutation observer options, or a getter that returns options.
@@ -700,7 +805,6 @@ var MutationController = class extends BaseController {
700
805
  * controllers that should not resolve a client from Lit context.
701
806
  * @returns An accessor for the latest mutation result with mutation helper
702
807
  * methods.
703
- *
704
808
  * @example
705
809
  * ```ts
706
810
  * import { LitElement, html } from 'lit'
@@ -735,6 +839,11 @@ function createMutationController(host, options, queryClient) {
735
839
  }
736
840
  //#endregion
737
841
  //#region src/createQueriesController.ts
842
+ /**
843
+ * Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
844
+ * that reject with the missing client error.
845
+ * @returns A new result object in that state.
846
+ */
738
847
  function createPendingQueryObserverResult() {
739
848
  return {
740
849
  data: void 0,
@@ -764,6 +873,12 @@ function createPendingQueryObserverResult() {
764
873
  refetch: (() => Promise.reject(createMissingQueryClientError()))
765
874
  };
766
875
  }
876
+ /**
877
+ * Returns the result of a query used while no `QueryClient` is available: a `'success'` result with
878
+ * the query's `initialData` (after `select`), or the pending result if there is no `initialData`.
879
+ * @param query - The query options.
880
+ * @returns The `'success'` result with `initialData`, or a pending one.
881
+ */
767
882
  function createPlaceholderQueryObserverResult(query) {
768
883
  const initialData = typeof query.initialData === "function" ? query.initialData() : query.initialData;
769
884
  if (initialData === void 0) return createPendingQueryObserverResult();
@@ -780,6 +895,12 @@ function createPlaceholderQueryObserverResult(query) {
780
895
  status: "success"
781
896
  };
782
897
  }
898
+ /**
899
+ * Reads the controller's options and defaults each query with the client.
900
+ * @param optionsAccessor - The options, or a getter that returns them.
901
+ * @param client - The client used to default the query options.
902
+ * @returns The defaulted query options and the `combine` function.
903
+ */
783
904
  function resolveQueriesOptions(optionsAccessor, client) {
784
905
  const resolvedOptions = readAccessor(optionsAccessor);
785
906
  const resolvedQueries = readAccessor(resolvedOptions.queries);
@@ -972,7 +1093,6 @@ var QueriesController = class QueriesController extends BaseController {
972
1093
  *
973
1094
  * If `queryClient` is omitted, the controller resolves the client from the
974
1095
  * nearest connected `QueryClientProvider`.
975
- *
976
1096
  * @param host - The Lit reactive controller host that owns the queries
977
1097
  * subscription.
978
1098
  * @param options - Queries controller options, or a getter that returns options.
@@ -980,7 +1100,6 @@ var QueriesController = class QueriesController extends BaseController {
980
1100
  * controllers that should not resolve a client from Lit context.
981
1101
  * @returns An accessor for the latest query results, or the value returned by
982
1102
  * `combine`.
983
- *
984
1103
  * @example
985
1104
  * ```ts
986
1105
  * import { LitElement, html } from 'lit'
@@ -1012,6 +1131,11 @@ function createQueriesController(host, options, queryClient) {
1012
1131
  }
1013
1132
  //#endregion
1014
1133
  //#region src/createQueryController.ts
1134
+ /**
1135
+ * Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
1136
+ * that reject with the missing client error.
1137
+ * @returns A new result object in that state.
1138
+ */
1015
1139
  function createPendingQueryResult() {
1016
1140
  return {
1017
1141
  data: void 0,
@@ -1160,14 +1284,12 @@ var QueryController = class extends BaseController {
1160
1284
  *
1161
1285
  * If `queryClient` is omitted, the controller resolves the client from the
1162
1286
  * nearest connected `QueryClientProvider`.
1163
- *
1164
1287
  * @param host - The Lit reactive controller host that owns the query
1165
1288
  * subscription.
1166
1289
  * @param options - Query observer options, or a getter that returns options.
1167
1290
  * @param queryClient - Optional explicit query client. Provide this for
1168
1291
  * controllers that should not resolve a client from Lit context.
1169
1292
  * @returns An accessor for the latest query result with query helper methods.
1170
- *
1171
1293
  * @example
1172
1294
  * ```ts
1173
1295
  * import { LitElement, html } from 'lit'
@@ -1268,14 +1390,12 @@ var IsFetchingController = class extends BaseController {
1268
1390
  * When `filters` is a function, it is re-read during host updates so the count
1269
1391
  * can follow reactive host state. If `queryClient` is omitted, the controller
1270
1392
  * resolves the client from the nearest connected `QueryClientProvider`.
1271
- *
1272
1393
  * @param host - The Lit reactive controller host that owns the cache
1273
1394
  * subscription.
1274
1395
  * @param filters - Query filters, or a getter that returns query filters.
1275
1396
  * @param queryClient - Optional explicit query client. Provide this for
1276
1397
  * controllers that should not resolve a client from Lit context.
1277
1398
  * @returns An accessor for the current number of matching fetching queries.
1278
- *
1279
1399
  * @example
1280
1400
  * ```ts
1281
1401
  * import { LitElement, html } from 'lit'
@@ -1366,14 +1486,12 @@ var IsMutatingController = class extends BaseController {
1366
1486
  * When `filters` is a function, it is re-read during host updates so the count
1367
1487
  * can follow reactive host state. If `queryClient` is omitted, the controller
1368
1488
  * resolves the client from the nearest connected `QueryClientProvider`.
1369
- *
1370
1489
  * @param host - The Lit reactive controller host that owns the cache
1371
1490
  * subscription.
1372
1491
  * @param filters - Mutation filters, or a getter that returns mutation filters.
1373
1492
  * @param queryClient - Optional explicit query client. Provide this for
1374
1493
  * controllers that should not resolve a client from Lit context.
1375
1494
  * @returns An accessor for the current number of matching pending mutations.
1376
- *
1377
1495
  * @example
1378
1496
  * ```ts
1379
1497
  * import { LitElement, html } from 'lit'
@@ -1476,14 +1594,12 @@ var MutationStateController = class extends BaseController {
1476
1594
  * the selection can follow reactive host state. If `queryClient` is omitted,
1477
1595
  * the controller resolves the client from the nearest connected
1478
1596
  * `QueryClientProvider`.
1479
- *
1480
1597
  * @param host - The Lit reactive controller host that owns the mutation cache
1481
1598
  * subscription.
1482
1599
  * @param options - Mutation state filters and optional selector.
1483
1600
  * @param queryClient - Optional explicit query client. Provide this for
1484
1601
  * controllers that should not resolve a client from Lit context.
1485
1602
  * @returns An accessor for the selected mutation state array.
1486
- *
1487
1603
  * @example
1488
1604
  * ```ts
1489
1605
  * import { LitElement, html } from 'lit'
@@ -1519,10 +1635,8 @@ function infiniteQueryOptions(options) {
1519
1635
  //#region src/mutationOptions.ts
1520
1636
  /**
1521
1637
  * Preserves and types mutation options for reuse across Lit Query APIs.
1522
- *
1523
1638
  * @param options - Mutation options to preserve.
1524
1639
  * @returns The same options object.
1525
- *
1526
1640
  * @example
1527
1641
  * ```ts
1528
1642
  * import { mutationOptions } from '@tanstack/lit-query'