@tanstack/solid-query 5.0.0-alpha.85 → 5.0.0-alpha.87
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/dev.cjs +354 -0
- package/build/dev.js +341 -0
- package/build/index.cjs +351 -0
- package/build/index.d.cts +142 -0
- package/build/index.d.ts +142 -0
- package/build/index.js +338 -0
- package/package.json +24 -18
- package/src/__tests__/createMutation.test.tsx +13 -14
- package/src/__tests__/createQuery.test.tsx +30 -64
- package/src/__tests__/utils.tsx +5 -2
- package/dist/cjs/index.cjs +0 -359
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.js +0 -342
- package/dist/esm/index.js.map +0 -1
- package/dist/source/QueryClient.js +0 -6
- package/dist/source/QueryClientProvider.jsx +0 -21
- package/dist/source/createBaseQuery.js +0 -177
- package/dist/source/createInfiniteQuery.js +0 -8
- package/dist/source/createMutation.js +0 -38
- package/dist/source/createQueries.js +0 -38
- package/dist/source/createQuery.js +0 -9
- package/dist/source/index.js +0 -15
- package/dist/source/setBatchUpdatesFn.js +0 -3
- package/dist/source/types.js +0 -2
- package/dist/source/useIsFetching.js +0 -12
- package/dist/source/useIsMutating.js +0 -12
- package/dist/source/utils.js +0 -7
- package/dist/types/QueryClient.d.ts +0 -29
- package/dist/types/QueryClientProvider.d.ts +0 -9
- package/dist/types/createBaseQuery.d.ts +0 -5
- package/dist/types/createInfiniteQuery.d.ts +0 -5
- package/dist/types/createMutation.d.ts +0 -5
- package/dist/types/createQueries.d.ts +0 -53
- package/dist/types/createQuery.d.ts +0 -14
- package/dist/types/index.d.ts +0 -13
- package/dist/types/setBatchUpdatesFn.d.ts +0 -1
- package/dist/types/types.d.ts +0 -34
- package/dist/types/useIsFetching.d.ts +0 -4
- package/dist/types/useIsMutating.d.ts +0 -4
- package/dist/types/utils.d.ts +0 -1
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
Blink,
|
|
23
23
|
createQueryClient,
|
|
24
24
|
expectType,
|
|
25
|
-
|
|
25
|
+
mockOnlineManagerIsOnline,
|
|
26
26
|
mockVisibilityState,
|
|
27
27
|
queryKey,
|
|
28
28
|
setActTimeout,
|
|
@@ -2129,39 +2129,6 @@ describe('createQuery', () => {
|
|
|
2129
2129
|
screen.getByText('status: pending')
|
|
2130
2130
|
})
|
|
2131
2131
|
|
|
2132
|
-
// See https://github.com/tannerlinsley/react-query/issues/147
|
|
2133
|
-
it('should not pass stringified variables to query function', async () => {
|
|
2134
|
-
const key = queryKey()
|
|
2135
|
-
const variables = { number: 5, boolean: false, object: {}, array: [] }
|
|
2136
|
-
type CustomQueryKey = readonly [typeof key, typeof variables]
|
|
2137
|
-
const states: CreateQueryResult<CustomQueryKey, unknown>[] = []
|
|
2138
|
-
|
|
2139
|
-
function Page() {
|
|
2140
|
-
const state = createQuery(() => ({
|
|
2141
|
-
queryKey: [key, variables] as const,
|
|
2142
|
-
queryFn: async (ctx) => {
|
|
2143
|
-
await sleep(10)
|
|
2144
|
-
return ctx.queryKey
|
|
2145
|
-
},
|
|
2146
|
-
}))
|
|
2147
|
-
|
|
2148
|
-
createRenderEffect(() => {
|
|
2149
|
-
states.push({ ...state })
|
|
2150
|
-
})
|
|
2151
|
-
return null
|
|
2152
|
-
}
|
|
2153
|
-
|
|
2154
|
-
render(() => (
|
|
2155
|
-
<QueryClientProvider client={queryClient}>
|
|
2156
|
-
<Page />
|
|
2157
|
-
</QueryClientProvider>
|
|
2158
|
-
))
|
|
2159
|
-
|
|
2160
|
-
await sleep(20)
|
|
2161
|
-
|
|
2162
|
-
expect(states[1]?.data).toEqual([key, variables])
|
|
2163
|
-
})
|
|
2164
|
-
|
|
2165
2132
|
it('should not refetch query on focus when `enabled` is set to `false`', async () => {
|
|
2166
2133
|
const key = queryKey()
|
|
2167
2134
|
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
@@ -4999,7 +4966,7 @@ describe('createQuery', () => {
|
|
|
4999
4966
|
|
|
5000
4967
|
describe('networkMode online', () => {
|
|
5001
4968
|
it('online queries should not start fetching if you are offline', async () => {
|
|
5002
|
-
const onlineMock =
|
|
4969
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5003
4970
|
|
|
5004
4971
|
const key = queryKey()
|
|
5005
4972
|
const states: Array<any> = []
|
|
@@ -5033,9 +5000,11 @@ describe('createQuery', () => {
|
|
|
5033
5000
|
</QueryClientProvider>
|
|
5034
5001
|
))
|
|
5035
5002
|
|
|
5003
|
+
window.dispatchEvent(new Event('offline'))
|
|
5004
|
+
|
|
5036
5005
|
await waitFor(() => screen.getByText('status: pending, isPaused: true'))
|
|
5037
5006
|
|
|
5038
|
-
onlineMock.
|
|
5007
|
+
onlineMock.mockRestore()
|
|
5039
5008
|
window.dispatchEvent(new Event('online'))
|
|
5040
5009
|
|
|
5041
5010
|
await waitFor(() => screen.getByText('status: success, isPaused: false'))
|
|
@@ -5044,8 +5013,6 @@ describe('createQuery', () => {
|
|
|
5044
5013
|
})
|
|
5045
5014
|
|
|
5046
5015
|
expect(states).toEqual(['paused', 'fetching', 'idle'])
|
|
5047
|
-
|
|
5048
|
-
onlineMock.mockRestore()
|
|
5049
5016
|
})
|
|
5050
5017
|
|
|
5051
5018
|
it('online queries should not refetch if you are offline', async () => {
|
|
@@ -5087,7 +5054,8 @@ describe('createQuery', () => {
|
|
|
5087
5054
|
|
|
5088
5055
|
await waitFor(() => screen.getByText('data: data1'))
|
|
5089
5056
|
|
|
5090
|
-
const onlineMock =
|
|
5057
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5058
|
+
window.dispatchEvent(new Event('offline'))
|
|
5091
5059
|
fireEvent.click(screen.getByRole('button', { name: /invalidate/i }))
|
|
5092
5060
|
|
|
5093
5061
|
await waitFor(() =>
|
|
@@ -5097,7 +5065,7 @@ describe('createQuery', () => {
|
|
|
5097
5065
|
)
|
|
5098
5066
|
await waitFor(() => screen.getByText('failureReason: null'))
|
|
5099
5067
|
|
|
5100
|
-
onlineMock.
|
|
5068
|
+
onlineMock.mockRestore()
|
|
5101
5069
|
window.dispatchEvent(new Event('online'))
|
|
5102
5070
|
|
|
5103
5071
|
await waitFor(() =>
|
|
@@ -5114,8 +5082,6 @@ describe('createQuery', () => {
|
|
|
5114
5082
|
await waitFor(() => {
|
|
5115
5083
|
expect(screen.getByText('data: data2')).toBeInTheDocument()
|
|
5116
5084
|
})
|
|
5117
|
-
|
|
5118
|
-
onlineMock.mockRestore()
|
|
5119
5085
|
})
|
|
5120
5086
|
|
|
5121
5087
|
it('online queries should not refetch if you are offline and refocus', async () => {
|
|
@@ -5155,7 +5121,7 @@ describe('createQuery', () => {
|
|
|
5155
5121
|
|
|
5156
5122
|
await waitFor(() => screen.getByText('data: data1'))
|
|
5157
5123
|
|
|
5158
|
-
const onlineMock =
|
|
5124
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5159
5125
|
fireEvent.click(screen.getByRole('button', { name: /invalidate/i }))
|
|
5160
5126
|
|
|
5161
5127
|
await waitFor(() =>
|
|
@@ -5201,7 +5167,7 @@ describe('createQuery', () => {
|
|
|
5201
5167
|
)
|
|
5202
5168
|
}
|
|
5203
5169
|
|
|
5204
|
-
const onlineMock =
|
|
5170
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5205
5171
|
|
|
5206
5172
|
render(() => (
|
|
5207
5173
|
<QueryClientProvider client={queryClient}>
|
|
@@ -5256,7 +5222,7 @@ describe('createQuery', () => {
|
|
|
5256
5222
|
)
|
|
5257
5223
|
}
|
|
5258
5224
|
|
|
5259
|
-
const onlineMock =
|
|
5225
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5260
5226
|
|
|
5261
5227
|
render(() => (
|
|
5262
5228
|
<QueryClientProvider client={queryClient}>
|
|
@@ -5314,7 +5280,7 @@ describe('createQuery', () => {
|
|
|
5314
5280
|
)
|
|
5315
5281
|
}
|
|
5316
5282
|
|
|
5317
|
-
const onlineMock =
|
|
5283
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5318
5284
|
|
|
5319
5285
|
render(() => (
|
|
5320
5286
|
<QueryClientProvider client={queryClient}>
|
|
@@ -5322,6 +5288,8 @@ describe('createQuery', () => {
|
|
|
5322
5288
|
</QueryClientProvider>
|
|
5323
5289
|
))
|
|
5324
5290
|
|
|
5291
|
+
window.dispatchEvent(new Event('offline'))
|
|
5292
|
+
|
|
5325
5293
|
await waitFor(() =>
|
|
5326
5294
|
screen.getByText('status: success, fetchStatus: paused'),
|
|
5327
5295
|
)
|
|
@@ -5341,7 +5309,7 @@ describe('createQuery', () => {
|
|
|
5341
5309
|
// triggers a second pause
|
|
5342
5310
|
window.dispatchEvent(new Event('visibilitychange'))
|
|
5343
5311
|
|
|
5344
|
-
onlineMock.
|
|
5312
|
+
onlineMock.mockRestore()
|
|
5345
5313
|
window.dispatchEvent(new Event('online'))
|
|
5346
5314
|
|
|
5347
5315
|
await waitFor(() =>
|
|
@@ -5352,7 +5320,6 @@ describe('createQuery', () => {
|
|
|
5352
5320
|
})
|
|
5353
5321
|
|
|
5354
5322
|
expect(count).toBe(1)
|
|
5355
|
-
onlineMock.mockRestore()
|
|
5356
5323
|
})
|
|
5357
5324
|
|
|
5358
5325
|
it('online queries should pause retries if you are offline', async () => {
|
|
@@ -5395,7 +5362,8 @@ describe('createQuery', () => {
|
|
|
5395
5362
|
)
|
|
5396
5363
|
await waitFor(() => screen.getByText('failureReason: failed1'))
|
|
5397
5364
|
|
|
5398
|
-
|
|
5365
|
+
window.dispatchEvent(new Event('offline'))
|
|
5366
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5399
5367
|
|
|
5400
5368
|
await sleep(20)
|
|
5401
5369
|
|
|
@@ -5408,7 +5376,7 @@ describe('createQuery', () => {
|
|
|
5408
5376
|
|
|
5409
5377
|
expect(count).toBe(1)
|
|
5410
5378
|
|
|
5411
|
-
onlineMock.
|
|
5379
|
+
onlineMock.mockRestore()
|
|
5412
5380
|
window.dispatchEvent(new Event('online'))
|
|
5413
5381
|
|
|
5414
5382
|
await waitFor(() =>
|
|
@@ -5417,8 +5385,6 @@ describe('createQuery', () => {
|
|
|
5417
5385
|
await waitFor(() => screen.getByText('failureReason: failed3'))
|
|
5418
5386
|
|
|
5419
5387
|
expect(count).toBe(3)
|
|
5420
|
-
|
|
5421
|
-
onlineMock.mockRestore()
|
|
5422
5388
|
})
|
|
5423
5389
|
|
|
5424
5390
|
it('online queries should fetch if paused and we go online even if already unmounted (because not cancelled)', async () => {
|
|
@@ -5456,7 +5422,7 @@ describe('createQuery', () => {
|
|
|
5456
5422
|
)
|
|
5457
5423
|
}
|
|
5458
5424
|
|
|
5459
|
-
const onlineMock =
|
|
5425
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5460
5426
|
|
|
5461
5427
|
render(() => (
|
|
5462
5428
|
<QueryClientProvider client={queryClient}>
|
|
@@ -5464,13 +5430,15 @@ describe('createQuery', () => {
|
|
|
5464
5430
|
</QueryClientProvider>
|
|
5465
5431
|
))
|
|
5466
5432
|
|
|
5433
|
+
window.dispatchEvent(new Event('offline'))
|
|
5434
|
+
|
|
5467
5435
|
await waitFor(() =>
|
|
5468
5436
|
screen.getByText('status: pending, fetchStatus: paused'),
|
|
5469
5437
|
)
|
|
5470
5438
|
|
|
5471
5439
|
fireEvent.click(screen.getByRole('button', { name: /hide/i }))
|
|
5472
5440
|
|
|
5473
|
-
onlineMock.
|
|
5441
|
+
onlineMock.mockRestore()
|
|
5474
5442
|
window.dispatchEvent(new Event('online'))
|
|
5475
5443
|
|
|
5476
5444
|
await sleep(15)
|
|
@@ -5481,8 +5449,6 @@ describe('createQuery', () => {
|
|
|
5481
5449
|
})
|
|
5482
5450
|
|
|
5483
5451
|
expect(count).toBe(1)
|
|
5484
|
-
|
|
5485
|
-
onlineMock.mockRestore()
|
|
5486
5452
|
})
|
|
5487
5453
|
|
|
5488
5454
|
it('online queries should not fetch if paused and we go online when cancelled and no refetchOnReconnect', async () => {
|
|
@@ -5515,7 +5481,7 @@ describe('createQuery', () => {
|
|
|
5515
5481
|
)
|
|
5516
5482
|
}
|
|
5517
5483
|
|
|
5518
|
-
const onlineMock =
|
|
5484
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5519
5485
|
|
|
5520
5486
|
render(() => (
|
|
5521
5487
|
<QueryClientProvider client={queryClient}>
|
|
@@ -5599,7 +5565,7 @@ describe('createQuery', () => {
|
|
|
5599
5565
|
screen.getByText('status: success, fetchStatus: idle'),
|
|
5600
5566
|
)
|
|
5601
5567
|
|
|
5602
|
-
const onlineMock =
|
|
5568
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5603
5569
|
|
|
5604
5570
|
fireEvent.click(screen.getByRole('button', { name: /invalidate/i }))
|
|
5605
5571
|
|
|
@@ -5629,7 +5595,7 @@ describe('createQuery', () => {
|
|
|
5629
5595
|
|
|
5630
5596
|
describe('networkMode always', () => {
|
|
5631
5597
|
it('always queries should start fetching even if you are offline', async () => {
|
|
5632
|
-
const onlineMock =
|
|
5598
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5633
5599
|
|
|
5634
5600
|
const key = queryKey()
|
|
5635
5601
|
let count = 0
|
|
@@ -5671,7 +5637,7 @@ describe('createQuery', () => {
|
|
|
5671
5637
|
})
|
|
5672
5638
|
|
|
5673
5639
|
it('always queries should not pause retries', async () => {
|
|
5674
|
-
const onlineMock =
|
|
5640
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5675
5641
|
|
|
5676
5642
|
const key = queryKey()
|
|
5677
5643
|
let count = 0
|
|
@@ -5721,7 +5687,7 @@ describe('createQuery', () => {
|
|
|
5721
5687
|
|
|
5722
5688
|
describe('networkMode offlineFirst', () => {
|
|
5723
5689
|
it('offlineFirst queries should start fetching if you are offline, but pause retries', async () => {
|
|
5724
|
-
const onlineMock =
|
|
5690
|
+
const onlineMock = mockOnlineManagerIsOnline(false)
|
|
5725
5691
|
|
|
5726
5692
|
const key = queryKey()
|
|
5727
5693
|
let count = 0
|
|
@@ -5756,6 +5722,8 @@ describe('createQuery', () => {
|
|
|
5756
5722
|
</QueryClientProvider>
|
|
5757
5723
|
))
|
|
5758
5724
|
|
|
5725
|
+
window.dispatchEvent(new Event('offline'))
|
|
5726
|
+
|
|
5759
5727
|
await waitFor(() =>
|
|
5760
5728
|
screen.getByText(
|
|
5761
5729
|
'status: pending, fetchStatus: paused, failureCount: 1',
|
|
@@ -5765,7 +5733,7 @@ describe('createQuery', () => {
|
|
|
5765
5733
|
|
|
5766
5734
|
expect(count).toBe(1)
|
|
5767
5735
|
|
|
5768
|
-
onlineMock.
|
|
5736
|
+
onlineMock.mockRestore()
|
|
5769
5737
|
window.dispatchEvent(new Event('online'))
|
|
5770
5738
|
|
|
5771
5739
|
await waitFor(() =>
|
|
@@ -5774,8 +5742,6 @@ describe('createQuery', () => {
|
|
|
5774
5742
|
await waitFor(() => screen.getByText('failureReason: failed3'))
|
|
5775
5743
|
|
|
5776
5744
|
expect(count).toBe(3)
|
|
5777
|
-
|
|
5778
|
-
onlineMock.mockRestore()
|
|
5779
5745
|
})
|
|
5780
5746
|
})
|
|
5781
5747
|
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Show, createEffect, createSignal, onCleanup } from 'solid-js'
|
|
2
2
|
import { vi } from 'vitest'
|
|
3
|
+
import { onlineManager } from '@tanstack/query-core'
|
|
3
4
|
import { QueryClient } from '../QueryClient'
|
|
4
5
|
import type { QueryClientConfig } from '@tanstack/query-core'
|
|
5
6
|
import type { ParentProps } from 'solid-js'
|
|
@@ -41,8 +42,10 @@ export function mockVisibilityState(
|
|
|
41
42
|
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
export function
|
|
45
|
-
|
|
45
|
+
export function mockOnlineManagerIsOnline(
|
|
46
|
+
value: boolean,
|
|
47
|
+
): SpyInstance<[], boolean> {
|
|
48
|
+
return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
export function sleep(timeout: number): Promise<void> {
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,359 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var queryCore = require('@tanstack/query-core');
|
|
4
|
-
var solidJs = require('solid-js');
|
|
5
|
-
var web = require('solid-js/web');
|
|
6
|
-
var store = require('solid-js/store');
|
|
7
|
-
|
|
8
|
-
queryCore.notifyManager.setBatchNotifyFunction(solidJs.batch);
|
|
9
|
-
|
|
10
|
-
class QueryClient extends queryCore.QueryClient {
|
|
11
|
-
constructor(config = {}) {
|
|
12
|
-
super(config);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const QueryClientContext = solidJs.createContext(undefined);
|
|
17
|
-
const useQueryClient = queryClient => {
|
|
18
|
-
if (queryClient) {
|
|
19
|
-
return queryClient;
|
|
20
|
-
}
|
|
21
|
-
const client = solidJs.useContext(QueryClientContext);
|
|
22
|
-
if (!client) {
|
|
23
|
-
throw new Error('No QueryClient set, use QueryClientProvider to set one');
|
|
24
|
-
}
|
|
25
|
-
return client;
|
|
26
|
-
};
|
|
27
|
-
const QueryClientProvider = props => {
|
|
28
|
-
solidJs.onMount(() => {
|
|
29
|
-
props.client.mount();
|
|
30
|
-
});
|
|
31
|
-
solidJs.onCleanup(() => props.client.unmount());
|
|
32
|
-
return web.createComponent(QueryClientContext.Provider, {
|
|
33
|
-
get value() {
|
|
34
|
-
return props.client;
|
|
35
|
-
},
|
|
36
|
-
get children() {
|
|
37
|
-
return props.children;
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
function shouldThrowError(throwError, params) {
|
|
43
|
-
// Allow throwError function to override throwing behavior on a per-error basis
|
|
44
|
-
if (typeof throwError === 'function') {
|
|
45
|
-
return throwError(...params);
|
|
46
|
-
}
|
|
47
|
-
return !!throwError;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
51
|
-
// Had to disable the lint rule because isServer type is defined as false
|
|
52
|
-
// in solid-js/web package. I'll create a GitHub issue with them to see
|
|
53
|
-
// why that happens.
|
|
54
|
-
function reconcileFn(store$1, result, reconcileOption) {
|
|
55
|
-
if (reconcileOption === false) return result;
|
|
56
|
-
if (typeof reconcileOption === 'function') {
|
|
57
|
-
const newData = reconcileOption(store$1.data, result.data);
|
|
58
|
-
return {
|
|
59
|
-
...result,
|
|
60
|
-
data: newData
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
const newData = store.reconcile(result.data, {
|
|
64
|
-
key: reconcileOption
|
|
65
|
-
})(store$1.data);
|
|
66
|
-
return {
|
|
67
|
-
...result,
|
|
68
|
-
data: newData
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Base Query Function that is used to create the query.
|
|
73
|
-
function createBaseQuery(options, Observer, queryClient) {
|
|
74
|
-
const client = solidJs.createMemo(() => useQueryClient(queryClient?.()));
|
|
75
|
-
const defaultedOptions = client().defaultQueryOptions(options());
|
|
76
|
-
defaultedOptions._optimisticResults = 'optimistic';
|
|
77
|
-
defaultedOptions.structuralSharing = false;
|
|
78
|
-
if (web.isServer) {
|
|
79
|
-
defaultedOptions.retry = false;
|
|
80
|
-
defaultedOptions.throwOnError = true;
|
|
81
|
-
}
|
|
82
|
-
const observer = new Observer(client(), defaultedOptions);
|
|
83
|
-
const [state, setState] = store.createStore(observer.getOptimisticResult(defaultedOptions));
|
|
84
|
-
const createServerSubscriber = (resolve, reject) => {
|
|
85
|
-
return observer.subscribe(result => {
|
|
86
|
-
queryCore.notifyManager.batchCalls(() => {
|
|
87
|
-
const query = observer.getCurrentQuery();
|
|
88
|
-
const {
|
|
89
|
-
refetch,
|
|
90
|
-
...rest
|
|
91
|
-
} = store.unwrap(result);
|
|
92
|
-
const unwrappedResult = {
|
|
93
|
-
...rest,
|
|
94
|
-
// hydrate() expects a QueryState object, which is similar but not
|
|
95
|
-
// quite the same as a QueryObserverResult object. Thus, for now, we're
|
|
96
|
-
// copying over the missing properties from state in order to support hydration
|
|
97
|
-
dataUpdateCount: query.state.dataUpdateCount,
|
|
98
|
-
fetchFailureCount: query.state.fetchFailureCount,
|
|
99
|
-
// Removing these properties since they might not be serializable
|
|
100
|
-
// fetchFailureReason: query.state.fetchFailureReason,
|
|
101
|
-
// fetchMeta: query.state.fetchMeta,
|
|
102
|
-
isInvalidated: query.state.isInvalidated
|
|
103
|
-
};
|
|
104
|
-
if (unwrappedResult.isError) {
|
|
105
|
-
if (process.env['NODE_ENV'] === 'development') {
|
|
106
|
-
console.error(unwrappedResult.error);
|
|
107
|
-
}
|
|
108
|
-
reject(unwrappedResult.error);
|
|
109
|
-
}
|
|
110
|
-
if (unwrappedResult.isSuccess) {
|
|
111
|
-
// Use of any here is fine
|
|
112
|
-
// We cannot include refetch since it is not serializable
|
|
113
|
-
resolve(unwrappedResult);
|
|
114
|
-
}
|
|
115
|
-
})();
|
|
116
|
-
});
|
|
117
|
-
};
|
|
118
|
-
const createClientSubscriber = () => {
|
|
119
|
-
return observer.subscribe(result => {
|
|
120
|
-
queryCore.notifyManager.batchCalls(() => {
|
|
121
|
-
// @ts-expect-error - This will error because the reconcile option does not
|
|
122
|
-
// exist on the query-core QueryObserverResult type
|
|
123
|
-
const reconcileOptions = observer.options.reconcile;
|
|
124
|
-
// If the query has data we dont suspend but instead mutate the resource
|
|
125
|
-
// This could happen when placeholderData/initialData is defined
|
|
126
|
-
if (queryResource()?.data && result.data && !queryResource.loading) {
|
|
127
|
-
setState(store => {
|
|
128
|
-
return reconcileFn(store, result, reconcileOptions === undefined ? 'id' : reconcileOptions);
|
|
129
|
-
});
|
|
130
|
-
mutate(state);
|
|
131
|
-
} else {
|
|
132
|
-
setState(store => {
|
|
133
|
-
return reconcileFn(store, result, reconcileOptions === undefined ? 'id' : reconcileOptions);
|
|
134
|
-
});
|
|
135
|
-
refetch();
|
|
136
|
-
}
|
|
137
|
-
})();
|
|
138
|
-
});
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Unsubscribe is set lazily, so that we can subscribe after hydration when needed.
|
|
143
|
-
*/
|
|
144
|
-
let unsubscribe = null;
|
|
145
|
-
const [queryResource, {
|
|
146
|
-
refetch,
|
|
147
|
-
mutate
|
|
148
|
-
}] = solidJs.createResource(() => {
|
|
149
|
-
return new Promise((resolve, reject) => {
|
|
150
|
-
if (web.isServer) {
|
|
151
|
-
unsubscribe = createServerSubscriber(resolve, reject);
|
|
152
|
-
} else {
|
|
153
|
-
if (!unsubscribe) {
|
|
154
|
-
unsubscribe = createClientSubscriber();
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
if (!state.isLoading) {
|
|
158
|
-
resolve(state);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
}, {
|
|
162
|
-
initialValue: state,
|
|
163
|
-
// If initialData is provided, we resolve the resource immediately
|
|
164
|
-
ssrLoadFrom: options().initialData ? 'initial' : 'server',
|
|
165
|
-
get deferStream() {
|
|
166
|
-
return options().deferStream;
|
|
167
|
-
},
|
|
168
|
-
/**
|
|
169
|
-
* If this resource was populated on the server (either sync render, or streamed in over time), onHydrated
|
|
170
|
-
* will be called. This is the point at which we can hydrate the query cache state, and setup the query subscriber.
|
|
171
|
-
*
|
|
172
|
-
* Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
|
|
173
|
-
*
|
|
174
|
-
* Note that this is only invoked on the client, for queries that were originally run on the server.
|
|
175
|
-
*/
|
|
176
|
-
onHydrated(_k, info) {
|
|
177
|
-
if (info.value) {
|
|
178
|
-
queryCore.hydrate(client(), {
|
|
179
|
-
queries: [{
|
|
180
|
-
queryKey: defaultedOptions.queryKey,
|
|
181
|
-
queryHash: defaultedOptions.queryHash,
|
|
182
|
-
state: info.value
|
|
183
|
-
}]
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
if (!unsubscribe) {
|
|
187
|
-
/**
|
|
188
|
-
* Do not refetch query on mount if query was fetched on server,
|
|
189
|
-
* even if `staleTime` is not set.
|
|
190
|
-
*/
|
|
191
|
-
const newOptions = {
|
|
192
|
-
...defaultedOptions
|
|
193
|
-
};
|
|
194
|
-
if (defaultedOptions.staleTime || !defaultedOptions.initialData) {
|
|
195
|
-
newOptions.refetchOnMount = false;
|
|
196
|
-
}
|
|
197
|
-
// Setting the options as an immutable object to prevent
|
|
198
|
-
// wonky behavior with observer subscriptions
|
|
199
|
-
observer.setOptions(newOptions);
|
|
200
|
-
setState(observer.getOptimisticResult(newOptions));
|
|
201
|
-
unsubscribe = createClientSubscriber();
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
solidJs.onCleanup(() => {
|
|
206
|
-
if (unsubscribe) {
|
|
207
|
-
unsubscribe();
|
|
208
|
-
unsubscribe = null;
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
solidJs.createComputed(solidJs.on(() => client().defaultQueryOptions(options()), () => observer.setOptions(client().defaultQueryOptions(options())), {
|
|
212
|
-
// Defer because we don't need to trigger on first render
|
|
213
|
-
// This only cares about changes to options after the observer is created
|
|
214
|
-
defer: true
|
|
215
|
-
}));
|
|
216
|
-
solidJs.createComputed(solidJs.on(() => state.status, () => {
|
|
217
|
-
if (state.isError && !state.isFetching && shouldThrowError(observer.options.throwOnError, [state.error, observer.getCurrentQuery()])) {
|
|
218
|
-
throw state.error;
|
|
219
|
-
}
|
|
220
|
-
}));
|
|
221
|
-
const handler = {
|
|
222
|
-
get(target, prop) {
|
|
223
|
-
const val = queryResource()?.[prop];
|
|
224
|
-
return val !== undefined ? val : Reflect.get(target, prop);
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
return new Proxy(state, handler);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function queryOptions(options) {
|
|
231
|
-
return options;
|
|
232
|
-
}
|
|
233
|
-
function createQuery(options, queryClient) {
|
|
234
|
-
return createBaseQuery(solidJs.createMemo(() => options()), queryCore.QueryObserver, queryClient);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function useIsFetching(filters, queryClient) {
|
|
238
|
-
const client = solidJs.createMemo(() => useQueryClient(queryClient?.()));
|
|
239
|
-
const queryCache = solidJs.createMemo(() => client().getQueryCache());
|
|
240
|
-
const [fetches, setFetches] = solidJs.createSignal(client().isFetching(filters?.()));
|
|
241
|
-
const unsubscribe = queryCache().subscribe(() => {
|
|
242
|
-
setFetches(client().isFetching(filters?.()));
|
|
243
|
-
});
|
|
244
|
-
solidJs.onCleanup(unsubscribe);
|
|
245
|
-
return fetches;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function createInfiniteQuery(options, queryClient) {
|
|
249
|
-
return createBaseQuery(solidJs.createMemo(() => options()),
|
|
250
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
251
|
-
queryCore.InfiniteQueryObserver, queryClient);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// HOOK
|
|
255
|
-
function createMutation(options, queryClient) {
|
|
256
|
-
const client = useQueryClient(queryClient?.());
|
|
257
|
-
const observer = new queryCore.MutationObserver(client, options());
|
|
258
|
-
const mutate = (variables, mutateOptions) => {
|
|
259
|
-
observer.mutate(variables, mutateOptions).catch(noop);
|
|
260
|
-
};
|
|
261
|
-
const [state, setState] = store.createStore({
|
|
262
|
-
...observer.getCurrentResult(),
|
|
263
|
-
mutate,
|
|
264
|
-
mutateAsync: observer.getCurrentResult().mutate
|
|
265
|
-
});
|
|
266
|
-
solidJs.createComputed(() => {
|
|
267
|
-
observer.setOptions(options());
|
|
268
|
-
});
|
|
269
|
-
solidJs.createComputed(solidJs.on(() => state.status, () => {
|
|
270
|
-
if (state.isError && shouldThrowError(observer.options.throwOnError, [state.error])) {
|
|
271
|
-
throw state.error;
|
|
272
|
-
}
|
|
273
|
-
}));
|
|
274
|
-
const unsubscribe = observer.subscribe(result => {
|
|
275
|
-
setState({
|
|
276
|
-
...result,
|
|
277
|
-
mutate,
|
|
278
|
-
mutateAsync: result.mutate
|
|
279
|
-
});
|
|
280
|
-
});
|
|
281
|
-
solidJs.onCleanup(unsubscribe);
|
|
282
|
-
return state;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
286
|
-
function noop() {}
|
|
287
|
-
|
|
288
|
-
function useIsMutating(filters, queryClient) {
|
|
289
|
-
const client = solidJs.createMemo(() => useQueryClient(queryClient?.()));
|
|
290
|
-
const mutationCache = solidJs.createMemo(() => client().getMutationCache());
|
|
291
|
-
const [mutations, setMutations] = solidJs.createSignal(client().isMutating(filters?.()));
|
|
292
|
-
const unsubscribe = mutationCache().subscribe(_result => {
|
|
293
|
-
setMutations(client().isMutating(filters?.()));
|
|
294
|
-
});
|
|
295
|
-
solidJs.onCleanup(unsubscribe);
|
|
296
|
-
return mutations;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
300
|
-
// `placeholderData` function does not have a parameter
|
|
301
|
-
// Avoid TS depth-limit error in case of large array literal
|
|
302
|
-
/**
|
|
303
|
-
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
|
|
304
|
-
*/
|
|
305
|
-
/**
|
|
306
|
-
* QueriesResults reducer recursively maps type param to results
|
|
307
|
-
*/
|
|
308
|
-
function createQueries(queriesOptions, queryClient) {
|
|
309
|
-
const client = useQueryClient(queryClient?.());
|
|
310
|
-
const defaultedQueries = queriesOptions().queries.map(options => {
|
|
311
|
-
const defaultedOptions = client.defaultQueryOptions(options);
|
|
312
|
-
defaultedOptions._optimisticResults = 'optimistic';
|
|
313
|
-
return defaultedOptions;
|
|
314
|
-
});
|
|
315
|
-
const observer = new queryCore.QueriesObserver(client, defaultedQueries, queriesOptions().combine ? {
|
|
316
|
-
combine: queriesOptions().combine
|
|
317
|
-
} : undefined);
|
|
318
|
-
|
|
319
|
-
// @ts-expect-error - Types issue with solid-js createStore
|
|
320
|
-
const [state, setState] = store.createStore(observer.getOptimisticResult(defaultedQueries)[1]());
|
|
321
|
-
const unsubscribe = observer.subscribe(result => {
|
|
322
|
-
queryCore.notifyManager.batchCalls(() => {
|
|
323
|
-
setState(store.unwrap(result));
|
|
324
|
-
})();
|
|
325
|
-
});
|
|
326
|
-
solidJs.onCleanup(unsubscribe);
|
|
327
|
-
solidJs.createComputed(() => {
|
|
328
|
-
const updatedQueries = queriesOptions().queries.map(options => {
|
|
329
|
-
const defaultedOptions = client.defaultQueryOptions(options);
|
|
330
|
-
defaultedOptions._optimisticResults = 'optimistic';
|
|
331
|
-
return defaultedOptions;
|
|
332
|
-
});
|
|
333
|
-
observer.setQueries(updatedQueries, queriesOptions().combine ? {
|
|
334
|
-
combine: queriesOptions().combine
|
|
335
|
-
} : undefined, {
|
|
336
|
-
listeners: false
|
|
337
|
-
});
|
|
338
|
-
});
|
|
339
|
-
return state;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
exports.QueryClient = QueryClient;
|
|
343
|
-
exports.QueryClientContext = QueryClientContext;
|
|
344
|
-
exports.QueryClientProvider = QueryClientProvider;
|
|
345
|
-
exports.createInfiniteQuery = createInfiniteQuery;
|
|
346
|
-
exports.createMutation = createMutation;
|
|
347
|
-
exports.createQueries = createQueries;
|
|
348
|
-
exports.createQuery = createQuery;
|
|
349
|
-
exports.queryOptions = queryOptions;
|
|
350
|
-
exports.useIsFetching = useIsFetching;
|
|
351
|
-
exports.useIsMutating = useIsMutating;
|
|
352
|
-
exports.useQueryClient = useQueryClient;
|
|
353
|
-
Object.keys(queryCore).forEach(function (k) {
|
|
354
|
-
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
355
|
-
enumerable: true,
|
|
356
|
-
get: function () { return queryCore[k]; }
|
|
357
|
-
});
|
|
358
|
-
});
|
|
359
|
-
//# sourceMappingURL=index.cjs.map
|