修复:页面切换先响应点击,后台合并校验避免网络阻塞
This commit is contained in:
@@ -23,39 +23,51 @@ export const CachedWorkspacePages = forwardRef<WorkspaceNavigation, {
|
|||||||
const entries = useRef(new Map<PageKey, Entry>([[page, { initialSearch: location.search, search: location.search, key: 0, scroll: 0 }]]));
|
const entries = useRef(new Map<PageKey, Entry>([[page, { initialSearch: location.search, search: location.search, key: 0, scroll: 0 }]]));
|
||||||
const current = useRef(page), generation = useRef(0), key = useRef(0);
|
const current = useRef(page), generation = useRef(0), key = useRef(0);
|
||||||
const revisions = useRef<{ authRevision: string; dataRevision: string } | null>(null);
|
const revisions = useRef<{ authRevision: string; dataRevision: string } | null>(null);
|
||||||
|
const validation = useRef<Promise<boolean> | null>(null);
|
||||||
const [refreshKey, setRefreshKey] = useState(0), [, render] = useState(0);
|
const [refreshKey, setRefreshKey] = useState(0), [, render] = useState(0);
|
||||||
const [ready, setReady] = useState(false), [error, setError] = useState('');
|
const [ready, setReady] = useState(false), [error, setError] = useState('');
|
||||||
const alive = useRef(true);
|
const alive = useRef(true);
|
||||||
const validate = async (currentSelection = () => true) => {
|
const validate = () => {
|
||||||
const state = await api.workspaceState();
|
if (validation.current) return validation.current;
|
||||||
if (!alive.current || !currentSelection()) return false;
|
const epoch = generation.current;
|
||||||
if (revisions.current && state.authRevision !== revisions.current.authRevision) {
|
const request = (async () => {
|
||||||
// 权限改变后销毁旧缓存,服务端随后按最新身份重新查询。
|
try {
|
||||||
const latest = await api.me();
|
const state = await api.workspaceState();
|
||||||
if (!alive.current || !currentSelection()) return false;
|
if (!alive.current || generation.current !== epoch) return false;
|
||||||
onUserChange(latest); entries.current.clear();
|
if (revisions.current && state.authRevision !== revisions.current.authRevision) {
|
||||||
}
|
const latest = await api.me();
|
||||||
if (revisions.current && state.dataRevision !== revisions.current.dataRevision) setRefreshKey(value => value + 1);
|
if (!alive.current || generation.current !== epoch) return false;
|
||||||
revisions.current = state;
|
// 权限改变时清除全部旧缓存,重建此刻正在查看的页面;校验不负责切换路由。
|
||||||
return true;
|
onUserChange(latest); entries.current.clear();
|
||||||
|
entries.current.set(current.current, { initialSearch: location.search, search: location.search, key: ++key.current, scroll: 0 });
|
||||||
|
render(value => value + 1);
|
||||||
|
}
|
||||||
|
if (revisions.current && state.dataRevision !== revisions.current.dataRevision) setRefreshKey(value => value + 1);
|
||||||
|
revisions.current = state;
|
||||||
|
setError('');
|
||||||
|
return true;
|
||||||
|
} catch (value) {
|
||||||
|
if (alive.current && generation.current === epoch && !onUnauthorized(value)) setError(value instanceof Error ? value.message : '页面状态校验失败,请重试');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
validation.current = request;
|
||||||
|
void request.finally(() => { if (validation.current === request) validation.current = null; });
|
||||||
|
return request;
|
||||||
};
|
};
|
||||||
const remember = (updateSearch = true) => {
|
const remember = (updateSearch = true) => {
|
||||||
const entry = entries.current.get(current.current);
|
const entry = entries.current.get(current.current);
|
||||||
if (entry) { if (updateSearch) entry.search = location.search; entry.scroll = window.scrollY; }
|
if (entry) { if (updateSearch) entry.search = location.search; entry.scroll = window.scrollY; }
|
||||||
};
|
};
|
||||||
const enter = async (next: PageKey, search: string, force = false, pop = false) => {
|
const enter = (next: PageKey, search: string, force = false, pop = false) => {
|
||||||
remember(!pop);
|
remember(!pop);
|
||||||
const selection = ++generation.current;
|
const previous = entries.current.get(next);
|
||||||
try {
|
if (!previous || force) entries.current.set(next, { initialSearch: search, search, key: ++key.current, scroll: 0 });
|
||||||
if (!await validate(() => generation.current === selection) || generation.current !== selection) return;
|
const entry = entries.current.get(next)!;
|
||||||
const previous = entries.current.get(next);
|
if (!pop) history.pushState({}, '', `${location.pathname}${entry.search}`);
|
||||||
if (!previous || force) entries.current.set(next, { initialSearch: search, search, key: ++key.current, scroll: 0 });
|
current.current = next; onPageChange(next); render(value => value + 1);
|
||||||
const entry = entries.current.get(next)!;
|
// 本地页面先响应点击,网络校验在后台进行;连续切换共享尚未结束的请求。
|
||||||
if (!pop) history.pushState({}, '', `${location.pathname}${entry.search}`);
|
void validate();
|
||||||
current.current = next; setError(''); onPageChange(next); render(value => value + 1);
|
|
||||||
} catch (value) {
|
|
||||||
if (alive.current && generation.current === selection && !onUnauthorized(value)) setError(value instanceof Error ? value.message : '页面状态校验失败,请重试');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
const navigate = (next: PageKey, query?: QueryParams, resultRef?: ResultRef) => {
|
const navigate = (next: PageKey, query?: QueryParams, resultRef?: ResultRef) => {
|
||||||
if (next === 'imports') next = 'sources';
|
if (next === 'imports') next = 'sources';
|
||||||
@@ -72,19 +84,20 @@ export const CachedWorkspacePages = forwardRef<WorkspaceNavigation, {
|
|||||||
} }));
|
} }));
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
alive.current = true;
|
alive.current = true;
|
||||||
void validate().then(ok => { if (ok) setReady(true); }).catch(value => { if (alive.current && !onUnauthorized(value)) setError(String(value)); });
|
void validate().then(ok => { if (ok) setReady(true); });
|
||||||
const invalidate = () => setRefreshKey(value => value + 1);
|
const invalidate = () => setRefreshKey(value => value + 1);
|
||||||
window.addEventListener(WORKSPACE_INVALIDATED, invalidate);
|
window.addEventListener(WORKSPACE_INVALIDATED, invalidate);
|
||||||
return () => { alive.current = false; generation.current++; window.removeEventListener(WORKSPACE_INVALIDATED, invalidate); };
|
return () => { alive.current = false; generation.current++; validation.current = null; window.removeEventListener(WORKSPACE_INVALIDATED, invalidate); };
|
||||||
}, []);
|
}, []);
|
||||||
|
const activeEntryKey = entries.current.get(page)?.key;
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
if (!ready) return;
|
if (!ready) return;
|
||||||
const frame = requestAnimationFrame(() => window.scrollTo(0, entries.current.get(page)?.scroll || 0));
|
const frame = requestAnimationFrame(() => window.scrollTo(0, entries.current.get(page)?.scroll || 0));
|
||||||
return () => cancelAnimationFrame(frame);
|
return () => cancelAnimationFrame(frame);
|
||||||
}, [page, ready]);
|
}, [page, ready, activeEntryKey]);
|
||||||
if (error) return <Alert type="error" showIcon message="页面暂不可用" description={error} action={<Button onClick={() => location.reload()}>重新连接</Button>} />;
|
const connectionNotice = error && <Alert type="warning" showIcon message="页面状态暂未更新" description={error} action={<Button onClick={() => { void validate().then(ok => { if (ok) setReady(true); }); }}>重新连接</Button>} />;
|
||||||
if (!ready) return <div className="loading-panel"><Spin />正在打开工作台…</div>;
|
if (!ready) return connectionNotice || <div className="loading-panel"><Spin />正在打开工作台…</div>;
|
||||||
return <>{Array.from(entries.current.entries()).filter(([name]) => retained(name) || name === page).map(([name, entry]) => {
|
return <>{connectionNotice}{Array.from(entries.current.entries()).filter(([name]) => retained(name) || name === page).map(([name, entry]) => {
|
||||||
const active = name === page;
|
const active = name === page;
|
||||||
return <div key={`${name}-${entry.key}`} className="page-view" data-page={name} hidden={!active} style={!active ? { display: 'none' } : undefined}>
|
return <div key={`${name}-${entry.key}`} className="page-view" data-page={name} hidden={!active} style={!active ? { display: 'none' } : undefined}>
|
||||||
<PageCacheContext.Provider value={{ active, initialSearch: entry.initialSearch, refreshKey, onLocationChange: search => { entry.search = search; } }}>
|
<PageCacheContext.Provider value={{ active, initialSearch: entry.initialSearch, refreshKey, onLocationChange: search => { entry.search = search; } }}>
|
||||||
|
|||||||
Reference in New Issue
Block a user