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() {
|
async function loadProxies() {
|
||||||
const a = alias;
|
const a = untrack(() => alias);
|
||||||
if (!a) return;
|
if (!a) return;
|
||||||
const notify = onLoadingChange;
|
const notify = untrack(() => onLoadingChange);
|
||||||
proxiesLoading = true;
|
proxiesLoading = true;
|
||||||
// Иначе $effect подпишется на проп-колбэк: новая fn каждый рендер родителя → бесконечный цикл /proxies
|
// Иначе $effect подпишется на проп-колбэк: новая fn каждый рендер родителя → бесконечный цикл /proxies
|
||||||
untrack(() => notify?.(true));
|
untrack(() => notify?.(true));
|
||||||
@@ -115,11 +115,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function pingOne(name: string) {
|
async function pingOne(name: string) {
|
||||||
const a = alias;
|
const a = untrack(() => alias);
|
||||||
if (!a) return;
|
if (!a) return;
|
||||||
testing = name;
|
testing = name;
|
||||||
try {
|
try {
|
||||||
const testUrl = proxiesData?.proxies?.[name]?.testUrl;
|
const testUrl = untrack(() => proxiesData?.proxies?.[name]?.testUrl);
|
||||||
await fetchMihomoJson(
|
await fetchMihomoJson(
|
||||||
a,
|
a,
|
||||||
`proxies/${encodeURIComponent(name)}/delay?${delayQuery(testUrl)}`
|
`proxies/${encodeURIComponent(name)}/delay?${delayQuery(testUrl)}`
|
||||||
@@ -133,23 +133,35 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function pingGroup(groupName: string) {
|
async function pingGroup(groupName: string) {
|
||||||
const a = alias;
|
const a = untrack(() => alias);
|
||||||
if (!a) return;
|
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;
|
testingGroup = groupName;
|
||||||
const groupTestUrl = proxiesData?.proxies?.[groupName]?.testUrl;
|
|
||||||
try {
|
try {
|
||||||
await fetchMihomoJson(
|
await fetchMihomoJson(
|
||||||
a,
|
a,
|
||||||
`group/${encodeURIComponent(groupName)}/delay?${delayQuery(groupTestUrl)}`
|
`group/${encodeURIComponent(groupName)}/delay?${delayQuery(snap.groupTestUrl)}`
|
||||||
);
|
);
|
||||||
await loadProxies();
|
await loadProxies();
|
||||||
} catch {
|
} catch {
|
||||||
try {
|
try {
|
||||||
const g = proxiesData?.proxies?.[groupName];
|
for (let i = 0; i < snap.names.length; i++) {
|
||||||
const names = g?.all ?? [];
|
const n = snap.names[i]!;
|
||||||
for (const n of names) {
|
await fetchMihomoJson(
|
||||||
const u = proxiesData?.proxies?.[n]?.testUrl;
|
a,
|
||||||
await fetchMihomoJson(a, `proxies/${encodeURIComponent(n)}/delay?${delayQuery(u)}`);
|
`proxies/${encodeURIComponent(n)}/delay?${delayQuery(snap.nodeTestUrls[i])}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
await loadProxies();
|
await loadProxies();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user