Skip to content

Commit efd6382

Browse files
authored
Merge pull request #189 from cnblogs/fix-state-is-undefined
fix: 'State is undefined'
2 parents 8271910 + 1fc294e commit efd6382

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

src/cmd/post-list/post-list-view.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,9 @@ async function goPage(f: (currentIndex: number) => number) {
2929
if (isRefreshing) return
3030

3131
const state = getListState()
32-
if (state === undefined) {
33-
void Alert.warn('操作失败: 状态错误')
34-
return
35-
}
36-
37-
const index = f(state.pageIndex)
38-
if (!isPageIndexInRange(index, state)) {
39-
void Alert.warn(`操作失败: 已达到最大页数`)
40-
return
41-
}
42-
43-
await updatePostListState(index, state.pageCap, state.pageItemCount, state.pageCount)
44-
await PostListView.refresh()
32+
let pageIndex = state?.pageIndex ?? 1
33+
pageIndex = f(pageIndex)
34+
await PostListView.refresh({ pageIndex: pageIndex })
4535
}
4636

4737
function isPageIndexInRange(pageIndex: number, state: PostListState) {
@@ -65,7 +55,7 @@ function updatePostListViewTitle() {
6555
export namespace PostListView {
6656
import calcPageCount = PageList.calcPageCount
6757

68-
export async function refresh({ queue = false } = {}): Promise<boolean> {
58+
export async function refresh({ queue = false, pageIndex = 1 } = {}): Promise<boolean> {
6959
if (isRefreshing && !queue) {
7060
await refreshTask
7161
return false
@@ -76,10 +66,9 @@ export namespace PostListView {
7666

7767
const fut = async () => {
7868
await setRefreshing(true)
79-
const page = await postDataProvider.loadPosts()
69+
const page = await postDataProvider.loadPosts(pageIndex)
8070
const postCount = await PostService.getCount()
8171
const pageCount = calcPageCount(page.cap, postCount)
82-
const pageIndex = page.index
8372
const hasPrev = PageList.hasPrev(pageIndex)
8473
const hasNext = PageList.hasNext(pageIndex, pageCount)
8574

@@ -113,7 +102,7 @@ export namespace PostListView {
113102
if (isNaN(n) || n === 0) return '请输入正确格式的页码'
114103

115104
const state = getListState()
116-
if (state === undefined) return '博文列表尚未加载'
105+
if (state === undefined) return undefined
117106

118107
if (isPageIndexInRange(n, state)) return undefined
119108

src/service/post/post-list-view.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ export async function revealPostListItem(
2828
}
2929

3030
export function getListState() {
31-
const state = <PostListState | undefined>LocalState.getState('postListState')
32-
if (state === undefined) throw Error('State is undefined')
33-
return state
31+
return <PostListState | undefined>LocalState.getState('postListState')
3432
}
3533

3634
export async function updatePostListState(

src/tree-view/provider/post-data-provider.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { PostTreeItem } from '@/tree-view/model/post-tree-item'
99
import { PostListCfg } from '@/ctx/cfg/post-list'
1010
import { Page } from '@/model/page'
1111
import { PostListView } from '@/cmd/post-list/post-list-view'
12-
import { getListState } from '@/service/post/post-list-view'
1312

1413
export type PostListTreeItem = Post | PostTreeItem | TreeItem | PostMetadata | PostSearchResultEntry
1514

@@ -53,8 +52,7 @@ export class PostDataProvider implements TreeDataProvider<PostListTreeItem> {
5352
return toTreeItem(item)
5453
}
5554

56-
async loadPosts(): Promise<Page<Post>> {
57-
const { pageIndex } = getListState() ?? {}
55+
async loadPosts(pageIndex: number): Promise<Page<Post>> {
5856
const pageSize = PostListCfg.getListPageSize()
5957

6058
try {

0 commit comments

Comments
 (0)