Open History to see the last commit
1import { LitElement, html } from 'lit'
2import {
3 QueryClient,
4 QueryClientProvider,
5 createMutationController,
6 createQueryController,
7} from '@tanstack/lit-query'
8import { worker } from './mocks'
9import { todosMutations, todosQueries } from './services/todos/queries'
10β
11const queryClient = new QueryClient()
12β
13class AutoRefetchingQueryProvider extends QueryClientProvider {
14 constructor() {
15 super()
16 this.client = queryClient
17 }
18}
19β
20class AutoRefetchingExample extends LitElement {
21 static properties = {
22 intervalMs: { state: true },
23 value: { state: true },
24 }
25β
26 private intervalMs = 1000
27 private value = ''
28β
29 private readonly todos = createQueryController(this, () => ({
30 ...todosQueries.list(),
31 // Refetch the data every second
32 refetchInterval: this.intervalMs,
33 }))
34β
35 private readonly addMutation = createMutationController(this, () => ({
36 ...todosMutations.create(),
37 onSuccess: (_data, _variables, _onMutateResult, { client }) =>
38 client.invalidateQueries({ queryKey: todosQueries.all() }),
39 }))
40β
41 private readonly clearMutation = createMutationController(this, () => ({
42 ...todosMutations.deleteAll(),
43 onSuccess: (_data, _variables, _onMutateResult, { client }) =>
44 client.invalidateQueries({ queryKey: todosQueries.all() }),
45 }))
46β
47 protected override createRenderRoot(): HTMLElement | DocumentFragment {
48 return this
49 }
50β
51 private submitForm(event: SubmitEvent) {
52 event.preventDefault()
53 this.addMutation.mutate(this.value, {
54 onSuccess: () => {
55 this.value = ''
56 },
57 })
58 }
59β
60 render() {
61 const { status, data, error, isFetching } = this.todos()
62β
63 if (status === 'pending') return html`<h1>Loading...</h1>`
64 if (status === 'error') return html`<span>Error: ${error.message}</span>`
65β
66 return html`
67 <div>
68 <h1>Auto Refetch with refetchInterval set to ${this.intervalMs}ms</h1>
69 <p>
70 This example is best experienced on your own machine, where you can
71 open multiple tabs to the same localhost server and see your changes
72 propagate between the two.
73 </p>
74 <label>
75 Query Interval speed (ms):
76 <input
77 .value=${String(this.intervalMs)}
78 @input=${(event: Event) =>
79 (this.intervalMs = Number(
80 (event.currentTarget as HTMLInputElement).value,
81 ))}
82 type="number"
83 step="100"
84 />
85 <span
86 style="display: inline-block; margin-left: .5rem; width: 10px; height: 10px; background: ${
87 isFetching ? 'green' : 'transparent'
88 }; transition: ${
89 !isFetching ? 'all .3s ease' : 'none'
90 }; border-radius: 100%; transform: scale(2)"
91 ></span>
92 </label>
93 <h2>Todo List</h2>
94 <form @submit=${this.submitForm}>
95 <input
96 placeholder="enter something"
97 .value=${this.value}
98 @input=${(event: Event) =>
99 (this.value = (event.currentTarget as HTMLInputElement).value)}
100 />
101 </form>
102 <ul>
103 ${data.map((item) => html`<li>${item}</li>`)}
104 </ul>
105 <div>
106 <button type="button" @click=${() => this.clearMutation.mutate()}>
107 Clear All
108 </button>
109 </div>
110 </div>
111 `
112 }
113}
114β
115class AutoRefetchingApp extends LitElement {
116 protected override createRenderRoot(): HTMLElement | DocumentFragment {
117 return this
118 }
119β
120 render() {
121 return html`
122 <auto-refetching-query-provider>
123 <auto-refetching-example></auto-refetching-example>
124 </auto-refetching-query-provider>
125 `
126 }
127}
128β
129customElements.define(
130 'auto-refetching-query-provider',
131 AutoRefetchingQueryProvider,
132)
133customElements.define('auto-refetching-example', AutoRefetchingExample)
134β
135worker.start({ onUnhandledRequest: 'bypass' }).then(() => {
136 customElements.define('auto-refetching-app', AutoRefetchingApp)
137})