Skip to content

Commit fe595d7

Browse files
committed
feat(jike-client): add lastKey, onDone
1 parent 8cf9922 commit fe595d7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/client/utils/paginate.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export interface PaginatedOption<
55
L extends keyof LimitOptionAll = keyof LimitOptionAll,
66
K = unknown
77
> {
8+
/** 上一页的 key */
9+
lastKey?: K
10+
811
/** @returns `true`: 继续下一页;`false`: 停止获取 */
912
limit?: LimitFn<L | 'total', T>
1013
/**
@@ -16,6 +19,11 @@ export interface PaginatedOption<
1619
key: K | undefined,
1720
data: T[]
1821
) => boolean | void
22+
/**
23+
* 当结束时触发
24+
* @returns `false`: 将停止获取下一页
25+
*/
26+
onDone?: (totalPage: number, key: K | undefined, data: T[]) => boolean | void
1927
}
2028

2129
export type PaginatedFetcher<T, K> = (
@@ -31,7 +39,7 @@ export const fetchPaginated = async <
3139
limitOptionGetter: (item: T, data: T[]) => LimitOption<L | 'total'>,
3240
option: PaginatedOption<T, L, K>
3341
) => {
34-
let lastKey: K | undefined = undefined
42+
let lastKey: K | undefined = option.lastKey
3543
const data: T[] = []
3644
let isContinue = true
3745
let page = 1
@@ -58,5 +66,7 @@ export const fetchPaginated = async <
5866
page++
5967
} while (isContinue && lastKey)
6068

69+
option.onDone?.(page, lastKey, data)
70+
6171
return data
6272
}

0 commit comments

Comments
 (0)