Open History to see the last commit
1import { createQuery, notifyManager } from '@tanstack/solid-query'
2import { createSignal } from 'solid-js'
3import { QueryBoundary } from '~/components/query-boundary'
4β
5function sleep(milliseconds: number) {
6 return new Promise((res) => setTimeout(res, milliseconds))
7}
8β
9function spin(milliseconds: number) {
10 const start = performance.now()
11 while (performance.now() - start <= milliseconds) {
12 // do nothing
13 }
14}
15β
16async function sayHello(name: string) {
17 console.info('[api] sayHello.start')
18β
19 await sleep(500)
20β
21 // make the layout shift more obvious, it doesn't always happen
22 console.time('[api] sayHello.spin')
23 spin(20)
24 console.timeEnd('[api] sayHello.spin')
25β
26 console.info('[api] sayHello.end')
27 return `Hello ${name}`
28}
29β
30export default function BatchMethods() {
31 const [count, setCount] = createSignal(0)
32β
33 const hello = createQuery(() => ({
34 queryKey: ['hello', count()] as const,
35 queryFn: ({ queryKey: [_, count] }) => sayHello(`solid ${count}`),
36 }))
37β
38 return (
39 <div>
40 <select
41 value="timer"
42 ref={(el) => (el.value = 'timer')} // browser caches form input
43 onInput={(e) => {
44 const type = e.currentTarget.value
45 if (type === 'raf') notifyManager.setScheduler(requestAnimationFrame)
46 if (type === 'tick') notifyManager.setScheduler(queueMicrotask)
47 if (type === 'timer')
48 notifyManager.setScheduler((cb) => setTimeout(cb, 0))
49 }}
50 >
51 <option value="raf">requestAnimationFrame</option>
52 <option value="timer">setTimeout</option>
53 <option value="tick">queueMicrotick</option>
54 </select>
55 <button class="increment" onClick={() => setCount((x) => x + 1)}>
56 Clicks: {count()}
57 </button>
58 <p>
59 <QueryBoundary loadingFallback={'Loading...'} query={hello}>
60 {(data) => <div style={{ 'background-color': 'aqua' }}>{data}</div>}
61 </QueryBoundary>
62 </p>
63 <div style={{ 'background-color': 'red' }}>
64 Something below to demonstrate layout shift
65 </div>
66 <p>
67 Due to the way solidjs handles updates, sometimes the updating of a
68 query results in DOM modifications triggering a rerender twice. This is
69 perceived as a glitch in the layout of the webpage that usually lasts
70 for one frame. By using another batching strategy in the browser,
71 instead of the default setTimeout one, we can mitigate this issue. Try
72 out requestAnimationFrame or queueMicrotick.
73 </p>
74 </div>
75 )
76}