优化代理延迟测速时的 UI 显示 (#741)

Co-authored-by: lyc8503 <admin@lyc8503.site>
This commit is contained in:
lyc8503 2023-02-12 18:43:15 +08:00 committed by GitHub
parent 313a182a46
commit 49f0b9b50c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 31 deletions

View file

@ -3,14 +3,14 @@ import * as React from 'react';
import s0 from './ProxyLatency.module.scss'; import s0 from './ProxyLatency.module.scss';
type ProxyLatencyProps = { type ProxyLatencyProps = {
number?: number; number?: any;
color: string; color: string;
}; };
export function ProxyLatency({ number, color }: ProxyLatencyProps) { export function ProxyLatency({ number, color }: ProxyLatencyProps) {
return ( return (
<span className={s0.proxyLatency} style={{ color }}> <span className={s0.proxyLatency} style={{ color }}>
{typeof number === 'number' && number !== 0 ? number + ' ms' : ' '} {typeof number === 'number' ? (number !== 0 ? number + ' ms' : ' ') : number}
</span> </span>
); );
} }

View file

@ -270,12 +270,13 @@ function requestDelayForProxyOnce(apiConfig: ClashAPIConfig, name: string) {
return async (dispatch: DispatchFn, getState: GetStateFn) => { return async (dispatch: DispatchFn, getState: GetStateFn) => {
const latencyTestUrl = getLatencyTestUrl(getState()); const latencyTestUrl = getLatencyTestUrl(getState());
const res = await proxiesAPI.requestDelayForProxy(apiConfig, name, latencyTestUrl); const res = await proxiesAPI.requestDelayForProxy(apiConfig, name, latencyTestUrl);
let error = '';
if (res.ok === false) { if (res.ok === false) {
error = res.statusText; var delay: any = res.statusText;
} else {
var { delay } = await res.json();
} }
const { delay } = await res.json(); const delayNext = { ...getDelay(getState()), [name]: { number: delay } };
const delayNext = { ...getDelay(getState()), [name]: { error, number: delay } };
dispatch('requestDelayForProxyOnce', (s) => { dispatch('requestDelayForProxyOnce', (s) => {
s.proxies.delay = delayNext; s.proxies.delay = delayNext;
@ -291,33 +292,19 @@ export function requestDelayForProxy(apiConfig: ClashAPIConfig, name: string) {
export function requestDelayForProxies(apiConfig: ClashAPIConfig, names: string[]) { export function requestDelayForProxies(apiConfig: ClashAPIConfig, names: string[]) {
return async (dispatch: DispatchFn, getState: GetStateFn) => { return async (dispatch: DispatchFn, getState: GetStateFn) => {
const proxies = getProxies(getState());
const latencyTestUrl = getLatencyTestUrl(getState());
const proxyDedupMap = new Map<string, boolean>(); let delayClear = getDelay(getState())
const providerDedupMap = new Map<string, boolean>();
const works = names.map((name) => { for (var name of names) {
const p = proxies[name]; delayClear[name] = { number: "- ms" }
if (!p.__provider) { }
if (proxyDedupMap.get(name)) {
return undefined; dispatch('clearDelayBeforeTest', (s) => {
} else { s.proxies.delay = delayClear;
proxyDedupMap.set(name, true);
return proxiesAPI.requestDelayForProxy(apiConfig, name, latencyTestUrl);
}
} else if (p.__provider) {
// this one is from a proxy provider
if (providerDedupMap.get(p.__provider)) {
return undefined;
} else {
providerDedupMap.set(p.__provider, true);
return healthcheckProviderByNameInternal(apiConfig, p.__provider);
}
} else {
return undefined;
}
}); });
const works = names.map((name) => requestDelayForProxy(apiConfig, name)(dispatch))
await Promise.all(works); await Promise.all(works);
await dispatch(fetchProxies(apiConfig)); await dispatch(fetchProxies(apiConfig));
}; };

View file

@ -41,7 +41,7 @@ export type ProxyItem = {
}; };
export type ProxyDelayItem = { export type ProxyDelayItem = {
number?: number; number?: any;
}; };
export type ProxiesMapping = Record<string, ProxyItem>; export type ProxiesMapping = Record<string, ProxyItem>;