Skip to content

Commit

Permalink
refactor(usePagination)!: does not support concurrent request
Browse files Browse the repository at this point in the history
  • Loading branch information
John60676 committed Feb 25, 2021
1 parent e605a3d commit 2c083ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,20 @@ describe('usePagination', () => {
expect(pageSizeEl.text()).toBe('10');
expect(totalPageEl.text()).toBe('99');
});

test('concurrent request should not work', async () => {
const fn = jest.fn();
try {
usePagination(customPropertyApi, {
// @ts-ignore
queryKey: () => '1',
});
} catch (error) {
expect(error.message).toBe(
'usePagination does not support concurrent request',
);
fn();
}
expect(fn).toHaveBeenCalledTimes(1);
});
});
46 changes: 28 additions & 18 deletions src/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import generateService from './core/utils/generateService';
import { IService } from './core/utils/types';

export interface PaginationResult<R, P extends unknown[]>
extends BaseResult<R, P> {
extends Omit<BaseResult<R, P>, 'queries'> {
current: Ref<number>;
pageSize: Ref<number>;
total: Ref<number>;
Expand All @@ -24,14 +24,16 @@ export interface PaginationExtendsOption {
};
}

export type PaginationFormatOptions<R, P extends unknown[], FR> = FormatOptions<
R,
P,
FR
export type PaginationFormatOptions<R, P extends unknown[], FR> = Omit<
FormatOptions<R, P, FR>,
'queryKey'
> &
PaginationExtendsOption;

export type PaginationBaseOptions<R, P extends unknown[]> = BaseOptions<R, P> &
export type PaginationBaseOptions<R, P extends unknown[]> = Omit<
BaseOptions<R, P>,
'queryKey'
> &
PaginationExtendsOption;

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

const {
pagination: { currentKey, pageSizeKey, totalKey, totalPageKey },
queryKey,
...restOptions
} = Object.assign(defaultOptions, options ?? ({} as any));

const { data, params, run, ...rest } = useAsyncQuery<R, P, FR>(promiseQuery, {
defaultParams: [
{
[currentKey]: 1,
[pageSizeKey]: 10,
},
],
...restOptions,
});
if (queryKey) {
throw new Error('usePagination does not support concurrent request');
}

const { data, params, run, queries, ...rest } = useAsyncQuery<R, P, FR>(
promiseQuery,
{
defaultParams: [
{
[currentKey]: 1,
[pageSizeKey]: 10,
},
],
...restOptions,
},
);

const paging = (paginationParams: Record<string, number>) => {
const [oldPaginationParams, ...restParams] = params.value as P[];
Expand Down Expand Up @@ -113,13 +123,13 @@ function usePagination<R, P extends unknown[], FR>(
return {
data,
params,
run,
changeCurrent,
changePageSize,
current,
pageSize,
total,
totalPage,
run,
changeCurrent,
changePageSize,
...rest,
};
}
Expand Down

0 comments on commit 2c083ef

Please sign in to comment.