Open History to see the last commit
1import { ContextProvider } from '@lit/context'
2import { LitElement, html } from 'lit'
3import {
4 createMissingQueryClientError,
5 queryClientContext,
6 registerDefaultQueryClient,
7 unregisterDefaultQueryClient,
8} from './context.js'
9import type { QueryClient } from '@tanstack/query-core'
10import type { TemplateResult } from 'lit'
11β
12/**
13 * Lit element that provides a `QueryClient` to descendant Lit Query
14 * controllers through Lit context.
15 *
16 * The `client` is a property, not an attribute. When rendering this element in
17 * a Lit template, bind it with property binding: `.client=${queryClient}`.
18 * The provider throws if it connects without a client, or if an already
19 * connected provider has its client cleared.
20 *
21 * This class is not registered as a custom element by the package. Applications
22 * must register either a subclass or the class itself with
23 * `customElements.define`.
24 * @example
25 * ```ts
26 * import { html, LitElement } from 'lit'
27 * import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
28 *
29 * const queryClient = new QueryClient()
30 *
31 * class AppQueryProvider extends QueryClientProvider {
32 * constructor() {
33 * super()
34 * this.client = queryClient
35 * }
36 * }
37 *
38 * customElements.define('app-query-provider', AppQueryProvider)
39 *
40 * class AppRoot extends LitElement {
41 * render() {
42 * return html`<app-query-provider><todos-view></todos-view></app-query-provider>`
43 * }
44 * }
45 * ```
46 * @example
47 * ```ts
48 * import { html } from 'lit'
49 * import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
50 *
51 * const queryClient = new QueryClient()
52 *
53 * customElements.define('query-client-provider', QueryClientProvider)
54 *
55 * const view = html`
56 * <query-client-provider .client=${queryClient}>
57 * <todos-view></todos-view>
58 * </query-client-provider>
59 * `
60 * ```
61 */
62export class QueryClientProvider extends LitElement {
63 /** @internal */
64 static override properties = {
65 client: { attribute: false },
66 }
67β
68 /**
69 * The `QueryClient` provided to descendant controllers and global fallback
70 * helpers while this provider is connected.
71 *
72 * Bind this as a property in Lit templates with `.client=${queryClient}`.
73 */
74 declare client: QueryClient
75β
76 private readonly contextProvider: ContextProvider<typeof queryClientContext>
77β
78 private mountedClient: QueryClient | undefined
79β
80 constructor() {
81 super()
82 this.contextProvider = new ContextProvider(this, {
83 context: queryClientContext,
84 })
85 }
86β
87 /**
88 * Provides the client to descendants and mounts it when the element is
89 * connected.
90 * @internal
91 */
92 override connectedCallback(): void {
93 super.connectedCallback()
94 const client = this.requireClient()
95 this.contextProvider.setValue(client)
96 this.mountClient(client)
97 }
98β
99 /**
100 * Unmounts the client when the element is disconnected.
101 * @internal
102 */
103 override disconnectedCallback(): void {
104 this.unmountClient(this.mountedClient)
105 super.disconnectedCallback()
106 }
107β
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 {
118 if (!changedProperties.has('client')) {
119 return
120 }
121β
122 const nextClient = this.client
123 // oxlint-disable-next-line typescript/no-unnecessary-condition -- 'client' is undefined until it is assigned
124 if (!nextClient) {
125 if (this.isConnected) {
126 this.unmountClient(this.mountedClient)
127 // Sentinel: notify active consumers that the provider is now unbound.
128 this.contextProvider.setValue(undefined as unknown as QueryClient)
129 throw createMissingQueryClientError()
130 }
131β
132 return
133 }
134β
135 const previousClient = changedProperties.get('client') as
136 | QueryClient
137 | undefined
138 if (previousClient && previousClient !== nextClient && this.isConnected) {
139 this.unmountClient(previousClient)
140 }
141β
142 this.contextProvider.setValue(nextClient)
143β
144 if (this.isConnected) {
145 this.mountClient(nextClient)
146 }
147 }
148β
149 /**
150 * Renders the provider's children.
151 * @internal
152 * @returns A `<slot>` that renders the provider's children.
153 */
154 override render(): TemplateResult {
155 return html`<slot></slot>`
156 }
157β
158 private mountClient(client: QueryClient): void {
159 if (this.mountedClient === client) {
160 return
161 }
162β
163 if (this.mountedClient) {
164 this.unmountClient(this.mountedClient)
165 }
166β
167 client.mount()
168 registerDefaultQueryClient(client)
169 this.mountedClient = client
170 }
171β
172 private unmountClient(client?: QueryClient): void {
173 if (!client) {
174 return
175 }
176β
177 client.unmount()
178 unregisterDefaultQueryClient(client)
179β
180 if (this.mountedClient === client) {
181 this.mountedClient = undefined
182 }
183 }
184β
185 private requireClient(): QueryClient {
186 // oxlint-disable-next-line typescript/no-unnecessary-condition -- 'client' is undefined until it is assigned
187 if (!this.client) {
188 throw createMissingQueryClientError()
189 }
190β
191 return this.client
192 }
193}