From 5dbb99be21f3bd640f2a7de1ee19c3d32bc56a06 Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Fri, 6 Feb 2026 14:12:47 +0900 Subject: [PATCH] =?UTF-8?q?[v1.3]=20=E6=94=B9=E8=89=AF=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=20-=20subscribeMessage=20&=20HookManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/batchupdate/App.tsx | 4 +-- src/pages/options/routes/ScriptList/hooks.tsx | 17 ++++------ src/pages/options/routes/Setting.tsx | 17 ++++------ src/pages/popup/App.tsx | 32 ++++++++----------- src/pages/store/AppContext.tsx | 22 ++++--------- src/pages/store/global.ts | 9 ++++++ src/pkg/utils/hookManger.ts | 13 ++++++++ tests/pages/options/MainLayout.test.tsx | 1 + tests/pages/popup/App.test.tsx | 1 + 9 files changed, 59 insertions(+), 57 deletions(-) create mode 100644 src/pkg/utils/hookManger.ts diff --git a/src/pages/batchupdate/App.tsx b/src/pages/batchupdate/App.tsx index 163261fcf..3386cf849 100644 --- a/src/pages/batchupdate/App.tsx +++ b/src/pages/batchupdate/App.tsx @@ -17,8 +17,8 @@ import { } from "@App/app/service/service_worker/types"; import { dayFormat } from "@App/pkg/utils/day_format"; import { IconSync } from "@arco-design/web-react/icon"; -import { useAppContext } from "../store/AppContext"; import { SCRIPT_STATUS_ENABLE } from "@App/app/repo/scripts"; +import { subscribeMessage } from "@App/pages/store/global"; const CollapseItem = Collapse.Item; const { GridItem } = Grid; @@ -29,8 +29,6 @@ const { Text } = Typography; const pageExecute: Record void> = {}; function App() { - const { subscribeMessage } = useAppContext(); - const AUTO_CLOSE_PAGE = 8; // after 8s, auto close const getUrlParam = (key: string): string => { return (location.search?.includes(`${key}=`) ? new URLSearchParams(location.search).get(`${key}`) : "") || ""; diff --git a/src/pages/options/routes/ScriptList/hooks.tsx b/src/pages/options/routes/ScriptList/hooks.tsx index 42974e2d5..1d3024e18 100644 --- a/src/pages/options/routes/ScriptList/hooks.tsx +++ b/src/pages/options/routes/ScriptList/hooks.tsx @@ -15,7 +15,7 @@ import type { TEnableScript, TSortedScript, } from "@App/app/service/queue"; -import { useAppContext } from "@App/pages/store/AppContext"; +import { subscribeMessage } from "@App/pages/store/global"; import type { ScriptLoading } from "@App/pages/store/features/script"; import { fetchScript, @@ -49,10 +49,10 @@ import { Message } from "@arco-design/web-react"; import { cacheInstance } from "@App/app/cache"; import type { SearchType } from "@App/app/service/service_worker/types"; import { SearchFilter, type SearchFilterRequest } from "./SearchFilter"; +import { HookManager } from "@App/pkg/utils/hookManger"; export function useScriptList() { const { t } = useTranslation(); - const { subscribeMessage } = useAppContext(); const [scriptList, setScriptList] = useState([]); const [loadingList, setLoadingList] = useState(true); @@ -188,18 +188,15 @@ export function useScriptList() { }, }; - const unhooks = [ + const hookMgr = new HookManager(); + hookMgr.append( subscribeMessage("scriptRunStatus", pageApi.scriptRunStatus), subscribeMessage("installScript", pageApi.installScript), subscribeMessage("deleteScripts", pageApi.deleteScripts), subscribeMessage("enableScripts", pageApi.enableScripts), - subscribeMessage("sortedScripts", pageApi.sortedScripts), - ]; - return () => { - for (const unhook of unhooks) unhook(); - unhooks.length = 0; - }; - // eslint-disable-next-line react-hooks/exhaustive-deps + subscribeMessage("sortedScripts", pageApi.sortedScripts) + ); + return hookMgr.unhook; }, []); const updateScripts = (uuids: string[], data: Partial