Skip to content

Commit 2c083ef

Browse files
committed
refactor(usePagination)!: does not support concurrent request
1 parent e605a3d commit 2c083ef

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/__tests__/pagination.test.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,20 @@ describe('usePagination', () => {
271271
expect(pageSizeEl.text()).toBe('10');
272272
expect(totalPageEl.text()).toBe('99');
273273
});
274+
275+
test('concurrent request should not work', async () => {
276+
const fn = jest.fn();
277+
try {
278+
usePagination(customPropertyApi, {
279+
// @ts-ignore
280+
queryKey: () => '1',
281+
});
282+
} catch (error) {
283+
expect(error.message).toBe(
284+
'usePagination does not support concurrent request',
285+
);
286+
fn();
287+
}
288+
expect(fn).toHaveBeenCalledTimes(1);
289+
});
274290
});

src/usePagination.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import generateService from './core/utils/generateService';
66
import { IService } from './core/utils/types';
77

88
export interface PaginationResult<R, P extends unknown[]>
9-
extends BaseResult<R, P> {
9+
extends Omit<BaseResult<R, P>, 'queries'> {
1010
current: Ref<number>;
1111
pageSize: Ref<number>;
1212
total: Ref<number>;
@@ -24,14 +24,16 @@ export interface PaginationExtendsOption {
2424
};
2525
}
2626

27-
export type PaginationFormatOptions<R, P extends unknown[], FR> = FormatOptions<
28-
R,
29-
P,
30-
FR
27+
export type PaginationFormatOptions<R, P extends unknown[], FR> = Omit<
28+
FormatOptions<R, P, FR>,
29+
'queryKey'
3130
> &
3231
PaginationExtendsOption;
3332

34-
export type PaginationBaseOptions<R, P extends unknown[]> = BaseOptions<R, P> &
33+
export type PaginationBaseOptions<R, P extends unknown[]> = Omit<
34+
BaseOptions<R, P>,
35+
'queryKey'
36+
> &
3537
PaginationExtendsOption;
3638

3739
export type PaginationMixinOptions<R, P extends unknown[], FR> =
@@ -66,18 +68,26 @@ function usePagination<R, P extends unknown[], FR>(
6668

6769
const {
6870
pagination: { currentKey, pageSizeKey, totalKey, totalPageKey },
71+
queryKey,
6972
...restOptions
7073
} = Object.assign(defaultOptions, options ?? ({} as any));
7174

72-
const { data, params, run, ...rest } = useAsyncQuery<R, P, FR>(promiseQuery, {
73-
defaultParams: [
74-
{
75-
[currentKey]: 1,
76-
[pageSizeKey]: 10,
77-
},
78-
],
79-
...restOptions,
80-
});
75+
if (queryKey) {
76+
throw new Error('usePagination does not support concurrent request');
77+
}
78+
79+
const { data, params, run, queries, ...rest } = useAsyncQuery<R, P, FR>(
80+
promiseQuery,
81+
{
82+
defaultParams: [
83+
{
84+
[currentKey]: 1,
85+
[pageSizeKey]: 10,
86+
},
87+
],
88+
...restOptions,
89+
},
90+
);
8191

8292
const paging = (paginationParams: Record<string, number>) => {
8393
const [oldPaginationParams, ...restParams] = params.value as P[];
@@ -113,13 +123,13 @@ function usePagination<R, P extends unknown[], FR>(
113123
return {
114124
data,
115125
params,
116-
run,
117-
changeCurrent,
118-
changePageSize,
119126
current,
120127
pageSize,
121128
total,
122129
totalPage,
130+
run,
131+
changeCurrent,
132+
changePageSize,
123133
...rest,
124134
};
125135
}

0 commit comments

Comments
 (0)