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 controllers
14 * 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}`. The
18 * provider throws if it connects without a client, or if an already connected
19 * 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 *
25 * @example
26 * ```ts
27 * import { html, LitElement } from 'lit'
28 * import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
29 *
30 * const queryClient = new QueryClient()
31 *
32 * class AppQueryProvider extends QueryClientProvider {
33 * constructor() {
34 * super()
35 * this.client = queryClient
36 * }
37 * }
38 *
39 * customElements.define('app-query-provider', AppQueryProvider)
40 *
41 * class AppRoot extends LitElement {
42 * render() {
43 * return html`<app-query-provider
44 * ><todos-view></todos-view
45 * ></app-query-provider>`
46 * }
47 * }
48 * ```
49 *
50 * @example
51 * ```ts
52 * import { html } from 'lit'
53 * import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
54 *
55 * const queryClient = new QueryClient()
56 *
57 * customElements.define('query-client-provider', QueryClientProvider)
58 *
59 * const view = html`
60 * <query-client-provider .client=${queryClient}>
61 * <todos-view></todos-view>
62 * </query-client-provider>
63 * `
64 * ```
65 */
66export class QueryClientProvider extends LitElement {
67 /** @internal */
68 static override properties = {
69 client: { attribute: false },
70 }
71β
72 /**
73 * The `QueryClient` provided to descendant controllers and global fallback
74 * helpers while this provider is connected.
75 *
76 * Bind this as a property in Lit templates with `.client=${queryClient}`.
77 */
78 declare client: QueryClient
79β
80 private readonly contextProvider: ContextProvider<typeof queryClientContext>
81β
82 private mountedClient: QueryClient | undefined
83β
84 constructor() {
85 super()
86 this.contextProvider = new ContextProvider(this, {
87 context: queryClientContext,
88 })
89 }
90β
91 /**
92 * Provides the client to descendants and mounts it when the element is
93 * connected.
94 *
95 * @internal
96 */
97 override connectedCallback(): void {
98 super.connectedCallback()
99 const client = this.requireClient()
100 this.contextProvider.setValue(client)
101 this.mountClient(client)
102 }
103β
104 /**
105 * Unmounts the client when the element is disconnected.
106 *
107 * @internal
108 */
109 override disconnectedCallback(): void {
110 this.unmountClient(this.mountedClient)
111 super.disconnectedCallback()
112 }
113β
114 /**
115 * Swaps the provided and mounted client when `client` changes.
116 *
117 * @param changedProperties - The properties that changed. When `client`
118 * changes, the previous client is unmounted and the new one is provided and
119 * mounted.
120 * @throws {Error} If `client` is unset while the provider is connected.
121 * @internal
122 */
123 protected override willUpdate(
124 changedProperties: Map<PropertyKey, unknown>,
125 ): void {
126 if (!changedProperties.has('client')) {
127 return
128 }
129β
130 const nextClient = this.client
131 // oxlint-disable-next-line typescript/no-unnecessary-condition
132 if (!nextClient) {
133 if (this.isConnected) {
134 this.unmountClient(this.mountedClient)
135 // Sentinel: notify active consumers that the provider is now unbound.
136 this.contextProvider.setValue(undefined as unknown as QueryClient)
137 throw createMissingQueryClientError()
138 }
139β
140 return
141 }
142β
143 const previousClient = changedProperties.get('client') as
144 | QueryClient
145 | undefined
146 if (previousClient && previousClient !== nextClient && this.isConnected) {
147 this.unmountClient(previousClient)
148 }
149β
150 this.contextProvider.setValue(nextClient)
151β
152 if (this.isConnected) {
153 this.mountClient(nextClient)
154 }
155 }
156β
157 /**
158 * Renders the provider's children.
159 *
160 * @returns A `<slot>` that renders the provider's children.
161 * @internal
162 */
163 override render(): TemplateResult {
164 return html`<slot></slot>`
165 }
166β
167 private mountClient(client: QueryClient): void {
168 if (this.mountedClient === client) {
169 return
170 }
171β
172 if (this.mountedClient) {
173 this.unmountClient(this.mountedClient)
174 }
175β
176 client.mount()
177 registerDefaultQueryClient(client)
178 this.mountedClient = client
179 }
180β
181 private unmountClient(client?: QueryClient): void {
182 if (!client) {
183 return
184 }
185β
186 client.unmount()
187 unregisterDefaultQueryClient(client)
188β
189 if (this.mountedClient === client) {
190 this.mountedClient = undefined
191 }
192 }
193β
194 private requireClient(): QueryClient {
195 // oxlint-disable-next-line typescript/no-unnecessary-condition
196 if (!this.client) {
197 throw createMissingQueryClientError()
198 }
199β
200 return this.client
201 }
202}