Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

QueryClientProvider.ts

174 lines4.4 KB

1import { ContextProvider } from '@lit/context'
2import type { QueryClient } from '@tanstack/query-core'
3import type { TemplateResult } from 'lit'
4import { LitElement, html } from 'lit'
5import {
6 createMissingQueryClientError,
7 queryClientContext,
8 registerDefaultQueryClient,
9 unregisterDefaultQueryClient,
10} from './context.js'
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 *
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><todos-view></todos-view></app-query-provider>`
44 * }
45 * }
46 * ```
47 *
48 * @example
49 * ```ts
50 * import { html } from 'lit'
51 * import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
52 *
53 * const queryClient = new QueryClient()
54 *
55 * customElements.define('query-client-provider', QueryClientProvider)
56 *
57 * const view = html`
58 * <query-client-provider .client=${queryClient}>
59 * <todos-view></todos-view>
60 * </query-client-provider>
61 * `
62 * ```
63 */
64export class QueryClientProvider extends LitElement {
65 /** @internal */
66 static properties = {
67 client: { attribute: false },
68 }
69​
70 /**
71 * The `QueryClient` provided to descendant controllers and global fallback
72 * helpers while this provider is connected.
73 *
74 * Bind this as a property in Lit templates with `.client=${queryClient}`.
75 */
76 declare client: QueryClient
77​
78 private readonly contextProvider: ContextProvider<typeof queryClientContext>
79​
80 private mountedClient: QueryClient | undefined
81​
82 constructor() {
83 super()
84 this.contextProvider = new ContextProvider(this, {
85 context: queryClientContext,
86 })
87 }
88​
89 /** @internal */
90 connectedCallback(): void {
91 super.connectedCallback()
92 const client = this.requireClient()
93 this.contextProvider.setValue(client)
94 this.mountClient(client)
95 }
96​
97 /** @internal */
98 disconnectedCallback(): void {
99 this.unmountClient(this.mountedClient)
100 super.disconnectedCallback()
101 }
102​
103 /** @internal */
104 protected willUpdate(changedProperties: Map<PropertyKey, unknown>): void {
105 if (!changedProperties.has('client')) {
106 return
107 }
108​
109 const nextClient = this.client
110 if (!nextClient) {
111 if (this.isConnected) {
112 this.unmountClient(this.mountedClient)
113 // Sentinel: notify active consumers that the provider is now unbound.
114 this.contextProvider.setValue(undefined as unknown as QueryClient)
115 throw createMissingQueryClientError()
116 }
117​
118 return
119 }
120​
121 const previousClient = changedProperties.get('client') as
122 | QueryClient
123 | undefined
124 if (previousClient && previousClient !== nextClient && this.isConnected) {
125 this.unmountClient(previousClient)
126 }
127​
128 this.contextProvider.setValue(nextClient)
129​
130 if (this.isConnected) {
131 this.mountClient(nextClient)
132 }
133 }
134​
135 /** @internal */
136 render(): TemplateResult {
137 return html`<slot></slot>`
138 }
139​
140 private mountClient(client: QueryClient): void {
141 if (this.mountedClient === client) {
142 return
143 }
144​
145 if (this.mountedClient) {
146 this.unmountClient(this.mountedClient)
147 }
148​
149 client.mount()
150 registerDefaultQueryClient(client)
151 this.mountedClient = client
152 }
153​
154 private unmountClient(client?: QueryClient): void {
155 if (!client) {
156 return
157 }
158​
159 client.unmount()
160 unregisterDefaultQueryClient(client)
161​
162 if (this.mountedClient === client) {
163 this.mountedClient = undefined
164 }
165 }
166​
167 private requireClient(): QueryClient {
168 if (!this.client) {
169 throw createMissingQueryClientError()
170 }
171​
172 return this.client
173 }