mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-20 02:42:12 +08:00
perf(profile): derive editor policy names and dedupe pasted proxies
The groups editor re-read both profile files and re-parsed three YAML documents on every prepend/append/delete edit. The origin names are now read once per open and folded into a memoized policy list, and batch pasted proxy names dedupe through a Set instead of a linear scan.
This commit is contained in:
@@ -176,7 +176,10 @@ export const GroupsEditorViewer = (props: Props) => {
|
||||
},
|
||||
})
|
||||
const [groupList, setGroupList] = useState<IProxyGroupConfig[]>([])
|
||||
const [proxyPolicyList, setProxyPolicyList] = useState<string[]>([])
|
||||
const [originPolicy, setOriginPolicy] = useState<{
|
||||
proxyNames: string[]
|
||||
groupNames: string[]
|
||||
}>({ proxyNames: [], groupNames: [] })
|
||||
const [proxyProviderList, setProxyProviderList] = useState<string[]>([])
|
||||
const [prependSeq, setPrependSeq] = useState<IProxyGroupConfig[]>([])
|
||||
const [appendSeq, setAppendSeq] = useState<IProxyGroupConfig[]>([])
|
||||
@@ -353,6 +356,9 @@ export const GroupsEditorViewer = (props: Props) => {
|
||||
}
|
||||
}, [prependSeq, appendSeq, deleteSeq])
|
||||
|
||||
// Origin policy names come from profile files that this dialog never
|
||||
// rewrites while open, so they are read once per open; the live seq states
|
||||
// are folded in by the derived proxyPolicyList below.
|
||||
const fetchProxyPolicy = useCallback(async () => {
|
||||
const data = await readProfileFile(profileUid)
|
||||
const proxiesData = await readProfileFile(proxiesUid)
|
||||
@@ -388,17 +394,13 @@ export const GroupsEditorViewer = (props: Props) => {
|
||||
(name): name is string => typeof name === 'string' && name.length > 0,
|
||||
)
|
||||
|
||||
const computedPolicyList = builtinProxyPolicies.concat(
|
||||
prependSeq.map((group: IProxyGroupConfig) => group.name),
|
||||
(originGroupsObj?.['proxy-groups'] || [])
|
||||
.map((group: IProxyGroupConfig) => group.name)
|
||||
.filter((name) => !deleteSeq.includes(name)),
|
||||
appendSeq.map((group: IProxyGroupConfig) => group.name),
|
||||
setOriginPolicy({
|
||||
proxyNames,
|
||||
)
|
||||
|
||||
setProxyPolicyList(Array.from(new Set(computedPolicyList)))
|
||||
}, [appendSeq, deleteSeq, prependSeq, profileUid, proxiesUid])
|
||||
groupNames: (originGroupsObj?.['proxy-groups'] || []).map(
|
||||
(group) => group.name,
|
||||
),
|
||||
})
|
||||
}, [profileUid, proxiesUid])
|
||||
const fetchProfile = useCallback(async () => {
|
||||
const data = await readProfileFile(profileUid)
|
||||
const mergeData = await readProfileFile(mergeUid)
|
||||
@@ -442,6 +444,16 @@ export const GroupsEditorViewer = (props: Props) => {
|
||||
fetchProxyPolicy()
|
||||
}, [fetchProxyPolicy, open])
|
||||
|
||||
const proxyPolicyList = useMemo(() => {
|
||||
const computedPolicyList = builtinProxyPolicies.concat(
|
||||
prependSeq.map((group) => group.name),
|
||||
originPolicy.groupNames.filter((name) => !deleteSeq.includes(name)),
|
||||
appendSeq.map((group) => group.name),
|
||||
originPolicy.proxyNames,
|
||||
)
|
||||
return Array.from(new Set(computedPolicyList))
|
||||
}, [appendSeq, deleteSeq, originPolicy, prependSeq])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
fetchContent()
|
||||
|
||||
@@ -180,7 +180,7 @@ export const ProxiesEditorViewer = (props: Props) => {
|
||||
// 优化:异步分片解析,避免主线程阻塞,解析完成后批量setState
|
||||
const handleParseAsync = (cb: (proxies: IProxyConfig[]) => void) => {
|
||||
const proxies: IProxyConfig[] = []
|
||||
const names: string[] = []
|
||||
const names = new Set<string>()
|
||||
let uris: string
|
||||
try {
|
||||
uris = atob(proxyUri)
|
||||
@@ -198,9 +198,9 @@ export const ProxiesEditorViewer = (props: Props) => {
|
||||
const uri = lines[idx]
|
||||
try {
|
||||
const proxy = parseUri(uri.trim())
|
||||
if (!names.includes(proxy.name)) {
|
||||
if (!names.has(proxy.name)) {
|
||||
proxies.push(proxy)
|
||||
names.push(proxy.name)
|
||||
names.add(proxy.name)
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
|
||||
Reference in New Issue
Block a user