@tanstack/solid-query 5.0.0-alpha.7 → 5.0.0-alpha.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{build/cjs/index.js → dist/cjs/index.cjs} +92 -32
- package/dist/cjs/index.cjs.map +1 -0
- package/{build → dist}/esm/index.js +92 -34
- package/dist/esm/index.js.map +1 -0
- package/dist/source/QueryClient.js +6 -0
- package/{build → dist}/source/createBaseQuery.js +49 -23
- package/{build → dist}/source/createMutation.js +1 -1
- package/{build → dist}/source/createQueries.js +13 -7
- package/{build → dist}/source/createQuery.js +3 -0
- package/{build → dist}/source/index.js +2 -1
- package/dist/types/QueryClient.d.ts +29 -0
- package/{build → dist}/types/QueryClientProvider.d.ts +3 -3
- package/{build → dist}/types/createBaseQuery.d.ts +2 -1
- package/{build → dist}/types/createInfiniteQuery.d.ts +2 -1
- package/{build → dist}/types/createMutation.d.ts +2 -1
- package/{build → dist}/types/createQueries.d.ts +11 -9
- package/dist/types/createQuery.d.ts +14 -0
- package/{build → dist}/types/index.d.ts +3 -1
- package/dist/types/types.d.ts +34 -0
- package/{build → dist}/types/useIsFetching.d.ts +2 -1
- package/{build → dist}/types/useIsMutating.d.ts +2 -1
- package/package.json +22 -26
- package/src/QueryClient.ts +84 -0
- package/src/QueryClientProvider.tsx +2 -3
- package/src/__tests__/QueryClientProvider.test.tsx +19 -3
- package/src/__tests__/createInfiniteQuery.test.tsx +83 -19
- package/src/__tests__/createMutation.test.tsx +5 -5
- package/src/__tests__/createQueries.test.tsx +6 -78
- package/src/__tests__/createQuery.test.tsx +66 -334
- package/src/__tests__/createQuery.types.test.tsx +21 -1
- package/src/__tests__/suspense.test.tsx +7 -90
- package/src/__tests__/transition.test.tsx +1 -1
- package/src/__tests__/useIsFetching.test.tsx +1 -1
- package/src/__tests__/useIsMutating.test.tsx +5 -7
- package/src/__tests__/utils.tsx +1 -1
- package/src/createBaseQuery.ts +73 -28
- package/src/createInfiniteQuery.ts +1 -1
- package/src/createMutation.ts +3 -2
- package/src/createQueries.ts +32 -14
- package/src/createQuery.ts +24 -1
- package/src/index.ts +8 -1
- package/src/types.ts +4 -2
- package/src/useIsFetching.ts +2 -1
- package/src/useIsMutating.ts +2 -1
- package/build/cjs/index.js.map +0 -1
- package/build/esm/index.js.map +0 -1
- package/build/source/__tests__/QueryClientProvider.test.jsx +0 -121
- package/build/source/__tests__/createInfiniteQuery.test.jsx +0 -1315
- package/build/source/__tests__/createMutation.test.jsx +0 -867
- package/build/source/__tests__/createQueries.test.jsx +0 -661
- package/build/source/__tests__/createQuery.test.jsx +0 -4608
- package/build/source/__tests__/createQuery.types.test.jsx +0 -135
- package/build/source/__tests__/suspense.test.jsx +0 -721
- package/build/source/__tests__/transition.test.jsx +0 -42
- package/build/source/__tests__/useIsFetching.test.jsx +0 -190
- package/build/source/__tests__/useIsMutating.test.jsx +0 -198
- package/build/source/__tests__/utils.jsx +0 -50
- package/build/types/__tests__/QueryClientProvider.test.d.ts +0 -1
- package/build/types/__tests__/createInfiniteQuery.test.d.ts +0 -1
- package/build/types/__tests__/createMutation.test.d.ts +0 -1
- package/build/types/__tests__/createQueries.test.d.ts +0 -1
- package/build/types/__tests__/createQuery.test.d.ts +0 -1
- package/build/types/__tests__/createQuery.types.test.d.ts +0 -2
- package/build/types/__tests__/suspense.test.d.ts +0 -1
- package/build/types/__tests__/transition.test.d.ts +0 -1
- package/build/types/__tests__/useIsFetching.test.d.ts +0 -1
- package/build/types/__tests__/useIsMutating.test.d.ts +0 -1
- package/build/types/__tests__/utils.d.ts +0 -21
- package/build/types/createQuery.d.ts +0 -11
- package/build/types/types.d.ts +0 -33
- package/build/umd/index.js +0 -2
- package/build/umd/index.js.map +0 -1
- package/{build → dist}/source/QueryClientProvider.jsx +1 -1
- /package/{build → dist}/source/createInfiniteQuery.js +0 -0
- /package/{build → dist}/source/setBatchUpdatesFn.js +0 -0
- /package/{build → dist}/source/types.js +0 -0
- /package/{build → dist}/source/useIsFetching.js +0 -0
- /package/{build → dist}/source/useIsMutating.js +0 -0
- /package/{build → dist}/source/utils.js +0 -0
- /package/{build → dist}/types/setBatchUpdatesFn.d.ts +0 -0
- /package/{build → dist}/types/utils.d.ts +0 -0
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { fireEvent, render, screen, waitFor } from 'solid-testing-library';
|
|
2
|
-
import { createSignal, Show, startTransition, Suspense } from 'solid-js';
|
|
3
|
-
import { createQuery, QueryCache, QueryClientProvider } from '..';
|
|
4
|
-
import { createQueryClient, queryKey, sleep } from './utils';
|
|
5
|
-
describe("useQuery's in Suspense mode with transitions", () => {
|
|
6
|
-
const queryCache = new QueryCache();
|
|
7
|
-
const queryClient = createQueryClient({ queryCache });
|
|
8
|
-
it('should render the content when the transition is done', async () => {
|
|
9
|
-
const key = queryKey();
|
|
10
|
-
function Suspended() {
|
|
11
|
-
const state = createQuery(() => ({
|
|
12
|
-
queryKey: key,
|
|
13
|
-
queryFn: async () => {
|
|
14
|
-
await sleep(10);
|
|
15
|
-
return true;
|
|
16
|
-
},
|
|
17
|
-
}));
|
|
18
|
-
return <Show when={state.data}>Message</Show>;
|
|
19
|
-
}
|
|
20
|
-
function Page() {
|
|
21
|
-
const [showSignal, setShowSignal] = createSignal(false);
|
|
22
|
-
return (<div>
|
|
23
|
-
<button aria-label="toggle" onClick={() => startTransition(() => setShowSignal((value) => !value))}>
|
|
24
|
-
{showSignal() ? 'Hide' : 'Show'}
|
|
25
|
-
</button>
|
|
26
|
-
<Suspense fallback="Loading">
|
|
27
|
-
<Show when={showSignal()}>
|
|
28
|
-
<Suspended />
|
|
29
|
-
</Show>
|
|
30
|
-
</Suspense>
|
|
31
|
-
</div>);
|
|
32
|
-
}
|
|
33
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
34
|
-
<Page />
|
|
35
|
-
</QueryClientProvider>));
|
|
36
|
-
await waitFor(() => screen.getByText('Show'));
|
|
37
|
-
fireEvent.click(screen.getByLabelText('toggle'));
|
|
38
|
-
await waitFor(() => screen.getByText('Message'));
|
|
39
|
-
// verify that the button also updated. See https://github.com/solidjs/solid/issues/1249
|
|
40
|
-
await waitFor(() => screen.getByText('Hide'));
|
|
41
|
-
});
|
|
42
|
-
});
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { fireEvent, render, screen, waitFor } from 'solid-testing-library';
|
|
2
|
-
import { createEffect, createRenderEffect, createSignal, Show } from 'solid-js';
|
|
3
|
-
import { createQuery, QueryCache, QueryClientProvider, useIsFetching } from '..';
|
|
4
|
-
import { createQueryClient, queryKey, setActTimeout, sleep } from './utils';
|
|
5
|
-
describe('useIsFetching', () => {
|
|
6
|
-
// See https://github.com/tannerlinsley/react-query/issues/105
|
|
7
|
-
it('should update as queries start and stop fetching', async () => {
|
|
8
|
-
const queryCache = new QueryCache();
|
|
9
|
-
const queryClient = createQueryClient({ queryCache });
|
|
10
|
-
const key = queryKey();
|
|
11
|
-
function IsFetching() {
|
|
12
|
-
const isFetching = useIsFetching();
|
|
13
|
-
return <div>isFetching: {isFetching()}</div>;
|
|
14
|
-
}
|
|
15
|
-
function Query() {
|
|
16
|
-
const [ready, setReady] = createSignal(false);
|
|
17
|
-
createQuery(() => ({
|
|
18
|
-
queryKey: key,
|
|
19
|
-
queryFn: async () => {
|
|
20
|
-
await sleep(50);
|
|
21
|
-
return 'test';
|
|
22
|
-
},
|
|
23
|
-
enabled: ready(),
|
|
24
|
-
}));
|
|
25
|
-
return <button onClick={() => setReady(true)}>setReady</button>;
|
|
26
|
-
}
|
|
27
|
-
function Page() {
|
|
28
|
-
return (<div>
|
|
29
|
-
<IsFetching />
|
|
30
|
-
<Query />
|
|
31
|
-
</div>);
|
|
32
|
-
}
|
|
33
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
34
|
-
<Page />
|
|
35
|
-
</QueryClientProvider>));
|
|
36
|
-
await screen.findByText('isFetching: 0');
|
|
37
|
-
fireEvent.click(screen.getByRole('button', { name: /setReady/i }));
|
|
38
|
-
await screen.findByText('isFetching: 1');
|
|
39
|
-
await screen.findByText('isFetching: 0');
|
|
40
|
-
});
|
|
41
|
-
it('should not update state while rendering', async () => {
|
|
42
|
-
const queryCache = new QueryCache();
|
|
43
|
-
const queryClient = createQueryClient({ queryCache });
|
|
44
|
-
const key1 = queryKey();
|
|
45
|
-
const key2 = queryKey();
|
|
46
|
-
const isFetchings = [];
|
|
47
|
-
function IsFetching() {
|
|
48
|
-
const isFetching = useIsFetching();
|
|
49
|
-
createRenderEffect(() => {
|
|
50
|
-
isFetchings.push(isFetching());
|
|
51
|
-
});
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
function FirstQuery() {
|
|
55
|
-
createQuery(() => ({
|
|
56
|
-
queryKey: key1,
|
|
57
|
-
queryFn: async () => {
|
|
58
|
-
await sleep(150);
|
|
59
|
-
return 'data';
|
|
60
|
-
},
|
|
61
|
-
}));
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
function SecondQuery() {
|
|
65
|
-
createQuery(() => ({
|
|
66
|
-
queryKey: key2,
|
|
67
|
-
queryFn: async () => {
|
|
68
|
-
await sleep(200);
|
|
69
|
-
return 'data';
|
|
70
|
-
},
|
|
71
|
-
}));
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
function Page() {
|
|
75
|
-
const [renderSecond, setRenderSecond] = createSignal(false);
|
|
76
|
-
createEffect(() => {
|
|
77
|
-
setActTimeout(() => {
|
|
78
|
-
setRenderSecond(true);
|
|
79
|
-
}, 100);
|
|
80
|
-
});
|
|
81
|
-
return (<>
|
|
82
|
-
<IsFetching />
|
|
83
|
-
<FirstQuery />
|
|
84
|
-
<Show when={renderSecond()}>
|
|
85
|
-
<SecondQuery />
|
|
86
|
-
</Show>
|
|
87
|
-
</>);
|
|
88
|
-
}
|
|
89
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
90
|
-
<Page />
|
|
91
|
-
</QueryClientProvider>));
|
|
92
|
-
// unlike react, Updating renderSecond wont cause a rerender for FirstQuery
|
|
93
|
-
await waitFor(() => expect(isFetchings).toEqual([0, 1, 2, 1, 0]));
|
|
94
|
-
});
|
|
95
|
-
it('should be able to filter', async () => {
|
|
96
|
-
const queryClient = createQueryClient();
|
|
97
|
-
const key1 = queryKey();
|
|
98
|
-
const key2 = queryKey();
|
|
99
|
-
const isFetchings = [];
|
|
100
|
-
function One() {
|
|
101
|
-
createQuery(() => ({
|
|
102
|
-
queryKey: key1,
|
|
103
|
-
queryFn: async () => {
|
|
104
|
-
await sleep(10);
|
|
105
|
-
return 'test';
|
|
106
|
-
},
|
|
107
|
-
}));
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
function Two() {
|
|
111
|
-
createQuery(() => ({
|
|
112
|
-
queryKey: key2,
|
|
113
|
-
queryFn: async () => {
|
|
114
|
-
await sleep(20);
|
|
115
|
-
return 'test';
|
|
116
|
-
},
|
|
117
|
-
}));
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
function Page() {
|
|
121
|
-
const [started, setStarted] = createSignal(false);
|
|
122
|
-
const isFetching = useIsFetching(() => ({
|
|
123
|
-
queryKey: key1,
|
|
124
|
-
}));
|
|
125
|
-
createRenderEffect(() => {
|
|
126
|
-
isFetchings.push(isFetching());
|
|
127
|
-
});
|
|
128
|
-
return (<div>
|
|
129
|
-
<button onClick={() => setStarted(true)}>setStarted</button>
|
|
130
|
-
<div>isFetching: {isFetching()}</div>
|
|
131
|
-
<Show when={started()}>
|
|
132
|
-
<>
|
|
133
|
-
<One />
|
|
134
|
-
<Two />
|
|
135
|
-
</>
|
|
136
|
-
</Show>
|
|
137
|
-
</div>);
|
|
138
|
-
}
|
|
139
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
140
|
-
<Page />
|
|
141
|
-
</QueryClientProvider>));
|
|
142
|
-
await screen.findByText('isFetching: 0');
|
|
143
|
-
fireEvent.click(screen.getByRole('button', { name: /setStarted/i }));
|
|
144
|
-
await screen.findByText('isFetching: 1');
|
|
145
|
-
await screen.findByText('isFetching: 0');
|
|
146
|
-
// at no point should we have isFetching: 2
|
|
147
|
-
expect(isFetchings).toEqual(expect.not.arrayContaining([2]));
|
|
148
|
-
});
|
|
149
|
-
it('should show the correct fetching state when mounted after a query', async () => {
|
|
150
|
-
const queryClient = createQueryClient();
|
|
151
|
-
const key = queryKey();
|
|
152
|
-
function Page() {
|
|
153
|
-
createQuery(() => ({
|
|
154
|
-
queryKey: key,
|
|
155
|
-
queryFn: async () => {
|
|
156
|
-
await sleep(10);
|
|
157
|
-
return 'test';
|
|
158
|
-
},
|
|
159
|
-
}));
|
|
160
|
-
const isFetching = useIsFetching();
|
|
161
|
-
return (<div>
|
|
162
|
-
<div>isFetching: {isFetching()}</div>
|
|
163
|
-
</div>);
|
|
164
|
-
}
|
|
165
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
166
|
-
<Page />
|
|
167
|
-
</QueryClientProvider>));
|
|
168
|
-
await screen.findByText('isFetching: 1');
|
|
169
|
-
await screen.findByText('isFetching: 0');
|
|
170
|
-
});
|
|
171
|
-
it('should use provided custom queryClient', async () => {
|
|
172
|
-
const queryClient = createQueryClient();
|
|
173
|
-
const key = queryKey();
|
|
174
|
-
function Page() {
|
|
175
|
-
createQuery(() => ({
|
|
176
|
-
queryKey: key,
|
|
177
|
-
queryFn: async () => {
|
|
178
|
-
await sleep(10);
|
|
179
|
-
return 'test';
|
|
180
|
-
},
|
|
181
|
-
}), () => queryClient);
|
|
182
|
-
const isFetching = useIsFetching(undefined, () => queryClient);
|
|
183
|
-
return (<div>
|
|
184
|
-
<div>isFetching: {isFetching}</div>
|
|
185
|
-
</div>);
|
|
186
|
-
}
|
|
187
|
-
render(() => <Page></Page>);
|
|
188
|
-
await screen.findByText('isFetching: 1');
|
|
189
|
-
});
|
|
190
|
-
});
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import { fireEvent, screen, waitFor } from 'solid-testing-library';
|
|
2
|
-
import { createMutation, QueryClientProvider, useIsMutating } from '..';
|
|
3
|
-
import { createQueryClient, sleep } from './utils';
|
|
4
|
-
import { createEffect, createRenderEffect, createSignal, Show } from 'solid-js';
|
|
5
|
-
import { render } from 'solid-testing-library';
|
|
6
|
-
import * as MutationCacheModule from '../../../query-core/src/mutationCache';
|
|
7
|
-
import { setActTimeout } from './utils';
|
|
8
|
-
import { vi } from 'vitest';
|
|
9
|
-
describe('useIsMutating', () => {
|
|
10
|
-
it('should return the number of fetching mutations', async () => {
|
|
11
|
-
const isMutatings = [];
|
|
12
|
-
const queryClient = createQueryClient();
|
|
13
|
-
function IsMutating() {
|
|
14
|
-
const isMutating = useIsMutating();
|
|
15
|
-
createRenderEffect(() => {
|
|
16
|
-
isMutatings.push(isMutating());
|
|
17
|
-
});
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
function Mutations() {
|
|
21
|
-
const { mutate: mutate1 } = createMutation(() => ({
|
|
22
|
-
mutationKey: ['mutation1'],
|
|
23
|
-
mutationFn: async () => {
|
|
24
|
-
await sleep(150);
|
|
25
|
-
return 'data';
|
|
26
|
-
},
|
|
27
|
-
}));
|
|
28
|
-
const { mutate: mutate2 } = createMutation(() => ({
|
|
29
|
-
mutationKey: ['mutation2'],
|
|
30
|
-
mutationFn: async () => {
|
|
31
|
-
await sleep(50);
|
|
32
|
-
return 'data';
|
|
33
|
-
},
|
|
34
|
-
}));
|
|
35
|
-
createEffect(() => {
|
|
36
|
-
mutate1();
|
|
37
|
-
setActTimeout(() => {
|
|
38
|
-
mutate2();
|
|
39
|
-
}, 50);
|
|
40
|
-
});
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
function Page() {
|
|
44
|
-
return (<div>
|
|
45
|
-
<IsMutating />
|
|
46
|
-
<Mutations />
|
|
47
|
-
</div>);
|
|
48
|
-
}
|
|
49
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
50
|
-
<Page />
|
|
51
|
-
</QueryClientProvider>));
|
|
52
|
-
await waitFor(() => expect(isMutatings).toEqual([0, 1, 2, 1, 0]));
|
|
53
|
-
});
|
|
54
|
-
it('should filter correctly by mutationKey', async () => {
|
|
55
|
-
const isMutatings = [];
|
|
56
|
-
const queryClient = createQueryClient();
|
|
57
|
-
function IsMutating() {
|
|
58
|
-
const isMutating = useIsMutating(() => ({ mutationKey: ['mutation1'] }));
|
|
59
|
-
createRenderEffect(() => {
|
|
60
|
-
isMutatings.push(isMutating());
|
|
61
|
-
});
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
function Page() {
|
|
65
|
-
const { mutate: mutate1 } = createMutation(() => ({
|
|
66
|
-
mutationKey: ['mutation1'],
|
|
67
|
-
mutationFn: async () => {
|
|
68
|
-
await sleep(100);
|
|
69
|
-
return 'data';
|
|
70
|
-
},
|
|
71
|
-
}));
|
|
72
|
-
const { mutate: mutate2 } = createMutation(() => ({
|
|
73
|
-
mutationKey: ['mutation2'],
|
|
74
|
-
mutationFn: async () => {
|
|
75
|
-
await sleep(100);
|
|
76
|
-
return 'data';
|
|
77
|
-
},
|
|
78
|
-
}));
|
|
79
|
-
createEffect(() => {
|
|
80
|
-
mutate1();
|
|
81
|
-
mutate2();
|
|
82
|
-
});
|
|
83
|
-
return <IsMutating />;
|
|
84
|
-
}
|
|
85
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
86
|
-
<Page />
|
|
87
|
-
</QueryClientProvider>));
|
|
88
|
-
// Unlike React, IsMutating Wont re-render twice with mutation2
|
|
89
|
-
await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]));
|
|
90
|
-
});
|
|
91
|
-
it('should filter correctly by predicate', async () => {
|
|
92
|
-
const isMutatings = [];
|
|
93
|
-
const queryClient = createQueryClient();
|
|
94
|
-
function IsMutating() {
|
|
95
|
-
const isMutating = useIsMutating(() => ({
|
|
96
|
-
predicate: (mutation) => mutation.options.mutationKey?.[0] === 'mutation1',
|
|
97
|
-
}));
|
|
98
|
-
createRenderEffect(() => {
|
|
99
|
-
isMutatings.push(isMutating());
|
|
100
|
-
});
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
function Page() {
|
|
104
|
-
const { mutate: mutate1 } = createMutation(() => ({
|
|
105
|
-
mutationKey: ['mutation1'],
|
|
106
|
-
mutationFn: async () => {
|
|
107
|
-
await sleep(100);
|
|
108
|
-
return 'data';
|
|
109
|
-
},
|
|
110
|
-
}));
|
|
111
|
-
const { mutate: mutate2 } = createMutation(() => ({
|
|
112
|
-
mutationKey: ['mutation2'],
|
|
113
|
-
mutationFn: async () => {
|
|
114
|
-
await sleep(100);
|
|
115
|
-
return 'data';
|
|
116
|
-
},
|
|
117
|
-
}));
|
|
118
|
-
createEffect(() => {
|
|
119
|
-
mutate1();
|
|
120
|
-
mutate2();
|
|
121
|
-
});
|
|
122
|
-
return <IsMutating />;
|
|
123
|
-
}
|
|
124
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
125
|
-
<Page />
|
|
126
|
-
</QueryClientProvider>));
|
|
127
|
-
// Again, No unnecessary re-renders like React
|
|
128
|
-
await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]));
|
|
129
|
-
});
|
|
130
|
-
it('should use provided custom queryClient', async () => {
|
|
131
|
-
const queryClient = createQueryClient();
|
|
132
|
-
function Page() {
|
|
133
|
-
const isMutating = useIsMutating(undefined, () => queryClient);
|
|
134
|
-
const { mutate } = createMutation(() => ({
|
|
135
|
-
mutationKey: ['mutation1'],
|
|
136
|
-
mutationFn: async () => {
|
|
137
|
-
await sleep(10);
|
|
138
|
-
return 'data';
|
|
139
|
-
},
|
|
140
|
-
}), () => queryClient);
|
|
141
|
-
createEffect(() => {
|
|
142
|
-
mutate();
|
|
143
|
-
});
|
|
144
|
-
return (<div>
|
|
145
|
-
<div>mutating: {isMutating}</div>
|
|
146
|
-
</div>);
|
|
147
|
-
}
|
|
148
|
-
render(() => <Page></Page>);
|
|
149
|
-
await waitFor(() => screen.findByText('mutating: 1'));
|
|
150
|
-
});
|
|
151
|
-
it('should not change state if unmounted', async () => {
|
|
152
|
-
// We have to mock the MutationCache to not unsubscribe
|
|
153
|
-
// the listener when the component is unmounted
|
|
154
|
-
class MutationCacheMock extends MutationCacheModule.MutationCache {
|
|
155
|
-
subscribe(listener) {
|
|
156
|
-
super.subscribe(listener);
|
|
157
|
-
return () => void 0;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
const MutationCacheSpy = vi
|
|
161
|
-
.spyOn(MutationCacheModule, 'MutationCache')
|
|
162
|
-
.mockImplementation((fn) => {
|
|
163
|
-
return new MutationCacheMock(fn);
|
|
164
|
-
});
|
|
165
|
-
const queryClient = createQueryClient();
|
|
166
|
-
function IsMutating() {
|
|
167
|
-
useIsMutating();
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
function Page() {
|
|
171
|
-
const [mounted, setMounted] = createSignal(true);
|
|
172
|
-
const { mutate: mutate1 } = createMutation(() => ({
|
|
173
|
-
mutationKey: ['mutation1'],
|
|
174
|
-
mutationFn: async () => {
|
|
175
|
-
await sleep(10);
|
|
176
|
-
return 'data';
|
|
177
|
-
},
|
|
178
|
-
}));
|
|
179
|
-
createEffect(() => {
|
|
180
|
-
mutate1();
|
|
181
|
-
});
|
|
182
|
-
return (<div>
|
|
183
|
-
<button onClick={() => setMounted(false)}>unmount</button>
|
|
184
|
-
<Show when={mounted()}>
|
|
185
|
-
<IsMutating />
|
|
186
|
-
</Show>
|
|
187
|
-
</div>);
|
|
188
|
-
}
|
|
189
|
-
render(() => (<QueryClientProvider client={queryClient}>
|
|
190
|
-
<Page />
|
|
191
|
-
</QueryClientProvider>));
|
|
192
|
-
fireEvent.click(screen.getByText('unmount'));
|
|
193
|
-
// Should not display the console error
|
|
194
|
-
// "Warning: Can't perform a React state update on an unmounted component"
|
|
195
|
-
await sleep(20);
|
|
196
|
-
MutationCacheSpy.mockRestore();
|
|
197
|
-
});
|
|
198
|
-
});
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { QueryClient } from '@tanstack/query-core';
|
|
2
|
-
import { createEffect, createSignal, onCleanup, Show } from 'solid-js';
|
|
3
|
-
import { vi } from 'vitest';
|
|
4
|
-
let queryKeyCount = 0;
|
|
5
|
-
export function queryKey() {
|
|
6
|
-
queryKeyCount++;
|
|
7
|
-
return [`query_${queryKeyCount}`];
|
|
8
|
-
}
|
|
9
|
-
export const Blink = (props) => {
|
|
10
|
-
const [shouldShow, setShouldShow] = createSignal(true);
|
|
11
|
-
createEffect(() => {
|
|
12
|
-
setShouldShow(true);
|
|
13
|
-
const timeout = setActTimeout(() => setShouldShow(false), props.duration);
|
|
14
|
-
onCleanup(() => clearTimeout(timeout));
|
|
15
|
-
});
|
|
16
|
-
return (<Show when={shouldShow()} fallback={<>off</>}>
|
|
17
|
-
<>{props.children}</>
|
|
18
|
-
</Show>);
|
|
19
|
-
};
|
|
20
|
-
export function createQueryClient(config) {
|
|
21
|
-
return new QueryClient(config);
|
|
22
|
-
}
|
|
23
|
-
export function mockVisibilityState(value) {
|
|
24
|
-
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value);
|
|
25
|
-
}
|
|
26
|
-
export function mockNavigatorOnLine(value) {
|
|
27
|
-
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value);
|
|
28
|
-
}
|
|
29
|
-
export function sleep(timeout) {
|
|
30
|
-
return new Promise((resolve, _reject) => {
|
|
31
|
-
setTimeout(resolve, timeout);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
export function setActTimeout(fn, ms) {
|
|
35
|
-
return setTimeout(() => {
|
|
36
|
-
fn();
|
|
37
|
-
}, ms);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Assert the parameter is of a specific type.
|
|
41
|
-
*/
|
|
42
|
-
export function expectType(_) {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Assert the parameter is not typed as `any`
|
|
47
|
-
*/
|
|
48
|
-
export function expectTypeNotAny(_) {
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import '@testing-library/jest-dom';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import '@testing-library/jest-dom';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import type { QueryClientConfig } from '@tanstack/query-core';
|
|
3
|
-
import { QueryClient } from '@tanstack/query-core';
|
|
4
|
-
import type { ParentProps } from 'solid-js';
|
|
5
|
-
export declare function queryKey(): Array<string>;
|
|
6
|
-
export declare const Blink: (props: {
|
|
7
|
-
duration: number;
|
|
8
|
-
} & ParentProps) => import("solid-js").JSX.Element;
|
|
9
|
-
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
10
|
-
export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
|
|
11
|
-
export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
|
|
12
|
-
export declare function sleep(timeout: number): Promise<void>;
|
|
13
|
-
export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
|
|
14
|
-
/**
|
|
15
|
-
* Assert the parameter is of a specific type.
|
|
16
|
-
*/
|
|
17
|
-
export declare function expectType<T>(_: T): void;
|
|
18
|
-
/**
|
|
19
|
-
* Assert the parameter is not typed as `any`
|
|
20
|
-
*/
|
|
21
|
-
export declare function expectTypeNotAny<T>(_: 0 extends 1 & T ? never : T): void;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { QueryClient, QueryKey, DefaultError } from '@tanstack/query-core';
|
|
2
|
-
import type { CreateQueryResult, DefinedCreateQueryResult, FunctionedParams, SolidQueryOptions } from './types';
|
|
3
|
-
declare type UndefinedInitialDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = FunctionedParams<SolidQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
4
|
-
initialData?: undefined;
|
|
5
|
-
}>;
|
|
6
|
-
declare type DefinedInitialDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = FunctionedParams<SolidQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
7
|
-
initialData: TQueryFnData | (() => TQueryFnData);
|
|
8
|
-
}>;
|
|
9
|
-
export declare function createQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: () => QueryClient): CreateQueryResult<TData, TError>;
|
|
10
|
-
export declare function createQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: () => QueryClient): DefinedCreateQueryResult<TData, TError>;
|
|
11
|
-
export {};
|
package/build/types/types.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { QueryKey, QueryObserverOptions, QueryObserverResult, MutateFunction, MutationObserverOptions, MutationObserverResult, DefinedQueryObserverResult, InfiniteQueryObserverOptions, InfiniteQueryObserverResult, WithRequired, DefaultError } from '@tanstack/query-core';
|
|
2
|
-
export declare type FunctionedParams<T> = () => T;
|
|
3
|
-
export interface CreateBaseQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> extends WithRequired<QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>, 'queryKey'> {
|
|
4
|
-
deferStream?: boolean;
|
|
5
|
-
}
|
|
6
|
-
export interface SolidQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> extends WithRequired<CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, 'queryKey'> {
|
|
7
|
-
}
|
|
8
|
-
export declare type CreateQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = FunctionedParams<SolidQueryOptions<TQueryFnData, TError, TData, TQueryKey>>;
|
|
9
|
-
export declare type CreateBaseQueryResult<TData = unknown, TError = DefaultError> = QueryObserverResult<TData, TError>;
|
|
10
|
-
export declare type CreateQueryResult<TData = unknown, TError = DefaultError> = CreateBaseQueryResult<TData, TError>;
|
|
11
|
-
export declare type DefinedCreateBaseQueryResult<TData = unknown, TError = DefaultError> = DefinedQueryObserverResult<TData, TError>;
|
|
12
|
-
export declare type DefinedCreateQueryResult<TData = unknown, TError = DefaultError> = DefinedCreateBaseQueryResult<TData, TError>;
|
|
13
|
-
export interface SolidInfiniteQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> extends Omit<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>, 'queryKey'> {
|
|
14
|
-
queryKey: TQueryKey;
|
|
15
|
-
deferStream?: boolean;
|
|
16
|
-
}
|
|
17
|
-
export declare type CreateInfiniteQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = FunctionedParams<SolidInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey, TPageParam>>;
|
|
18
|
-
export declare type CreateInfiniteQueryResult<TData = unknown, TError = DefaultError> = InfiniteQueryObserverResult<TData, TError>;
|
|
19
|
-
export interface SolidMutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown> extends Omit<MutationObserverOptions<TData, TError, TVariables, TContext>, '_defaulted' | 'variables'> {
|
|
20
|
-
}
|
|
21
|
-
export declare type CreateMutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown> = FunctionedParams<SolidMutationOptions<TData, TError, TVariables, TContext>>;
|
|
22
|
-
export declare type CreateMutateFunction<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown> = (...args: Parameters<MutateFunction<TData, TError, TVariables, TContext>>) => void;
|
|
23
|
-
export declare type CreateMutateAsyncFunction<TData = unknown, TError = DefaultError, TVariables = void, TContext = unknown> = MutateFunction<TData, TError, TVariables, TContext>;
|
|
24
|
-
export declare type CreateBaseMutationResult<TData = unknown, TError = DefaultError, TVariables = unknown, TContext = unknown> = Override<MutationObserverResult<TData, TError, TVariables, TContext>, {
|
|
25
|
-
mutate: CreateMutateFunction<TData, TError, TVariables, TContext>;
|
|
26
|
-
}> & {
|
|
27
|
-
mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TContext>;
|
|
28
|
-
};
|
|
29
|
-
export declare type CreateMutationResult<TData = unknown, TError = DefaultError, TVariables = unknown, TContext = unknown> = CreateBaseMutationResult<TData, TError, TVariables, TContext>;
|
|
30
|
-
declare type Override<A, B> = {
|
|
31
|
-
[K in keyof A]: K extends keyof B ? B[K] : A[K];
|
|
32
|
-
};
|
|
33
|
-
export {};
|
package/build/umd/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@tanstack/query-core"),require("solid-js"),require("solid-js/web"),require("solid-js/store")):"function"==typeof define&&define.amd?define(["exports","@tanstack/query-core","solid-js","solid-js/web","solid-js/store"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).index={},e.QueryCore,e.Solid,e.SolidWeb,e.SolidStore)}(this,(function(e,t,r,n,o){"use strict";t.notifyManager.setBatchNotifyFunction(r.batch);const i=r.createContext(void 0),s=e=>{const t=r.useContext(i);if(e)return e;if(!t)throw new Error("No QueryClient set, use QueryClientProvider to set one");return t};function u(e,t){return"function"==typeof e?e(...t):!!e}function a(e,i,a){const c=r.createMemo((()=>s(a?.()))),l=c().defaultQueryOptions(e());l._optimisticResults="optimistic",n.isServer&&(l.retry=!1,l.throwErrors=!0);const f=new i(c(),l),[d,p]=o.createStore(f.getOptimisticResult(l)),m=()=>f.subscribe((e=>{t.notifyManager.batchCalls((()=>{const t={...o.unwrap(e)};g()?.data&&t.data&&!g.loading?(p(t),b(d)):(p(t),h())}))()}));let y=null;const[g,{refetch:h,mutate:b}]=r.createResource((()=>new Promise(((e,r)=>{n.isServer?y=((e,r)=>f.subscribe((n=>{t.notifyManager.batchCalls((()=>{const t={...o.unwrap(n)};t.isError&&("development"===process.env.NODE_ENV&&console.error(t.error),r(t.error)),t.isSuccess&&e(t)}))()})))(e,r):y||(y=m()),d.isLoading||e(d)}))),{initialValue:d,ssrLoadFrom:e().initialData?"initial":"server",get deferStream(){return e().deferStream},onHydrated(e,r){if(r.value&&t.hydrate(c(),{queries:[{queryKey:l.queryKey,queryHash:l.queryHash,state:r.value}]}),!y){const e={...l};!l.staleTime&&l.initialData||(e.refetchOnMount=!1),f.setOptions(e),p(f.getOptimisticResult(e)),y=m()}}});r.onCleanup((()=>{y&&(y(),y=null)})),r.onMount((()=>{f.setOptions(l,{listeners:!1})})),r.createComputed((()=>{f.setOptions(c().defaultQueryOptions(e()))})),r.createComputed(r.on((()=>d.status),(()=>{if(d.isError&&!d.isFetching&&u(f.options.throwErrors,[d.error,f.getCurrentQuery()]))throw d.error})));return new Proxy(d,{get:(e,t)=>"data"===t?g()?.data:Reflect.get(e,t)})}function c(){}e.QueryClientContext=i,e.QueryClientProvider=e=>(r.onMount((()=>{e.client.mount()})),r.onCleanup((()=>e.client.unmount())),n.createComponent(i.Provider,{get value(){return e.client},get children(){return e.children}})),e.createInfiniteQuery=function(e,n){return a(r.createMemo((()=>e())),t.InfiniteQueryObserver,n)},e.createMutation=function(e,n){const i=s(n?.()),a=new t.MutationObserver(i,e()),l=(e,t)=>{a.mutate(e,t).catch(c)},[f,d]=o.createStore({...a.getCurrentResult(),mutate:l,mutateAsync:a.getCurrentResult().mutate});r.createComputed((()=>{a.setOptions(e())})),r.createComputed(r.on((()=>f.status),(()=>{if(f.isError&&u(a.options.throwErrors,[f.error]))throw f.error})));const p=a.subscribe((e=>{d({...e,mutate:l,mutateAsync:e.mutate})}));return r.onCleanup(p),f},e.createQueries=function(e,n){const i=s(n?.()),u=e().queries.map((e=>{const t=i.defaultQueryOptions(e);return t._optimisticResults="optimistic",t})),a=new t.QueriesObserver(i,u),[c,l]=o.createStore(a.getOptimisticResult(u)),f=a.subscribe((e=>{t.notifyManager.batchCalls((()=>{l(o.unwrap(e))}))()}));return r.onCleanup(f),r.onMount((()=>{a.setQueries(u,{listeners:!1})})),r.createComputed((()=>{const t=e().queries.map((e=>{const t=i.defaultQueryOptions(e);return t._optimisticResults="optimistic",t}));a.setQueries(t)})),c},e.createQuery=function(e,n){return a(r.createMemo((()=>e())),t.QueryObserver,n)},e.useIsFetching=function(e,t){const n=r.createMemo((()=>s(t?.()))),o=r.createMemo((()=>n().getQueryCache())),[i,u]=r.createSignal(n().isFetching(e?.())),a=o().subscribe((()=>{u(n().isFetching(e?.()))}));return r.onCleanup(a),i},e.useIsMutating=function(e,t){const n=r.createMemo((()=>s(t?.()))),o=r.createMemo((()=>n().getMutationCache())),[i,u]=r.createSignal(n().isMutating(e?.())),a=o().subscribe((t=>{u(n().isMutating(e?.()))}));return r.onCleanup(a),i},e.useQueryClient=s,Object.keys(t).forEach((function(r){"default"===r||e.hasOwnProperty(r)||Object.defineProperty(e,r,{enumerable:!0,get:function(){return t[r]}})}))}));
|
|
2
|
-
//# sourceMappingURL=index.js.map
|