@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/lit-query",
3
- "version": "0.2.25",
3
+ "version": "0.2.27",
4
4
  "description": "Lit adapter for TanStack Query Core",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -36,37 +36,36 @@
36
36
  "files": [
37
37
  "build",
38
38
  "src",
39
- "!src/tests"
39
+ "!src/__tests__"
40
40
  ],
41
41
  "dependencies": {
42
42
  "@lit/context": "^1.1.6",
43
- "@tanstack/query-core": "5.104.0",
43
+ "@tanstack/query-core": "5.104.2",
44
44
  "lit": "^3.3.3"
45
45
  },
46
- "peerDependencies": {
47
- "@tanstack/query-core": "^5.104.0",
48
- "lit": ">=2.8.0 <4"
49
- },
50
46
  "devDependencies": {
51
- "@eslint/js": "^9.36.0",
47
+ "@lit-labs/ssr": "^3.3.1",
52
48
  "@tanstack/query-test-utils": "0.0.0",
53
- "globals": "^17.12.0",
54
- "typescript-eslint": "^8.54.0"
49
+ "typescript": "6.0.3"
50
+ },
51
+ "peerDependencies": {
52
+ "@tanstack/query-core": "^5.104.2",
53
+ "lit": ">=2.8.0 <4"
55
54
  },
56
55
  "scripts": {
57
56
  "clean": "premove ./build ./coverage ./dist-ts ./.cache/test-types",
58
57
  "compile": "tsc --project tsconfig.json",
59
- "test:eslint": "eslint --concurrency=auto .",
60
- "lint:fix": "eslint --concurrency=auto . --fix",
61
- "test:types": "tsc --noEmit --tsBuildInfoFile .cache/test-types/tscurrent.tsbuildinfo",
58
+ "test:oxlint": "oxlint ./src",
59
+ "test:types": "npm-run-all --serial test:types:*",
60
+ "test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts57 --tsBuildInfoFile .cache/test-types/ts57.tsbuildinfo",
61
+ "test:types:ts58": "node ../../node_modules/typescript58/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts58 --tsBuildInfoFile .cache/test-types/ts58.tsbuildinfo",
62
+ "test:types:ts59": "node ../../node_modules/typescript59/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts59 --tsBuildInfoFile .cache/test-types/ts59.tsbuildinfo",
63
+ "test:types:tscurrent": "tsc --project tsconfig.json --outDir .cache/test-types/tscurrent --tsBuildInfoFile .cache/test-types/tscurrent.tsbuildinfo",
64
+ "test:types:ts70": "node ../../node_modules/typescript70/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts70 --tsBuildInfoFile .cache/test-types/ts70.tsbuildinfo",
65
+ "test:types:ts71": "node ../../node_modules/typescript71/lib/tsc.js --project tsconfig.legacy.json --outDir .cache/test-types/ts71 --tsBuildInfoFile .cache/test-types/ts71.tsbuildinfo",
62
66
  "test:lib": "vitest",
63
67
  "test:lib:dev": "pnpm run test:lib --watch",
64
- "test:watch": "pnpm run test:lib:dev",
65
68
  "test:build": "publint --strict && attw --pack",
66
- "build": "tsdown --tsconfig tsconfig.prod.json",
67
- "measure:bundle": "pnpm run build && node scripts/measure-bundle.mjs",
68
- "measure:bundle:raw": "node scripts/measure-bundle.mjs",
69
- "perf:l3": "pnpm run build && node scripts/l3-stress.mjs",
70
- "perf:l3:raw": "node scripts/l3-stress.mjs"
69
+ "build": "tsdown --tsconfig tsconfig.prod.json"
71
70
  }
72
71
  }
@@ -1,6 +1,4 @@
1
1
  import { ContextProvider } from '@lit/context'
2
- import type { QueryClient } from '@tanstack/query-core'
3
- import type { TemplateResult } from 'lit'
4
2
  import { LitElement, html } from 'lit'
5
3
  import {
6
4
  createMissingQueryClientError,
@@ -8,6 +6,8 @@ import {
8
6
  registerDefaultQueryClient,
9
7
  unregisterDefaultQueryClient,
10
8
  } from './context.js'
9
+ import type { QueryClient } from '@tanstack/query-core'
10
+ import type { TemplateResult } from 'lit'
11
11
 
12
12
  /**
13
13
  * Lit element that provides a `QueryClient` to descendant Lit Query
@@ -21,7 +21,6 @@ import {
21
21
  * This class is not registered as a custom element by the package. Applications
22
22
  * must register either a subclass or the class itself with
23
23
  * `customElements.define`.
24
- *
25
24
  * @example
26
25
  * ```ts
27
26
  * import { html, LitElement } from 'lit'
@@ -44,7 +43,6 @@ import {
44
43
  * }
45
44
  * }
46
45
  * ```
47
- *
48
46
  * @example
49
47
  * ```ts
50
48
  * import { html } from 'lit'
@@ -63,7 +61,7 @@ import {
63
61
  */
64
62
  export class QueryClientProvider extends LitElement {
65
63
  /** @internal */
66
- static properties = {
64
+ static override properties = {
67
65
  client: { attribute: false },
68
66
  }
69
67
 
@@ -86,27 +84,43 @@ export class QueryClientProvider extends LitElement {
86
84
  })
87
85
  }
88
86
 
89
- /** @internal */
90
- connectedCallback(): void {
87
+ /**
88
+ * Provides the client to descendants and mounts it when the element is
89
+ * connected.
90
+ * @internal
91
+ */
92
+ override connectedCallback(): void {
91
93
  super.connectedCallback()
92
94
  const client = this.requireClient()
93
95
  this.contextProvider.setValue(client)
94
96
  this.mountClient(client)
95
97
  }
96
98
 
97
- /** @internal */
98
- disconnectedCallback(): void {
99
+ /**
100
+ * Unmounts the client when the element is disconnected.
101
+ * @internal
102
+ */
103
+ override disconnectedCallback(): void {
99
104
  this.unmountClient(this.mountedClient)
100
105
  super.disconnectedCallback()
101
106
  }
102
107
 
103
- /** @internal */
104
- protected willUpdate(changedProperties: Map<PropertyKey, unknown>): void {
108
+ /**
109
+ * Swaps the provided and mounted client when `client` changes.
110
+ * @internal
111
+ * @param changedProperties - The properties that changed. When `client` changes, the previous
112
+ * client is unmounted and the new one is provided and mounted.
113
+ * @throws {Error} If `client` is unset while the provider is connected.
114
+ */
115
+ protected override willUpdate(
116
+ changedProperties: Map<PropertyKey, unknown>,
117
+ ): void {
105
118
  if (!changedProperties.has('client')) {
106
119
  return
107
120
  }
108
121
 
109
122
  const nextClient = this.client
123
+ // oxlint-disable-next-line typescript/no-unnecessary-condition
110
124
  if (!nextClient) {
111
125
  if (this.isConnected) {
112
126
  this.unmountClient(this.mountedClient)
@@ -119,7 +133,8 @@ export class QueryClientProvider extends LitElement {
119
133
  }
120
134
 
121
135
  const previousClient = changedProperties.get('client') as
122
- QueryClient | undefined
136
+ | QueryClient
137
+ | undefined
123
138
  if (previousClient && previousClient !== nextClient && this.isConnected) {
124
139
  this.unmountClient(previousClient)
125
140
  }
@@ -131,8 +146,12 @@ export class QueryClientProvider extends LitElement {
131
146
  }
132
147
  }
133
148
 
134
- /** @internal */
135
- render(): TemplateResult {
149
+ /**
150
+ * Renders the provider's children.
151
+ * @internal
152
+ * @returns A `<slot>` that renders the provider's children.
153
+ */
154
+ override render(): TemplateResult {
136
155
  return html`<slot></slot>`
137
156
  }
138
157
 
@@ -164,6 +183,7 @@ export class QueryClientProvider extends LitElement {
164
183
  }
165
184
 
166
185
  private requireClient(): QueryClient {
186
+ // oxlint-disable-next-line typescript/no-unnecessary-condition
167
187
  if (!this.client) {
168
188
  throw createMissingQueryClientError()
169
189
  }
package/src/accessor.ts CHANGED
@@ -3,7 +3,6 @@
3
3
  *
4
4
  * Lit Query APIs read function accessors during host updates, so the getter can
5
5
  * depend on reactive host state.
6
- *
7
6
  * @example
8
7
  * ```ts
9
8
  * const staticKey: Accessor<readonly unknown[]> = ['todos']
@@ -12,6 +11,11 @@
12
11
  */
13
12
  export type Accessor<T> = T | (() => T)
14
13
 
14
+ /**
15
+ * Reads an {@link Accessor}: calls it if it is a function, otherwise returns it as is.
16
+ * @param value - The value, or a getter that returns it.
17
+ * @returns The value.
18
+ */
15
19
  export function readAccessor<T>(value: Accessor<T>): T {
16
20
  return typeof value === 'function' ? (value as () => T)() : value
17
21
  }
@@ -22,7 +26,6 @@ export function readAccessor<T>(value: Accessor<T>): T {
22
26
  *
23
27
  * Controller creators and cache state helpers return this shape so render code
24
28
  * can use either `result()` or `result.current`.
25
- *
26
29
  * @example
27
30
  * ```ts
28
31
  * const query = this.todos()
@@ -33,6 +36,12 @@ export type ValueAccessor<T> = (() => T) & {
33
36
  readonly current: T
34
37
  }
35
38
 
39
+ /**
40
+ * Creates a {@link ValueAccessor} that reads its value from `getter`, both when called and through
41
+ * its `current` property.
42
+ * @param getter - Returns the latest value.
43
+ * @returns A function that returns the latest value, which is also available on `current`.
44
+ */
36
45
  export function createValueAccessor<T>(getter: () => T): ValueAccessor<T> {
37
46
  const accessor = (() => getter()) as ValueAccessor<T>
38
47
  Object.defineProperty(accessor, 'current', {
package/src/context.ts CHANGED
@@ -26,7 +26,6 @@ let defaultClient: QueryClient | undefined
26
26
  *
27
27
  * `QueryClientProvider` calls this automatically while it is connected. Prefer
28
28
  * passing an explicit client or rendering under a provider when possible.
29
- *
30
29
  * @param client - The query client to register as the current default.
31
30
  */
32
31
  export function registerDefaultQueryClient(client: QueryClient): void {
@@ -39,7 +38,6 @@ export function registerDefaultQueryClient(client: QueryClient): void {
39
38
  * `registerDefaultQueryClient`.
40
39
  *
41
40
  * `QueryClientProvider` calls this automatically when it disconnects.
42
- *
43
41
  * @param client - The query client registration to release.
44
42
  */
45
43
  export function unregisterDefaultQueryClient(client: QueryClient): void {
@@ -65,7 +63,6 @@ export function unregisterDefaultQueryClient(client: QueryClient): void {
65
63
  /**
66
64
  * Returns the registered default `QueryClient`, if exactly one default client is
67
65
  * available.
68
- *
69
66
  * @returns The default query client, or `undefined` when there is no registered
70
67
  * client or more than one registered client.
71
68
  */
@@ -77,10 +74,19 @@ export function getDefaultQueryClient(): QueryClient | undefined {
77
74
  return defaultClient
78
75
  }
79
76
 
77
+ /**
78
+ * Creates the error thrown when no `QueryClient` is available.
79
+ * @returns A new `Error` explaining that no `QueryClient` is available.
80
+ */
80
81
  export function createMissingQueryClientError(): Error {
81
82
  return new Error(missingQueryClientMessage)
82
83
  }
83
84
 
85
+ /**
86
+ * Creates the error thrown when multiple `QueryClient`s are mounted and the default one would be
87
+ * ambiguous.
88
+ * @returns A new `Error` explaining that the default `QueryClient` is ambiguous.
89
+ */
84
90
  function createAmbiguousQueryClientError(): Error {
85
91
  return new Error(ambiguousQueryClientMessage)
86
92
  }
@@ -92,8 +98,9 @@ function createAmbiguousQueryClientError(): Error {
92
98
  * This helper is useful outside a Lit reactive controller when a single
93
99
  * provider is mounted. It throws if no client is registered or if multiple
94
100
  * clients are mounted and the default would be ambiguous.
95
- *
96
101
  * @returns The single registered query client.
102
+ * @throws {Error} If no `QueryClient` is registered, or if multiple are
103
+ * mounted and the default is ambiguous.
97
104
  */
98
105
  export function useQueryClient(): QueryClient {
99
106
  const client = getDefaultQueryClient()
@@ -110,7 +117,6 @@ export function useQueryClient(): QueryClient {
110
117
 
111
118
  /**
112
119
  * Resolves an explicit `QueryClient` or falls back to `useQueryClient`.
113
- *
114
120
  * @param explicit - Optional client supplied by the caller.
115
121
  * @returns The explicit client when provided, otherwise the current default
116
122
  * client.
@@ -1,14 +1,23 @@
1
1
  import { ContextEvent } from '@lit/context'
2
- import type { QueryClient } from '@tanstack/query-core'
3
- import type { ReactiveController, ReactiveControllerHost } from 'lit'
4
2
  import {
5
3
  createMissingQueryClientError,
6
4
  queryClientContext,
7
5
  } from '../context.js'
6
+ import type { QueryClient } from '@tanstack/query-core'
7
+ import type { ReactiveController, ReactiveControllerHost } from 'lit'
8
8
 
9
9
  type QueryClientResolutionState =
10
- 'pre-connect' | 'awaiting-context' | 'bound' | 'missing'
11
-
10
+ | 'pre-connect'
11
+ | 'awaiting-context'
12
+ | 'bound'
13
+ | 'missing'
14
+
15
+ /**
16
+ * Base class of the query controllers. It resolves the `QueryClient`, either
17
+ * passed explicitly or provided through context by a `QueryClientProvider`,
18
+ * follows the host's lifecycle, and requests a host update when the result
19
+ * changes.
20
+ */
12
21
  export abstract class BaseController<TResult> implements ReactiveController {
13
22
  protected result: TResult
14
23
 
@@ -36,6 +45,11 @@ export abstract class BaseController<TResult> implements ReactiveController {
36
45
  host.addController(this)
37
46
  }
38
47
 
48
+ /**
49
+ * Starts resolving the `QueryClient` from context, unless one was passed
50
+ * explicitly, and calls `onConnected` in a microtask so subclass fields are
51
+ * initialized first.
52
+ */
39
53
  hostConnected(): void {
40
54
  if (this.connected || this.destroyed) {
41
55
  return
@@ -69,6 +83,10 @@ export abstract class BaseController<TResult> implements ReactiveController {
69
83
  }
70
84
  }
71
85
 
86
+ /**
87
+ * Releases the context `QueryClient`, unless one was passed explicitly, and
88
+ * calls `onDisconnected`.
89
+ */
72
90
  hostDisconnected(): void {
73
91
  if (!this.connected) {
74
92
  return
@@ -85,6 +103,10 @@ export abstract class BaseController<TResult> implements ReactiveController {
85
103
  this.onDisconnected()
86
104
  }
87
105
 
106
+ /**
107
+ * Calls `onHostUpdate` before the host renders. Results set during this call
108
+ * don't request another update.
109
+ */
88
110
  hostUpdate(): void {
89
111
  if (this.destroyed) {
90
112
  return
@@ -98,6 +120,11 @@ export abstract class BaseController<TResult> implements ReactiveController {
98
120
  }
99
121
  }
100
122
 
123
+ /**
124
+ * Permanently disconnects the controller and removes it from its host, if
125
+ * the host supports `removeController`. The controller ignores later
126
+ * lifecycle callbacks.
127
+ */
101
128
  destroy(): void {
102
129
  if (this.destroyed) {
103
130
  return
@@ -117,10 +144,20 @@ export abstract class BaseController<TResult> implements ReactiveController {
117
144
  }
118
145
  }
119
146
 
147
+ /**
148
+ * Returns the `QueryClient` without throwing when none is available.
149
+ * @returns The explicitly passed client, otherwise the client from context,
150
+ * or `undefined` if neither is available.
151
+ */
120
152
  protected tryGetQueryClient(): QueryClient | undefined {
121
153
  return this.explicitClient ?? this.contextClient
122
154
  }
123
155
 
156
+ /**
157
+ * Returns the `QueryClient`, throwing when none is available.
158
+ * @returns The explicitly passed client, otherwise the client from context.
159
+ * @throws {Error} If no `QueryClient` is available.
160
+ */
124
161
  protected getQueryClient(): QueryClient {
125
162
  const client = this.tryGetQueryClient()
126
163
  if (!client) {
@@ -130,6 +167,11 @@ export abstract class BaseController<TResult> implements ReactiveController {
130
167
  return client
131
168
  }
132
169
 
170
+ /**
171
+ * Stores a new result and requests a host update, unless it is the same
172
+ * result or the host is already updating.
173
+ * @param next - The new result.
174
+ */
133
175
  protected setResult(next: TResult): void {
134
176
  if (Object.is(this.result, next)) {
135
177
  return
@@ -141,6 +183,11 @@ export abstract class BaseController<TResult> implements ReactiveController {
141
183
  }
142
184
  }
143
185
 
186
+ /**
187
+ * The latest result of the controller.
188
+ * @returns The latest result.
189
+ * @throws {Error} If no `QueryClient` could be resolved from context.
190
+ */
144
191
  get current(): TResult {
145
192
  if (this.queryClientResolutionState === 'missing') {
146
193
  throw createMissingQueryClientError()
@@ -149,10 +196,17 @@ export abstract class BaseController<TResult> implements ReactiveController {
149
196
  return this.result
150
197
  }
151
198
 
199
+ /**
200
+ * Whether the controller's host is connected.
201
+ * @returns `true` while the host is connected.
202
+ */
152
203
  protected get connectedState(): boolean {
153
204
  return this.connected
154
205
  }
155
206
 
207
+ /**
208
+ * Requests a host update in a microtask, batching multiple calls into one.
209
+ */
156
210
  protected queueUpdate(): void {
157
211
  if (this.updateQueued) {
158
212
  return
@@ -230,6 +284,7 @@ export abstract class BaseController<TResult> implements ReactiveController {
230
284
  }
231
285
 
232
286
  const resolutionChanged = this.updateQueryClientResolutionState(
287
+ // oxlint-disable-next-line typescript/no-unnecessary-condition
233
288
  value === undefined ? 'missing' : 'bound',
234
289
  )
235
290
  const clientChanged = this.contextClient !== value
@@ -282,8 +337,26 @@ export abstract class BaseController<TResult> implements ReactiveController {
282
337
  return true
283
338
  }
284
339
 
340
+ /**
341
+ * Called in a microtask after the host connects, once the subclass fields
342
+ * are initialized. Subclasses subscribe to the `QueryClient` here, if one is
343
+ * available.
344
+ */
285
345
  protected abstract onConnected(): void
346
+ /**
347
+ * Called when the host disconnects or the controller is destroyed.
348
+ * Subclasses unsubscribe here.
349
+ */
286
350
  protected abstract onDisconnected(): void
351
+ /**
352
+ * Called before each host update. Subclasses refresh their options here.
353
+ */
287
354
  protected abstract onHostUpdate(): void
355
+ /**
356
+ * Called in a microtask when the `QueryClient` from context changes, or when
357
+ * resolving it finishes without finding one (`tryGetQueryClient` then returns
358
+ * `undefined`). Subclasses resubscribe to the new client here, if there is
359
+ * one.
360
+ */
288
361
  protected abstract onQueryClientChanged(): void
289
362
  }
@@ -1,23 +1,19 @@
1
- import {
2
- InfiniteQueryObserver,
3
- type DefaultError,
4
- type DefaultedInfiniteQueryObserverOptions,
5
- type InfiniteData,
6
- type InfiniteQueryObserverOptions,
7
- type InfiniteQueryObserverResult,
8
- type QueryKey,
9
- } from '@tanstack/query-core'
10
- import type { QueryClient } from '@tanstack/query-core'
11
- import type { ReactiveControllerHost } from 'lit'
12
- import {
13
- createValueAccessor,
14
- readAccessor,
15
- type Accessor,
16
- type ValueAccessor,
17
- } from './accessor.js'
1
+ import { InfiniteQueryObserver } from '@tanstack/query-core'
2
+ import { createValueAccessor, readAccessor } from './accessor.js'
18
3
  import { createMissingQueryClientError } from './context.js'
19
4
  import { BaseController } from './controllers/BaseController.js'
20
5
  import { QueryObserverResultTracker } from './queryObserverResultTracker.js'
6
+ import type { Accessor, ValueAccessor } from './accessor.js'
7
+ import type {
8
+ DefaultError,
9
+ DefaultedInfiniteQueryObserverOptions,
10
+ InfiniteData,
11
+ InfiniteQueryObserverOptions,
12
+ InfiniteQueryObserverResult,
13
+ QueryClient,
14
+ QueryKey,
15
+ } from '@tanstack/query-core'
16
+ import type { ReactiveControllerHost } from 'lit'
21
17
 
22
18
  /**
23
19
  * Options accepted by `createInfiniteQueryController`.
@@ -62,6 +58,11 @@ export type InfiniteQueryResultAccessor<TData, TError> = ValueAccessor<
62
58
  destroy: () => void
63
59
  }
64
60
 
61
+ /**
62
+ * Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
63
+ * that reject with the missing client error.
64
+ * @returns A new result object in that state.
65
+ */
65
66
  function createPendingInfiniteQueryResult<
66
67
  TData,
67
68
  TError,
@@ -168,7 +169,7 @@ class InfiniteQueryController<
168
169
  this.assignObserverResult(observer.getOptimisticResult(defaulted))
169
170
  }
170
171
 
171
- protected onConnected(): void {
172
+ protected override onConnected(): void {
172
173
  if (!this.syncClient()) {
173
174
  return
174
175
  }
@@ -181,12 +182,12 @@ class InfiniteQueryController<
181
182
  }
182
183
  }
183
184
 
184
- protected onDisconnected(): void {
185
+ protected override onDisconnected(): void {
185
186
  this.unsubscribeObserver()
186
187
  this.syncClient()
187
188
  }
188
189
 
189
- protected onHostUpdate(): void {
190
+ protected override onHostUpdate(): void {
190
191
  if (typeof this.options !== 'function') {
191
192
  return
192
193
  }
@@ -194,7 +195,7 @@ class InfiniteQueryController<
194
195
  this.refreshOptions()
195
196
  }
196
197
 
197
- protected onQueryClientChanged(): void {
198
+ protected override onQueryClientChanged(): void {
198
199
  if (!this.syncClient() || !this.connectedState) {
199
200
  return
200
201
  }
@@ -364,7 +365,6 @@ class InfiniteQueryController<
364
365
  *
365
366
  * If `queryClient` is omitted, the controller resolves the client from the
366
367
  * nearest connected `QueryClientProvider`.
367
- *
368
368
  * @param host - The Lit reactive controller host that owns the infinite query
369
369
  * subscription.
370
370
  * @param options - Infinite query observer options, or a getter that returns
@@ -373,7 +373,6 @@ class InfiniteQueryController<
373
373
  * controllers that should not resolve a client from Lit context.
374
374
  * @returns An accessor for the latest infinite query result with page helper
375
375
  * methods.
376
- *
377
376
  * @example
378
377
  * ```ts
379
378
  * import { LitElement, html } from 'lit'
@@ -1,20 +1,16 @@
1
- import {
2
- MutationObserver,
3
- type DefaultError,
4
- type MutateFunction,
5
- type MutationObserverOptions,
6
- type MutationObserverResult,
7
- } from '@tanstack/query-core'
8
- import type { QueryClient } from '@tanstack/query-core'
9
- import type { ReactiveControllerHost } from 'lit'
10
- import {
11
- createValueAccessor,
12
- readAccessor,
13
- type Accessor,
14
- type ValueAccessor,
15
- } from './accessor.js'
1
+ import { MutationObserver } from '@tanstack/query-core'
2
+ import { createValueAccessor, readAccessor } from './accessor.js'
16
3
  import { createMissingQueryClientError } from './context.js'
17
4
  import { BaseController } from './controllers/BaseController.js'
5
+ import type { Accessor, ValueAccessor } from './accessor.js'
6
+ import type {
7
+ DefaultError,
8
+ MutateFunction,
9
+ MutationObserverOptions,
10
+ MutationObserverResult,
11
+ QueryClient,
12
+ } from '@tanstack/query-core'
13
+ import type { ReactiveControllerHost } from 'lit'
18
14
 
19
15
  /**
20
16
  * Options accepted by `createMutationController`.
@@ -71,6 +67,11 @@ export type MutationResultAccessor<TData, TError, TVariables, TOnMutateResult> =
71
67
  destroy: () => void
72
68
  }
73
69
 
70
+ /**
71
+ * Returns the result used while no `QueryClient` is available: `'idle'`, with a `mutate` that
72
+ * rejects with the missing client error.
73
+ * @returns A new result object in that state.
74
+ */
74
75
  function createIdleMutationResult<
75
76
  TData,
76
77
  TError,
@@ -91,22 +92,9 @@ function createIdleMutationResult<
91
92
  status: 'idle',
92
93
  submittedAt: 0,
93
94
  variables: undefined,
94
- mutate: (() =>
95
- Promise.reject(
96
- createMissingQueryClientError(),
97
- )) as MutationObserverResult<
98
- TData,
99
- TError,
100
- TVariables,
101
- TOnMutateResult
102
- >['mutate'],
103
- reset: (() => undefined) as MutationObserverResult<
104
- TData,
105
- TError,
106
- TVariables,
107
- TOnMutateResult
108
- >['reset'],
109
- } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>
95
+ mutate: () => Promise.reject(createMissingQueryClientError()),
96
+ reset: () => undefined,
97
+ }
110
98
  }
111
99
 
112
100
  class MutationController<
@@ -121,7 +109,8 @@ class MutationController<
121
109
  CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>
122
110
  >
123
111
  private observer:
124
- MutationObserver<TData, TError, TVariables, TOnMutateResult> | undefined
112
+ | MutationObserver<TData, TError, TVariables, TOnMutateResult>
113
+ | undefined
125
114
  private unsubscribe: (() => void) | undefined
126
115
  private queryClient: QueryClient | undefined
127
116
 
@@ -152,7 +141,7 @@ class MutationController<
152
141
  this.result = observer.getCurrentResult()
153
142
  }
154
143
 
155
- protected onConnected(): void {
144
+ protected override onConnected(): void {
156
145
  if (!this.syncClient()) {
157
146
  return
158
147
  }
@@ -164,12 +153,12 @@ class MutationController<
164
153
  }
165
154
  }
166
155
 
167
- protected onDisconnected(): void {
156
+ protected override onDisconnected(): void {
168
157
  this.unsubscribeObserver()
169
158
  this.syncClient()
170
159
  }
171
160
 
172
- protected onHostUpdate(): void {
161
+ protected override onHostUpdate(): void {
173
162
  if (typeof this.options !== 'function') {
174
163
  return
175
164
  }
@@ -177,7 +166,7 @@ class MutationController<
177
166
  this.refreshOptions()
178
167
  }
179
168
 
180
- protected onQueryClientChanged(): void {
169
+ protected override onQueryClientChanged(): void {
181
170
  if (!this.syncClient() || !this.connectedState) {
182
171
  return
183
172
  }
@@ -304,7 +293,6 @@ class MutationController<
304
293
  *
305
294
  * If `queryClient` is omitted, the controller resolves the client from the
306
295
  * nearest connected `QueryClientProvider`.
307
- *
308
296
  * @param host - The Lit reactive controller host that owns the mutation
309
297
  * subscription.
310
298
  * @param options - Mutation observer options, or a getter that returns options.
@@ -312,7 +300,6 @@ class MutationController<
312
300
  * controllers that should not resolve a client from Lit context.
313
301
  * @returns An accessor for the latest mutation result with mutation helper
314
302
  * methods.
315
- *
316
303
  * @example
317
304
  * ```ts
318
305
  * import { LitElement, html } from 'lit'