-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
292 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { UseCacheStoreOptions, useCacheStore } from '../lib/utils'; | ||
|
||
export type PaginationReq = { | ||
page: number; | ||
size: number; | ||
}; | ||
|
||
type PaginationRes<T> = | ||
| { | ||
list: T[]; | ||
// 总项目数 | ||
count: number; | ||
} | ||
| { | ||
list: T[]; | ||
// 总页数 | ||
total: number; | ||
}; | ||
|
||
type PageInit = { ids?: string[]; loading?: boolean }; | ||
|
||
export type PaginationStore<T> = { | ||
// 总页数 | ||
total: number; | ||
// 每页数据,只存 ID | ||
pagination: Partial<{ [page: number]: PageInit }>; | ||
// 每项数据 | ||
items: Partial<{ [id: string]: T }>; | ||
// 正在加载,避免重复请求 | ||
loader: Partial<Record<string, Promise<T>>>; | ||
}; | ||
|
||
type PaginationStoreOptions<T> = { | ||
// 缓存 key | ||
storageKey: string; | ||
// 指定 item 的唯一 key | ||
idKey?: keyof T; | ||
// 页面数据是否包含 item,如果包含,则不需要调用额外的 updateItem | ||
pageContainItem?: boolean; | ||
// 是否缓存 Item,如果不缓存,store.items 中的内容可能为空 | ||
cacheItems?: boolean; | ||
} & UseCacheStoreOptions<T>; | ||
|
||
export function createPaginationStore<T extends Record<string, any>>(options: PaginationStoreOptions<T>) { | ||
const { idKey = 'id', storageKey, cacheItems, pageContainItem, ...rest } = options; | ||
|
||
const cacheExcludeKeys: (keyof PaginationStore<T>)[] = ['loader']; | ||
if (cacheItems) cacheExcludeKeys.push('items'); | ||
|
||
const initStore: PaginationStore<T> = { | ||
total: 0, | ||
pagination: {}, | ||
items: {}, | ||
loader: {}, | ||
}; | ||
const [store, update, saveStore] = useCacheStore<PaginationStore<T>>(storageKey, initStore, { | ||
...rest, | ||
cacheExcludeKeys, | ||
}); | ||
|
||
const changePage = (page: number, content: Partial<PageInit>) => { | ||
store.pagination[page] = { ...store.pagination[page], ...content }; | ||
update(); | ||
}; | ||
|
||
const updatePage = async (request: (args: PaginationReq) => Promise<PaginationRes<any>>, args: PaginationReq) => { | ||
changePage(args.page, { loading: true }); | ||
try { | ||
const result = await request(args); | ||
changePage(args.page, { ids: result.list.map((e) => e[idKey]) }); | ||
|
||
if (pageContainItem) { | ||
result.list.forEach((e) => (store.items[e[idKey]] = e)); | ||
} | ||
|
||
update({ total: 'total' in result ? result.total : Math.ceil(result.count / args.size) }); | ||
} finally { | ||
changePage(args.page, { loading: false }); | ||
} | ||
}; | ||
|
||
const updateItem = async (request: (id: string) => Promise<T>, id: string) => { | ||
if (store.loader[id]) return; | ||
const loader = request(id); | ||
store.loader[id] = loader; | ||
update(); | ||
try { | ||
const item = await loader; | ||
store.items[item[idKey]] = item; | ||
update(); | ||
} finally { | ||
delete store.loader[id]; | ||
update(); | ||
} | ||
}; | ||
|
||
return { | ||
store, | ||
update, | ||
saveStore, | ||
updateItem, | ||
updatePage, | ||
}; | ||
} |
Oops, something went wrong.