Peach: Local-first video metadata scraper and media manager.
fix(catalog-filter): 换名单时旧的续页请求不再挡住新名单续页
longmeidao104ff4975f04
diff --git a/frontend/src/react/catalog-filter/catalog-filter-page.tsx b/frontend/src/react/catalog-filter/catalog-filter-page.tsxindex 458acea16..70eb45c50 100644--- a/frontend/src/react/catalog-filter/catalog-filter-page.tsx+++ b/frontend/src/react/catalog-filter/catalog-filter-page.tsx@@ -245,7 +245,7 @@ function usePaged<T>(row: RefObject<HTMLElement | null>, items: T[], key: stringif (current !== page) setPage(current);const all = useMemo(() => (current.fetched.length ? items.concat(current.fetched) : items),[items, current.fetched]);- const fetching = useRef(false);+ const fetching = useRef<string | null>(null);const state = useRef({ current, all, more });state.current = { current, all, more };const fill = useCallback(() => {@@ -256,11 +256,12 @@ function usePaged<T>(row: RefObject<HTMLElement | null>, items: T[], key: stringsetPage((p) => (p.key === now.key ? { ...p, limit: p.limit + ROW_BATCH } : p));return;}- if (now.drained || !next || fetching.current) return;- fetching.current = true;- /* 要下一页的这段时间里人还在滚,`fetching` 挡住重入,免得同一页要两遍。 */+ if (now.drained || !next || fetching.current === now.key) return;+ fetching.current = now.key;+ /* 要下一页的这段时间里人还在滚,`fetching` 挡住同一份名单重入,免得同一页要两遍。按 `key` 记:+ * 旧名单那一页还在路上时名单换了,新名单照样续页,不然旧请求回来被丢掉后这一排就停在第一页。 */void next().catch(() => []).then((rows) => {- fetching.current = false;+ if (fetching.current === now.key) fetching.current = null;setPage((p) => {if (p.key !== now.key) return p;return rows.length ? { ...p, fetched: p.fetched.concat(rows as T[]) } : { ...p, drained: true };diff --git a/frontend/test/react/catalog-filter.test.tsx b/frontend/test/react/catalog-filter.test.tsxindex 0e2bd4c24..7fa294ac1 100644--- a/frontend/test/react/catalog-filter.test.tsx+++ b/frontend/test/react/catalog-filter.test.tsx@@ -4,8 +4,8 @@import { describe, expect, it, vi } from 'vitest';import { CatalogFilterPage } from '../../src/react/catalog-filter/catalog-filter-page';-import { EMPTY_SLOTS, type CatalogFilterProps } from '../../src/react/catalog-filter/catalog-filter';-import { mount } from './render';+import { EMPTY_SLOTS, type CatalogFilterProps, type TierPerformer } from '../../src/react/catalog-filter/catalog-filter';+import { mount, mountRoot, pending, settle } from './render';function props(patch: Partial<CatalogFilterProps> = {}): CatalogFilterProps {return {@@ -43,3 +43,31 @@ describe('空馆藏', () => {expect(host.querySelector('[data-catalog-frame]')).toBeNull();});});++describe('续页', () => {+ it('上一份名单要的那页还在路上时换了名单,新名单照样续页,旧的那页回来不进这一排', async () => {+ // 一排一枚铺不满一行:没溢出就一直停在右端,每次画完都要下一页。+ vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(1000);+ vi.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockReturnValue(1000);+ const stale = pending<TierPerformer[]>();+ const moreTops = vi.fn<CatalogFilterProps['actions']['moreTops']>()+ .mockReturnValueOnce(stale.answer)+ .mockResolvedValueOnce([{ name: '新二', ringHtml: '' }])+ .mockResolvedValue([]);+ const tiers = (key: string, name: string) => ({ key, performers: [{ name, ringHtml: '' }], studios: [] });+ const base = props();+ const page = (key: string, name: string) =>+ <CatalogFilterPage {...base} tiers={tiers(key, name)} actions={{ ...base.actions, moreTops }} />;+ const { host, rerender } = await mountRoot(page('旧', '旧一'));+ expect(moreTops).toHaveBeenCalledTimes(1);++ await rerender(page('新', '新一'));+ await settle();+ await stale.release([{ name: '旧二', ringHtml: '' }]);+ await settle();++ const names = [...host.querySelectorAll('[data-tier-performer]')].map((node) => node.getAttribute('data-entity-name'));+ expect(names).toEqual(['新一', '新二']);+ expect(moreTops).toHaveBeenCalledTimes(3);+ });+});diff --git a/frontend/test/react/render.tsx b/frontend/test/react/render.tsxindex d3aaca90a..bc58dc407 100644--- a/frontend/test/react/render.tsx+++ b/frontend/test/react/render.tsx@@ -14,8 +14,10 @@ afterEach(() => {vi.useRealTimers();});-/** 挂一棵根并把卸载留给用例:用例要看卸载之后还发不发请求时用它。 */-export async function mountRoot(element: ReactElement): Promise<{ host: HTMLElement; unmount(): Promise<void> }> {+/** 挂一棵根并把重画与卸载留给用例:用例要换一份 props 重画,或看卸载之后还发不发请求时用它。 */+export async function mountRoot(element: ReactElement): Promise<{+ host: HTMLElement; rerender(next: ReactElement): Promise<void>; unmount(): Promise<void>;+}> {const host = document.createElement('div');host.className = 'peach-react';document.body.append(host);@@ -24,6 +26,7 @@ export async function mountRoot(element: ReactElement): Promise<{ host: HTMLElemawait act(async () => root.render(element));return {host,+ rerender: async (next) => { await act(async () => root.render(next)) },unmount: async () => {const at = roots.indexOf(root);if (at >= 0) roots.splice(at, 1);diff --git a/web/dist/peach-react.js b/web/dist/peach-react.jsindex ba16d5d73..bbf0f8c01 100644--- a/web/dist/peach-react.js+++ b/web/dist/peach-react.js@@ -27820,7 +27820,7 @@ function LC(e, t, n, r, i) {drained: !i}), [o, s] = (0, B.useState)(a), c = o.key === n ? o : a();c !== o && s(c);- let l = (0, B.useMemo)(() => c.fetched.length ? t.concat(c.fetched) : t, [t, c.fetched]), u = (0, B.useRef)(!1), d = (0, B.useRef)({+ let l = (0, B.useMemo)(() => c.fetched.length ? t.concat(c.fetched) : t, [t, c.fetched]), u = (0, B.useRef)(null), d = (0, B.useRef)({current: c,all: l,more: i@@ -27840,8 +27840,8 @@ function LC(e, t, n, r, i) {} : e);return;}- n.drained || !i || u.current || (u.current = !0, i().catch(() => []).then((e) => {- u.current = !1, s((t) => t.key === n.key ? e.length ? {+ n.drained || !i || u.current === n.key || (u.current = n.key, i().catch(() => []).then((e) => {+ u.current === n.key && (u.current = null), s((t) => t.key === n.key ? e.length ? {...t,fetched: t.fetched.concat(e)} : {