@tanstack/lit-query 0.2.25 → 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'
@@ -149,19 +153,32 @@ var QueryClientProvider = class extends lit.LitElement {
149
153
  super();
150
154
  this.contextProvider = new _lit_context.ContextProvider(this, { context: queryClientContext });
151
155
  }
152
- /** @internal */
156
+ /**
157
+ * Provides the client to descendants and mounts it when the element is
158
+ * connected.
159
+ * @internal
160
+ */
153
161
  connectedCallback() {
154
162
  super.connectedCallback();
155
163
  const client = this.requireClient();
156
164
  this.contextProvider.setValue(client);
157
165
  this.mountClient(client);
158
166
  }
159
- /** @internal */
167
+ /**
168
+ * Unmounts the client when the element is disconnected.
169
+ * @internal
170
+ */
160
171
  disconnectedCallback() {
161
172
  this.unmountClient(this.mountedClient);
162
173
  super.disconnectedCallback();
163
174
  }
164
- /** @internal */
175
+ /**
176
+ * Swaps the provided and mounted client when `client` changes.
177
+ * @internal
178
+ * @param changedProperties - The properties that changed. When `client` changes, the previous
179
+ * client is unmounted and the new one is provided and mounted.
180
+ * @throws {Error} If `client` is unset while the provider is connected.
181
+ */
165
182
  willUpdate(changedProperties) {
166
183
  if (!changedProperties.has("client")) return;
167
184
  const nextClient = this.client;
@@ -178,7 +195,11 @@ var QueryClientProvider = class extends lit.LitElement {
178
195
  this.contextProvider.setValue(nextClient);
179
196
  if (this.isConnected) this.mountClient(nextClient);
180
197
  }
181
- /** @internal */
198
+ /**
199
+ * Renders the provider's children.
200
+ * @internal
201
+ * @returns A `<slot>` that renders the provider's children.
202
+ */
182
203
  render() {
183
204
  return lit.html`<slot></slot>`;
184
205
  }
@@ -203,9 +224,20 @@ var QueryClientProvider = class extends lit.LitElement {
203
224
  QueryClientProvider.properties = { client: { attribute: false } };
204
225
  //#endregion
205
226
  //#region src/accessor.ts
227
+ /**
228
+ * Reads an {@link Accessor}: calls it if it is a function, otherwise returns it as is.
229
+ * @param value - The value, or a getter that returns it.
230
+ * @returns The value.
231
+ */
206
232
  function readAccessor(value) {
207
233
  return typeof value === "function" ? value() : value;
208
234
  }
235
+ /**
236
+ * Creates a {@link ValueAccessor} that reads its value from `getter`, both when called and through
237
+ * its `current` property.
238
+ * @param getter - Returns the latest value.
239
+ * @returns A function that returns the latest value, which is also available on `current`.
240
+ */
209
241
  function createValueAccessor(getter) {
210
242
  const accessor = (() => getter());
211
243
  Object.defineProperty(accessor, "current", {
@@ -216,6 +248,12 @@ function createValueAccessor(getter) {
216
248
  }
217
249
  //#endregion
218
250
  //#region src/controllers/BaseController.ts
251
+ /**
252
+ * Base class of the query controllers. It resolves the `QueryClient`, either
253
+ * passed explicitly or provided through context by a `QueryClientProvider`,
254
+ * follows the host's lifecycle, and requests a host update when the result
255
+ * changes.
256
+ */
219
257
  var BaseController = class {
220
258
  constructor(host, initialResult, queryClient) {
221
259
  this.host = host;
@@ -230,6 +268,11 @@ var BaseController = class {
230
268
  this.queryClientResolutionState = queryClient ? "bound" : "pre-connect";
231
269
  host.addController(this);
232
270
  }
271
+ /**
272
+ * Starts resolving the `QueryClient` from context, unless one was passed
273
+ * explicitly, and calls `onConnected` in a microtask so subclass fields are
274
+ * initialized first.
275
+ */
233
276
  hostConnected() {
234
277
  if (this.connected || this.destroyed) return;
235
278
  this.connected = true;
@@ -244,6 +287,10 @@ var BaseController = class {
244
287
  });
245
288
  if (contextResolutionAttempt !== void 0) this.queueContextResolution(contextResolutionAttempt);
246
289
  }
290
+ /**
291
+ * Releases the context `QueryClient`, unless one was passed explicitly, and
292
+ * calls `onDisconnected`.
293
+ */
247
294
  hostDisconnected() {
248
295
  if (!this.connected) return;
249
296
  this.connected = false;
@@ -254,6 +301,10 @@ var BaseController = class {
254
301
  }
255
302
  this.onDisconnected();
256
303
  }
304
+ /**
305
+ * Calls `onHostUpdate` before the host renders. Results set during this call
306
+ * don't request another update.
307
+ */
257
308
  hostUpdate() {
258
309
  if (this.destroyed) return;
259
310
  this.isHostUpdating = true;
@@ -263,6 +314,11 @@ var BaseController = class {
263
314
  this.isHostUpdating = false;
264
315
  }
265
316
  }
317
+ /**
318
+ * Permanently disconnects the controller and removes it from its host, if
319
+ * the host supports `removeController`. The controller ignores later
320
+ * lifecycle callbacks.
321
+ */
266
322
  destroy() {
267
323
  if (this.destroyed) return;
268
324
  this.destroyed = true;
@@ -273,26 +329,53 @@ var BaseController = class {
273
329
  this.onDisconnected();
274
330
  if ("removeController" in this.host) this.host.removeController(this);
275
331
  }
332
+ /**
333
+ * Returns the `QueryClient` without throwing when none is available.
334
+ * @returns The explicitly passed client, otherwise the client from context,
335
+ * or `undefined` if neither is available.
336
+ */
276
337
  tryGetQueryClient() {
277
338
  return this.explicitClient ?? this.contextClient;
278
339
  }
340
+ /**
341
+ * Returns the `QueryClient`, throwing when none is available.
342
+ * @returns The explicitly passed client, otherwise the client from context.
343
+ * @throws {Error} If no `QueryClient` is available.
344
+ */
279
345
  getQueryClient() {
280
346
  const client = this.tryGetQueryClient();
281
347
  if (!client) throw createMissingQueryClientError();
282
348
  return client;
283
349
  }
350
+ /**
351
+ * Stores a new result and requests a host update, unless it is the same
352
+ * result or the host is already updating.
353
+ * @param next - The new result.
354
+ */
284
355
  setResult(next) {
285
356
  if (Object.is(this.result, next)) return;
286
357
  this.result = next;
287
358
  if (!this.isHostUpdating) this.queueUpdate();
288
359
  }
360
+ /**
361
+ * The latest result of the controller.
362
+ * @returns The latest result.
363
+ * @throws {Error} If no `QueryClient` could be resolved from context.
364
+ */
289
365
  get current() {
290
366
  if (this.queryClientResolutionState === "missing") throw createMissingQueryClientError();
291
367
  return this.result;
292
368
  }
369
+ /**
370
+ * Whether the controller's host is connected.
371
+ * @returns `true` while the host is connected.
372
+ */
293
373
  get connectedState() {
294
374
  return this.connected;
295
375
  }
376
+ /**
377
+ * Requests a host update in a microtask, batching multiple calls into one.
378
+ */
296
379
  queueUpdate() {
297
380
  if (this.updateQueued) return;
298
381
  this.updateQueued = true;
@@ -362,14 +445,29 @@ var BaseController = class {
362
445
  };
363
446
  //#endregion
364
447
  //#region src/queryObserverResultTracker.ts
448
+ /**
449
+ * Wraps observer results with property tracking when `notifyOnChangeProps` is
450
+ * not set, so the observer only notifies about the properties the host reads.
451
+ */
365
452
  var QueryObserverResultTracker = class {
366
453
  constructor() {
367
454
  this.usesTracking = false;
368
455
  }
456
+ /**
457
+ * Forgets the last result, so the next `update` returns a result again.
458
+ */
369
459
  reset() {
370
460
  this.result = void 0;
371
461
  this.usesTracking = false;
372
462
  }
463
+ /**
464
+ * Returns the result to expose, tracked through the observer when it uses
465
+ * property tracking.
466
+ * @param observer - The observer the result comes from, if any.
467
+ * @param result - The latest result of the observer.
468
+ * @returns The result to expose, or `undefined` if neither the result nor
469
+ * the tracking mode changed since the last call.
470
+ */
373
471
  update(observer, result) {
374
472
  const usesTracking = !!observer && !observer.options.notifyOnChangeProps;
375
473
  if (Object.is(this.result, result) && this.usesTracking === usesTracking) return;
@@ -380,6 +478,11 @@ var QueryObserverResultTracker = class {
380
478
  };
381
479
  //#endregion
382
480
  //#region src/createInfiniteQueryController.ts
481
+ /**
482
+ * Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
483
+ * that reject with the missing client error.
484
+ * @returns A new result object in that state.
485
+ */
383
486
  function createPendingInfiniteQueryResult() {
384
487
  return {
385
488
  data: void 0,
@@ -537,7 +640,6 @@ var InfiniteQueryController = class extends BaseController {
537
640
  *
538
641
  * If `queryClient` is omitted, the controller resolves the client from the
539
642
  * nearest connected `QueryClientProvider`.
540
- *
541
643
  * @param host - The Lit reactive controller host that owns the infinite query
542
644
  * subscription.
543
645
  * @param options - Infinite query observer options, or a getter that returns
@@ -546,7 +648,6 @@ var InfiniteQueryController = class extends BaseController {
546
648
  * controllers that should not resolve a client from Lit context.
547
649
  * @returns An accessor for the latest infinite query result with page helper
548
650
  * methods.
549
- *
550
651
  * @example
551
652
  * ```ts
552
653
  * import { LitElement, html } from 'lit'
@@ -583,6 +684,11 @@ function createInfiniteQueryController(host, options, queryClient) {
583
684
  }
584
685
  //#endregion
585
686
  //#region src/createMutationController.ts
687
+ /**
688
+ * Returns the result used while no `QueryClient` is available: `'idle'`, with a `mutate` that
689
+ * rejects with the missing client error.
690
+ * @returns A new result object in that state.
691
+ */
586
692
  function createIdleMutationResult() {
587
693
  return {
588
694
  context: void 0,
@@ -598,8 +704,8 @@ function createIdleMutationResult() {
598
704
  status: "idle",
599
705
  submittedAt: 0,
600
706
  variables: void 0,
601
- mutate: (() => Promise.reject(createMissingQueryClientError())),
602
- reset: (() => void 0)
707
+ mutate: () => Promise.reject(createMissingQueryClientError()),
708
+ reset: () => void 0
603
709
  };
604
710
  }
605
711
  var MutationController = class extends BaseController {
@@ -695,7 +801,6 @@ var MutationController = class extends BaseController {
695
801
  *
696
802
  * If `queryClient` is omitted, the controller resolves the client from the
697
803
  * nearest connected `QueryClientProvider`.
698
- *
699
804
  * @param host - The Lit reactive controller host that owns the mutation
700
805
  * subscription.
701
806
  * @param options - Mutation observer options, or a getter that returns options.
@@ -703,7 +808,6 @@ var MutationController = class extends BaseController {
703
808
  * controllers that should not resolve a client from Lit context.
704
809
  * @returns An accessor for the latest mutation result with mutation helper
705
810
  * methods.
706
- *
707
811
  * @example
708
812
  * ```ts
709
813
  * import { LitElement, html } from 'lit'
@@ -738,6 +842,11 @@ function createMutationController(host, options, queryClient) {
738
842
  }
739
843
  //#endregion
740
844
  //#region src/createQueriesController.ts
845
+ /**
846
+ * Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
847
+ * that reject with the missing client error.
848
+ * @returns A new result object in that state.
849
+ */
741
850
  function createPendingQueryObserverResult() {
742
851
  return {
743
852
  data: void 0,
@@ -767,6 +876,12 @@ function createPendingQueryObserverResult() {
767
876
  refetch: (() => Promise.reject(createMissingQueryClientError()))
768
877
  };
769
878
  }
879
+ /**
880
+ * Returns the result of a query used while no `QueryClient` is available: a `'success'` result with
881
+ * the query's `initialData` (after `select`), or the pending result if there is no `initialData`.
882
+ * @param query - The query options.
883
+ * @returns The `'success'` result with `initialData`, or a pending one.
884
+ */
770
885
  function createPlaceholderQueryObserverResult(query) {
771
886
  const initialData = typeof query.initialData === "function" ? query.initialData() : query.initialData;
772
887
  if (initialData === void 0) return createPendingQueryObserverResult();
@@ -783,6 +898,12 @@ function createPlaceholderQueryObserverResult(query) {
783
898
  status: "success"
784
899
  };
785
900
  }
901
+ /**
902
+ * Reads the controller's options and defaults each query with the client.
903
+ * @param optionsAccessor - The options, or a getter that returns them.
904
+ * @param client - The client used to default the query options.
905
+ * @returns The defaulted query options and the `combine` function.
906
+ */
786
907
  function resolveQueriesOptions(optionsAccessor, client) {
787
908
  const resolvedOptions = readAccessor(optionsAccessor);
788
909
  const resolvedQueries = readAccessor(resolvedOptions.queries);
@@ -976,7 +1097,6 @@ var QueriesController = class QueriesController extends BaseController {
976
1097
  *
977
1098
  * If `queryClient` is omitted, the controller resolves the client from the
978
1099
  * nearest connected `QueryClientProvider`.
979
- *
980
1100
  * @param host - The Lit reactive controller host that owns the queries
981
1101
  * subscription.
982
1102
  * @param options - Queries controller options, or a getter that returns options.
@@ -984,7 +1104,6 @@ var QueriesController = class QueriesController extends BaseController {
984
1104
  * controllers that should not resolve a client from Lit context.
985
1105
  * @returns An accessor for the latest query results, or the value returned by
986
1106
  * `combine`.
987
- *
988
1107
  * @example
989
1108
  * ```ts
990
1109
  * import { LitElement, html } from 'lit'
@@ -1016,6 +1135,11 @@ function createQueriesController(host, options, queryClient) {
1016
1135
  }
1017
1136
  //#endregion
1018
1137
  //#region src/createQueryController.ts
1138
+ /**
1139
+ * Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
1140
+ * that reject with the missing client error.
1141
+ * @returns A new result object in that state.
1142
+ */
1019
1143
  function createPendingQueryResult() {
1020
1144
  return {
1021
1145
  data: void 0,
@@ -1167,14 +1291,12 @@ var QueryController = class extends BaseController {
1167
1291
  *
1168
1292
  * If `queryClient` is omitted, the controller resolves the client from the
1169
1293
  * nearest connected `QueryClientProvider`.
1170
- *
1171
1294
  * @param host - The Lit reactive controller host that owns the query
1172
1295
  * subscription.
1173
1296
  * @param options - Query observer options, or a getter that returns options.
1174
1297
  * @param queryClient - Optional explicit query client. Provide this for
1175
1298
  * controllers that should not resolve a client from Lit context.
1176
1299
  * @returns An accessor for the latest query result with query helper methods.
1177
- *
1178
1300
  * @example
1179
1301
  * ```ts
1180
1302
  * import { LitElement, html } from 'lit'
@@ -1278,14 +1400,12 @@ var IsFetchingController = class extends BaseController {
1278
1400
  * When `filters` is a function, it is re-read during host updates so the count
1279
1401
  * can follow reactive host state. If `queryClient` is omitted, the controller
1280
1402
  * resolves the client from the nearest connected `QueryClientProvider`.
1281
- *
1282
1403
  * @param host - The Lit reactive controller host that owns the cache
1283
1404
  * subscription.
1284
1405
  * @param filters - Query filters, or a getter that returns query filters.
1285
1406
  * @param queryClient - Optional explicit query client. Provide this for
1286
1407
  * controllers that should not resolve a client from Lit context.
1287
1408
  * @returns An accessor for the current number of matching fetching queries.
1288
- *
1289
1409
  * @example
1290
1410
  * ```ts
1291
1411
  * import { LitElement, html } from 'lit'
@@ -1379,14 +1499,12 @@ var IsMutatingController = class extends BaseController {
1379
1499
  * When `filters` is a function, it is re-read during host updates so the count
1380
1500
  * can follow reactive host state. If `queryClient` is omitted, the controller
1381
1501
  * resolves the client from the nearest connected `QueryClientProvider`.
1382
- *
1383
1502
  * @param host - The Lit reactive controller host that owns the cache
1384
1503
  * subscription.
1385
1504
  * @param filters - Mutation filters, or a getter that returns mutation filters.
1386
1505
  * @param queryClient - Optional explicit query client. Provide this for
1387
1506
  * controllers that should not resolve a client from Lit context.
1388
1507
  * @returns An accessor for the current number of matching pending mutations.
1389
- *
1390
1508
  * @example
1391
1509
  * ```ts
1392
1510
  * import { LitElement, html } from 'lit'
@@ -1492,14 +1610,12 @@ var MutationStateController = class extends BaseController {
1492
1610
  * the selection can follow reactive host state. If `queryClient` is omitted,
1493
1611
  * the controller resolves the client from the nearest connected
1494
1612
  * `QueryClientProvider`.
1495
- *
1496
1613
  * @param host - The Lit reactive controller host that owns the mutation cache
1497
1614
  * subscription.
1498
1615
  * @param options - Mutation state filters and optional selector.
1499
1616
  * @param queryClient - Optional explicit query client. Provide this for
1500
1617
  * controllers that should not resolve a client from Lit context.
1501
1618
  * @returns An accessor for the selected mutation state array.
1502
- *
1503
1619
  * @example
1504
1620
  * ```ts
1505
1621
  * import { LitElement, html } from 'lit'
@@ -1535,10 +1651,8 @@ function infiniteQueryOptions(options) {
1535
1651
  //#region src/mutationOptions.ts
1536
1652
  /**
1537
1653
  * Preserves and types mutation options for reuse across Lit Query APIs.
1538
- *
1539
1654
  * @param options - Mutation options to preserve.
1540
1655
  * @returns The same options object.
1541
- *
1542
1656
  * @example
1543
1657
  * ```ts
1544
1658
  * import { mutationOptions } from '@tanstack/lit-query'