From 4cc5eeb01f8b460f78b0705141f19423256b6ef7 Mon Sep 17 00:00:00 2001 From: kunish Date: Tue, 19 Sep 2023 20:24:13 +0800 Subject: [PATCH] refactor: hoist the websocket connection of `connections` to global App level --- src/App.tsx | 43 +++++++++++++++++++++++++------------- src/signals/config.ts | 14 +------------ src/signals/connections.ts | 14 +++---------- src/signals/proxies.ts | 12 +++++------ 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 7d7dbd0..1106ba5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,3 +1,4 @@ +import { usePrefersDark } from '@solid-primitives/media' import { Navigate, Route, Routes, useNavigate } from '@solidjs/router' import { Show, createEffect, lazy, onMount } from 'solid-js' import { Toaster } from 'solid-toast' @@ -6,12 +7,13 @@ import { Header } from '~/components' import { ROUTES } from '~/constants' import { WsMsg, + autoSwitchTheme, curTheme, endpoint, - selectedEndpoint, - setAllConnections, + favDayTheme, + favNightTheme, + setCurTheme, setLatestConnectionMsg, - useAutoSwitchTheme, useProxies, useTwemoji, useWsRequest, @@ -25,21 +27,33 @@ const Proxies = lazy(() => import('~/pages/Proxies')) const Rules = lazy(() => import('~/pages/Rules')) const Config = lazy(() => import('~/pages/Config')) +const ProtectedResources = () => { + const { fetchProxies } = useProxies() + + onMount(fetchProxies) + + const latestConnectionMsg = useWsRequest('connections') + + createEffect(() => { + setLatestConnectionMsg(latestConnectionMsg()) + }) + + return null +} + export const App = () => { const navigate = useNavigate() - useAutoSwitchTheme() + const prefersDark = usePrefersDark() createEffect(() => { - if (selectedEndpoint() && endpoint()) { - void useProxies().updateProxies() - setAllConnections([]) - setLatestConnectionMsg(useWsRequest('connections')) + if (autoSwitchTheme()) { + setCurTheme(prefersDark() ? favNightTheme() : favDayTheme()) } }) onMount(() => { - if (!selectedEndpoint()) { + if (!endpoint()) { navigate(ROUTES.Setup) } }) @@ -56,7 +70,7 @@ export const App = () => {
- + @@ -66,11 +80,12 @@ export const App = () => { } /> - + + + + +
diff --git a/src/signals/config.ts b/src/signals/config.ts index df0a8fd..a2d9431 100644 --- a/src/signals/config.ts +++ b/src/signals/config.ts @@ -1,6 +1,5 @@ -import { usePrefersDark } from '@solid-primitives/media' import { makePersisted } from '@solid-primitives/storage' -import { createEffect, createSignal } from 'solid-js' +import { createSignal } from 'solid-js' import { CONNECTIONS_TABLE_INITIAL_COLUMN_ORDER, CONNECTIONS_TABLE_INITIAL_COLUMN_VISIBILITY, @@ -11,7 +10,6 @@ import { PROXIES_PREVIEW_TYPE, TAILWINDCSS_SIZE, } from '~/constants' -import { setCurTheme } from '~/signals' import { ConnectionsTableColumnOrder, ConnectionsTableColumnVisibility, @@ -134,13 +132,3 @@ export const isLatencyTestByHttps = () => export const latencyQualityMap = () => isLatencyTestByHttps() ? LATENCY_QUALITY_MAP_HTTPS : LATENCY_QUALITY_MAP_HTTP - -export const useAutoSwitchTheme = () => { - const prefersDark = usePrefersDark() - - createEffect(() => { - if (autoSwitchTheme()) { - setCurTheme(prefersDark() ? favNightTheme() : favDayTheme()) - } - }) -} diff --git a/src/signals/connections.ts b/src/signals/connections.ts index 8e48cfd..436e664 100644 --- a/src/signals/connections.ts +++ b/src/signals/connections.ts @@ -1,5 +1,5 @@ import { differenceWith, isNumber, unionWith } from 'lodash' -import { Accessor, createEffect, createSignal, untrack } from 'solid-js' +import { createEffect, createSignal, untrack } from 'solid-js' import { CONNECTIONS_TABLE_MAX_CLOSED_ROWS } from '~/constants' import { Connection, ConnectionRawMessage } from '~/types' @@ -15,16 +15,8 @@ export type WsMsg = { export const [allConnections, setAllConnections] = createSignal( [], ) - -export let latestConnectionMsg: Accessor = () => ({ - uploadTotal: 0, - downloadTotal: 0, - connections: [], -}) - -export const setLatestConnectionMsg = (accessor: Accessor) => { - latestConnectionMsg = accessor -} +export const [latestConnectionMsg, setLatestConnectionMsg] = + createSignal(null) export const useConnections = () => { const [closedConnections, setClosedConnections] = createSignal( diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts index 8df38cf..79467bc 100644 --- a/src/signals/proxies.ts +++ b/src/signals/proxies.ts @@ -60,7 +60,7 @@ const setProxiesInfo = (proxies: (Proxy | ProxyNode)[]) => { } export const useProxies = () => { - const updateProxies = async () => { + const fetchProxies = async () => { const [{ providers }, { proxies }] = await Promise.all([ fetchProxyProvidersAPI(), fetchProxiesAPI(), @@ -91,7 +91,7 @@ export const useProxies = () => { const selectProxyInGroup = async (proxy: Proxy, proxyName: string) => { await selectProxyInGroupAPI(proxy.name, proxyName) - await updateProxies() + await fetchProxies() if (autoCloseConns()) { // we don't use activeConns from useConnection here for better performance, @@ -130,19 +130,19 @@ export const useProxies = () => { try { await updateProxyProviderAPI(providerName) } catch {} - await updateProxies() + await fetchProxies() } const updateAllProvider = async () => { await Promise.allSettled( proxyProviders().map((provider) => updateProxyProviderAPI(provider.name)), ) - await updateProxies() + await fetchProxies() } const healthCheckByProviderName = async (providerName: string) => { await proxyProviderHealthCheck(providerName) - await updateProxies() + await fetchProxies() } return { @@ -151,7 +151,7 @@ export const useProxies = () => { latencyTestByProxyGroupName, latencyMap, proxyNodeMap, - updateProxies, + fetchProxies, selectProxyInGroup, updateProviderByProviderName, updateAllProvider,