feat(frontend): 热榜翻页跟随服务端快照信号

服务端有三条重置信号。as_of 与请求不同、snapshot_rebuilt 为 true、ranking_source 与上一页不同。
任一条成立时,loadHot 以响应的 as_of 从 offset 0 重取一次。offset 在新榜上已失去意义。
否则按 next_offset 追加。用户因此不会看到重复的名次。
类型定义补上 ranking_source 与 snapshot_rebuilt。next_latest_* 标注废弃但保留。
验证:npm run build 通过。
This commit is contained in:
2026-11-01 15:15:36 +08:00
parent e2c2ec5454
commit 5137639a00
2 changed files with 28 additions and 6 deletions

View File

@@ -75,11 +75,22 @@ export type ListLikesCountResponse = {
export type ListByPopularityResponse = {
video_list: FeedVideoItem[]
/** 本次快照的标识(分钟右端,必为已闭合分钟)。翻页时原样带回。 */
as_of: number
next_offset: number
has_more: boolean
/**
* 这一页来自哪里:`hot` = Redis 点时快照,`hot_db` = MySQL 重算同一窗口,
* `latest` = 窗口内无互动、返回最新视频(终结页)。
*/
ranking_source: 'hot' | 'hot_db' | 'latest'
/** 服务端刚重建过快照:这一页可能与上一页重叠,应当从 offset 0 重来。 */
snapshot_rebuilt: boolean
/** @deprecated 旧游标字段,服务端不再返回,仅为兼容旧客户端保留声明。 */
next_latest_popularity?: number
/** @deprecated 见 next_latest_popularity。 */
next_latest_before?: string
/** @deprecated 见 next_latest_popularity。 */
next_latest_id_before?: number
}

View File

@@ -23,6 +23,7 @@ const state = reactive({
limit: 10,
asOf: 0,
nextOffset: 0,
rankingSource: '' as '' | 'hot' | 'hot_db' | 'latest',
})
const likeBusy = reactive<Record<string, boolean>>({})
@@ -32,15 +33,25 @@ async function loadHot(reset: boolean) {
state.loading = true
state.error = ''
try {
const res = await feedApi.listByPopularity({
limit: state.limit,
as_of: reset ? 0 : state.asOf,
offset: reset ? 0 : state.nextOffset,
})
const asOf = reset ? 0 : state.asOf
const offset = reset ? 0 : state.nextOffset
let res = await feedApi.listByPopularity({ limit: state.limit, as_of: asOf, offset })
// 服务端的三条重置信号:as_of 与请求不同(旧快照已过期,服务端换了新榜)、
// snapshot_rebuilt=true(快照刚被重建)、ranking_source 与上一页不同(换榜)。
// 任一条成立时 offset 不再指向这一页,只能把这一页当作新会话——从 0 重来。
let restart =
reset || res.as_of !== asOf || res.snapshot_rebuilt || res.ranking_source !== state.rankingSource
if (restart && offset > 0) {
res = await feedApi.listByPopularity({ limit: state.limit, as_of: res.as_of, offset: 0 })
restart = true
}
state.hasMore = res.has_more
state.asOf = res.as_of
state.rankingSource = res.ranking_source
state.nextOffset = res.next_offset
state.items = reset ? res.video_list : state.items.concat(res.video_list)
state.items = restart ? res.video_list : state.items.concat(res.video_list)
} catch (e) {
state.error = e instanceof ApiError ? e.message : String(e)
} finally {