Skip to content

Commit f97742e

Browse files
authored
feat(solid-query-persist-client): Solid.js implementation of client persistence (TanStack#5858)
* added solid-query-persist-client package * isRestoring context * convert persistence logic to solid * modify tests for Solid * modify test for increased reactivity * fix Provider * fix jest for Solid * add isRestoring logic * prettier + lint * make fixes to pass tests * change testing to vitest * add isRestoring check * update test * fix staleTime test * add unsubscribe condition * formatting * update createBaseQuery to refetch after hydration * refetch on hydration * formatting and build test fix * T[] -> Array<T> * changed useQueryClient to return a signal * keep queryClient reactive, without breaking API
1 parent 8cedf5c commit f97742e

23 files changed

+1064
-117
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

packages/query-devtools/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defineConfig } from 'vitest/config'
33

44
export default defineConfig({
55
test: {
6-
name: 'solid-query',
6+
name: 'query-devtools',
77
dir: './src',
88
watch: false,
99
setupFiles: [],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @ts-check
2+
3+
/** @type {import('eslint').Linter.Config} */
4+
const config = {
5+
parserOptions: {
6+
tsconfigRootDir: __dirname,
7+
project: './tsconfig.json',
8+
},
9+
}
10+
11+
module.exports = config
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "@tanstack/solid-query-persist-client",
3+
"version": "5.0.0-beta.15",
4+
"description": "Solid.js bindings to work with persisters in TanStack/solid-query",
5+
"author": "tannerlinsley",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/TanStack/query.git",
10+
"directory": "packages/solid-query-persist-client"
11+
},
12+
"homepage": "https://tanstack.com/query",
13+
"funding": {
14+
"type": "github",
15+
"url": "https://github.com/sponsors/tannerlinsley"
16+
},
17+
"type": "module",
18+
"types": "./build/index.d.ts",
19+
"main": "./build/index.cjs",
20+
"module": "./build/index.js",
21+
"exports": {
22+
"development": {
23+
"import": {
24+
"types": "./build/index.d.ts",
25+
"default": "./build/dev.js"
26+
},
27+
"require": {
28+
"types": "./build/index.d.cts",
29+
"default": "./build/dev.cjs"
30+
}
31+
},
32+
"import": {
33+
"types": "./build/index.d.ts",
34+
"default": "./build/index.js"
35+
},
36+
"require": {
37+
"types": "./build/index.d.cts",
38+
"default": "./build/index.cjs"
39+
}
40+
},
41+
"sideEffects": false,
42+
"files": [
43+
"build",
44+
"src"
45+
],
46+
"scripts": {
47+
"clean": "rimraf ./build && rimraf ./coverage",
48+
"test:eslint": "eslint --ext .ts,.tsx ./src",
49+
"test:types": "tsc",
50+
"test:lib": "vitest run --coverage",
51+
"test:lib:dev": "vitest watch --coverage",
52+
"test:build": "publint --strict",
53+
"build": "tsup"
54+
},
55+
"dependencies": {
56+
"@tanstack/query-persist-client-core": "workspace:*"
57+
},
58+
"devDependencies": {
59+
"tsup-preset-solid": "^2.0.1",
60+
"vite-plugin-solid": "^2.7.0",
61+
"solid-js": "^1.7.8"
62+
},
63+
"peerDependencies": {
64+
"@tanstack/solid-query": "workspace:*",
65+
"solid-js": "^1.6.0"
66+
}
67+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { persistQueryClient } from '@tanstack/query-persist-client-core'
2+
import { createComputed, createSignal, onCleanup } from 'solid-js'
3+
import { IsRestoringProvider, QueryClientProvider } from '@tanstack/solid-query'
4+
import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core'
5+
import type { QueryClientProviderProps } from '@tanstack/solid-query'
6+
import type { JSX } from 'solid-js'
7+
8+
export type PersistQueryClientProviderProps = QueryClientProviderProps & {
9+
persistOptions: Omit<PersistQueryClientOptions, 'queryClient'>
10+
onSuccess?: () => void
11+
}
12+
13+
export const PersistQueryClientProvider = (
14+
props: PersistQueryClientProviderProps,
15+
): JSX.Element => {
16+
const [isRestoring, setIsRestoring] = createSignal(true)
17+
18+
let unsub: undefined | (() => void)
19+
createComputed<() => void>((cleanup) => {
20+
cleanup?.()
21+
let isStale = false
22+
setIsRestoring(true)
23+
const [unsubscribe, promise] = persistQueryClient({
24+
...props.persistOptions,
25+
queryClient: props.client,
26+
})
27+
28+
promise.then(async () => {
29+
if (isStale) return
30+
try {
31+
await props.onSuccess?.()
32+
} finally {
33+
setIsRestoring(false)
34+
}
35+
})
36+
37+
unsub = () => {
38+
isStale = true
39+
unsubscribe()
40+
}
41+
return unsub
42+
})
43+
44+
onCleanup(() => unsub?.())
45+
return (
46+
<QueryClientProvider client={props.client}>
47+
<IsRestoringProvider value={isRestoring}>
48+
{props.children}
49+
</IsRestoringProvider>
50+
</QueryClientProvider>
51+
)
52+
}

0 commit comments

Comments
 (0)