Fix: make proxy change selected faster

This commit is contained in:
Dreamacro 2020-08-17 14:19:49 +08:00
parent 1c45cc16e6
commit 70606e5ac4
2 changed files with 15 additions and 5 deletions

View File

@ -10,14 +10,14 @@ interface GroupProps {
} }
export function Group (props: GroupProps) { export function Group (props: GroupProps) {
const { update } = useProxy() const { markProxySelected } = useProxy()
const proxyMap = useRecoilValue(proxyMapping) const proxyMap = useRecoilValue(proxyMapping)
const { data: Config } = useConfig() const { data: Config } = useConfig()
const { config } = props const { config } = props
async function handleChangeProxySelected (name: string) { async function handleChangeProxySelected (name: string) {
await changeProxySelected(props.config.name, name) await changeProxySelected(props.config.name, name)
await update() markProxySelected(props.config.name, name)
if (Config.breakConnections) { if (Config.breakConnections) {
const list: string[] = [] const list: string[] = []
const snapshot = await getConnections() const snapshot = await getConnections()

View File

@ -170,7 +170,7 @@ export const proxies = atom({
export function useProxy () { export function useProxy () {
const [allProxy, set] = useRecoilObjectWithImmer(proxies) const [allProxy, set] = useRecoilObjectWithImmer(proxies)
const { data, mutate } = swr('/proxies', async () => { const { mutate } = swr(['/proxies', set], async () => {
const allProxies = await API.getProxies() const allProxies = await API.getProxies()
const global = allProxies.data.proxies.GLOBAL as API.Group const global = allProxies.data.proxies.GLOBAL as API.Group
@ -183,15 +183,25 @@ export function useProxy () {
.filter(key => !unUsedProxy.has(key)) .filter(key => !unUsedProxy.has(key))
.map(key => ({ ...allProxies.data.proxies[key], name: key })) .map(key => ({ ...allProxies.data.proxies[key], name: key }))
const [proxy, groups] = partition(proxies, proxy => !policyGroup.has(proxy.type)) const [proxy, groups] = partition(proxies, proxy => !policyGroup.has(proxy.type))
return { proxies: proxy as API.Proxy[], groups: groups as API.Group[], global: global } set({ proxies: proxy as API.Proxy[], groups: groups as API.Group[], global: global })
}) })
useEffect(() => data && set(data), [data, set])
const markProxySelected = useCallback((name: string, selected: string) => {
set(draft => {
for (const group of draft.groups) {
if (group.name === name) {
group.now = selected
}
}
})
}, [set])
return { return {
proxies: allProxy.proxies, proxies: allProxy.proxies,
groups: allProxy.groups, groups: allProxy.groups,
global: allProxy.global, global: allProxy.global,
update: mutate, update: mutate,
markProxySelected,
set set
} }
} }