fix: timeout sort

This commit is contained in:
Slinetrac
2025-10-17 14:51:33 +08:00
parent c2f59ffc02
commit c63584daca
2 changed files with 53 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
import { useEffect, useMemo, useReducer } from "react"; import { useEffect, useMemo, useReducer } from "react";
import { useVerge } from "@/hooks/use-verge";
import delayManager from "@/services/delay"; import delayManager from "@/services/delay";
// default | delay | alphabet // default | delay | alphabet
@@ -11,6 +12,7 @@ export default function useFilterSort(
filterText: string, filterText: string,
sortType: ProxySortType, sortType: ProxySortType,
) { ) {
const { verge } = useVerge();
const [_, bumpRefresh] = useReducer((count: number) => count + 1, 0); const [_, bumpRefresh] = useReducer((count: number) => count + 1, 0);
useEffect(() => { useEffect(() => {
@@ -32,9 +34,20 @@ export default function useFilterSort(
return useMemo(() => { return useMemo(() => {
const fp = filterProxies(proxies, groupName, filterText); const fp = filterProxies(proxies, groupName, filterText);
const sp = sortProxies(fp, groupName, sortType); const sp = sortProxies(
fp,
groupName,
sortType,
verge?.default_latency_timeout,
);
return sp; return sp;
}, [proxies, groupName, filterText, sortType]); }, [
proxies,
groupName,
filterText,
sortType,
verge?.default_latency_timeout,
]);
} }
export function filterSort( export function filterSort(
@@ -42,9 +55,10 @@ export function filterSort(
groupName: string, groupName: string,
filterText: string, filterText: string,
sortType: ProxySortType, sortType: ProxySortType,
latencyTimeout?: number,
) { ) {
const fp = filterProxies(proxies, groupName, filterText); const fp = filterProxies(proxies, groupName, filterText);
const sp = sortProxies(fp, groupName, sortType); const sp = sortProxies(fp, groupName, sortType, latencyTimeout);
return sp; return sp;
} }
@@ -102,23 +116,36 @@ function sortProxies(
proxies: IProxyItem[], proxies: IProxyItem[],
groupName: string, groupName: string,
sortType: ProxySortType, sortType: ProxySortType,
latencyTimeout?: number,
) { ) {
if (!proxies) return []; if (!proxies) return [];
if (sortType === 0) return proxies; if (sortType === 0) return proxies;
const list = proxies.slice(); const list = proxies.slice();
const effectiveTimeout =
typeof latencyTimeout === "number" && latencyTimeout > 0
? latencyTimeout
: 10000;
if (sortType === 1) { if (sortType === 1) {
const toSortableValue = (delay: number) => { const categorizeDelay = (delay: number): [number, number] => {
if (!Number.isFinite(delay) || delay <= 0) return Number.MAX_SAFE_INTEGER; if (!Number.isFinite(delay)) return [3, Number.MAX_SAFE_INTEGER];
return delay; if (delay > 1e5) return [4, delay];
if (delay < 0) return [2, Math.abs(delay)];
if (delay === 0 || (delay >= effectiveTimeout && delay <= 1e5)) {
return [3, delay || effectiveTimeout];
}
return [0, delay];
}; };
list.sort((a, b) => { list.sort((a, b) => {
const ad = toSortableValue(delayManager.getDelayFix(a, groupName)); const ad = delayManager.getDelayFix(a, groupName);
const bd = toSortableValue(delayManager.getDelayFix(b, groupName)); const bd = delayManager.getDelayFix(b, groupName);
const [ar, av] = categorizeDelay(ad);
const [br, bv] = categorizeDelay(bd);
return ad - bd; if (ar !== br) return ar - br;
return av - bv;
}); });
} else { } else {
list.sort((a, b) => a.name.localeCompare(b.name)); list.sort((a, b) => a.name.localeCompare(b.name));

View File

@@ -103,6 +103,7 @@ export const useRenderList = (
const { verge } = useVerge(); const { verge } = useVerge();
const { width } = useWindowWidth(); const { width } = useWindowWidth();
const [headStates, setHeadState] = useHeadStateNew(); const [headStates, setHeadState] = useHeadStateNew();
const latencyTimeout = verge?.default_latency_timeout;
// 获取运行时配置用于链式代理模式 // 获取运行时配置用于链式代理模式
const { data: runtimeConfig } = useSWR( const { data: runtimeConfig } = useSWR(
@@ -197,7 +198,13 @@ export const useRenderList = (
(g: any) => g.name === selectedGroup, (g: any) => g.name === selectedGroup,
); );
if (targetGroup) { if (targetGroup) {
const proxies = filterSort(targetGroup.all, targetGroup.name, "", 0); const proxies = filterSort(
targetGroup.all,
targetGroup.name,
"",
0,
latencyTimeout,
);
if (col > 1) { if (col > 1) {
return groupProxies(proxies, col).map((proxyCol, colIndex) => ({ return groupProxies(proxies, col).map((proxyCol, colIndex) => ({
@@ -226,7 +233,13 @@ export const useRenderList = (
// 如果没有选择特定组,显示第一个组的节点(如果有组的话) // 如果没有选择特定组,显示第一个组的节点(如果有组的话)
if (allGroups.length > 0) { if (allGroups.length > 0) {
const firstGroup = allGroups[0]; const firstGroup = allGroups[0];
const proxies = filterSort(firstGroup.all, firstGroup.name, "", 0); const proxies = filterSort(
firstGroup.all,
firstGroup.name,
"",
0,
latencyTimeout,
);
if (col > 1) { if (col > 1) {
return groupProxies(proxies, col).map((proxyCol, colIndex) => ({ return groupProxies(proxies, col).map((proxyCol, colIndex) => ({
@@ -391,6 +404,7 @@ export const useRenderList = (
group.name, group.name,
headState.filterText, headState.filterText,
headState.sortType, headState.sortType,
latencyTimeout,
); );
ret.push({ ret.push({
@@ -445,6 +459,7 @@ export const useRenderList = (
isChainMode, isChainMode,
runtimeConfig, runtimeConfig,
selectedGroup, selectedGroup,
latencyTimeout,
]); ]);
return { return {