Refactor Mihomo proxy groups component to optimize data handling and prevent rendering issues
- Utilized `untrack` from Svelte to avoid infinite rendering loops during asynchronous operations. - Improved loading state management by ensuring notifications are handled correctly without causing UI flickering. - Enhanced data fetching logic for proxy groups, ensuring stable access to test URLs and reducing potential delays in updates.
This commit is contained in:
@@ -53,9 +53,9 @@
|
||||
}
|
||||
|
||||
async function loadProxies() {
|
||||
const a = alias;
|
||||
const a = untrack(() => alias);
|
||||
if (!a) return;
|
||||
const notify = onLoadingChange;
|
||||
const notify = untrack(() => onLoadingChange);
|
||||
proxiesLoading = true;
|
||||
// Иначе $effect подпишется на проп-колбэк: новая fn каждый рендер родителя → бесконечный цикл /proxies
|
||||
untrack(() => notify?.(true));
|
||||
@@ -115,11 +115,11 @@
|
||||
}
|
||||
|
||||
async function pingOne(name: string) {
|
||||
const a = alias;
|
||||
const a = untrack(() => alias);
|
||||
if (!a) return;
|
||||
testing = name;
|
||||
try {
|
||||
const testUrl = proxiesData?.proxies?.[name]?.testUrl;
|
||||
const testUrl = untrack(() => proxiesData?.proxies?.[name]?.testUrl);
|
||||
await fetchMihomoJson(
|
||||
a,
|
||||
`proxies/${encodeURIComponent(name)}/delay?${delayQuery(testUrl)}`
|
||||
@@ -133,23 +133,35 @@
|
||||
}
|
||||
|
||||
async function pingGroup(groupName: string) {
|
||||
const a = alias;
|
||||
const a = untrack(() => alias);
|
||||
if (!a) return;
|
||||
// Снимок до await: иначе $effect(delayAllNonce) подпишется на proxiesData и
|
||||
// после loadProxies() перезапустится при том же nonce → бесконечные delay/ping.
|
||||
const snap = untrack(() => {
|
||||
const p = proxiesData?.proxies;
|
||||
const g = p?.[groupName];
|
||||
const names = g?.all ?? [];
|
||||
return {
|
||||
groupTestUrl: g?.testUrl,
|
||||
names,
|
||||
nodeTestUrls: names.map((n) => p?.[n]?.testUrl)
|
||||
};
|
||||
});
|
||||
testingGroup = groupName;
|
||||
const groupTestUrl = proxiesData?.proxies?.[groupName]?.testUrl;
|
||||
try {
|
||||
await fetchMihomoJson(
|
||||
a,
|
||||
`group/${encodeURIComponent(groupName)}/delay?${delayQuery(groupTestUrl)}`
|
||||
`group/${encodeURIComponent(groupName)}/delay?${delayQuery(snap.groupTestUrl)}`
|
||||
);
|
||||
await loadProxies();
|
||||
} catch {
|
||||
try {
|
||||
const g = proxiesData?.proxies?.[groupName];
|
||||
const names = g?.all ?? [];
|
||||
for (const n of names) {
|
||||
const u = proxiesData?.proxies?.[n]?.testUrl;
|
||||
await fetchMihomoJson(a, `proxies/${encodeURIComponent(n)}/delay?${delayQuery(u)}`);
|
||||
for (let i = 0; i < snap.names.length; i++) {
|
||||
const n = snap.names[i]!;
|
||||
await fetchMihomoJson(
|
||||
a,
|
||||
`proxies/${encodeURIComponent(n)}/delay?${delayQuery(snap.nodeTestUrls[i])}`
|
||||
);
|
||||
}
|
||||
await loadProxies();
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user