From 6469466fc5b0f1cf785a478af05d9dbb00ef2a3d Mon Sep 17 00:00:00 2001 From: Curry Yang <1019yanglu@gmail.com> Date: Thu, 26 Dec 2024 16:35:23 +0800 Subject: [PATCH] unify close connection after tab closed --- .../src/ui/hooks/use-close-connection.ts | 40 +++++++++++++++++++ .../insomnia/src/ui/hooks/use-close-grpc.ts | 27 ------------- .../src/ui/hooks/use-close-websocket.ts | 27 ------------- packages/insomnia/src/ui/routes/debug.tsx | 6 +-- 4 files changed, 42 insertions(+), 58 deletions(-) create mode 100644 packages/insomnia/src/ui/hooks/use-close-connection.ts delete mode 100644 packages/insomnia/src/ui/hooks/use-close-grpc.ts delete mode 100644 packages/insomnia/src/ui/hooks/use-close-websocket.ts diff --git a/packages/insomnia/src/ui/hooks/use-close-connection.ts b/packages/insomnia/src/ui/hooks/use-close-connection.ts new file mode 100644 index 00000000000..7cce8162167 --- /dev/null +++ b/packages/insomnia/src/ui/hooks/use-close-connection.ts @@ -0,0 +1,40 @@ +import { useCallback, useEffect } from 'react'; + +import * as models from '../../models'; +import { isGrpcRequestId } from '../../models/grpc-request'; +import { isEventStreamRequest, isRequestId } from '../../models/request'; +import { isWebSocketRequestId } from '../../models/websocket-request'; +import uiEventBus, { UIEventType } from '../eventBus'; + +export const useCloseConnection = () => { + // close websocket&grpc&SSE connections + const closeConnection = useCallback((ids: 'all' | string[]) => { + if (ids === 'all') { + window.main.webSocket.closeAll(); + window.main.grpc.closeAll(); + window.main.curl.closeAll(); + return; + } + + ids.forEach(async id => { + if (isGrpcRequestId(id)) { + window.main.grpc.cancel(id); + } else if (isWebSocketRequestId()) { + window.main.webSocket.close({ requestId: id }); + } else if (isRequestId(id)) { + const request = await models.request.getById(id); + if (request && isEventStreamRequest(request)) { + window.main.webSocket.close({ requestId: id }); + } + } + }); + }, []); + + useEffect(() => { + uiEventBus.on(UIEventType.CLOSE_TAB, closeConnection); + + return () => { + uiEventBus.off(UIEventType.CLOSE_TAB, closeConnection); + }; + }, [closeConnection]); +}; diff --git a/packages/insomnia/src/ui/hooks/use-close-grpc.ts b/packages/insomnia/src/ui/hooks/use-close-grpc.ts deleted file mode 100644 index 8168147c827..00000000000 --- a/packages/insomnia/src/ui/hooks/use-close-grpc.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { useCallback, useEffect } from 'react'; - -import { isGrpcRequestId } from '../../models/grpc-request'; -import uiEventBus, { UIEventType } from '../eventBus'; - -export const useCloseGrpc = () => { - const closeGrpcConnection = useCallback((ids: 'all' | string[]) => { - if (ids === 'all') { - window.main.webSocket.closeAll(); - return; - } - - ids.forEach(id => { - if (isGrpcRequestId(id)) { - window.main.grpc.cancel(id); - } - }); - }, []); - - useEffect(() => { - uiEventBus.on(UIEventType.CLOSE_TAB, closeGrpcConnection); - - return () => { - uiEventBus.off(UIEventType.CLOSE_TAB, closeGrpcConnection); - }; - }, [closeGrpcConnection]); -}; diff --git a/packages/insomnia/src/ui/hooks/use-close-websocket.ts b/packages/insomnia/src/ui/hooks/use-close-websocket.ts deleted file mode 100644 index 0d5816b9e6b..00000000000 --- a/packages/insomnia/src/ui/hooks/use-close-websocket.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { useCallback, useEffect } from 'react'; - -import { isWebSocketRequestId } from '../../models/websocket-request'; -import uiEventBus, { UIEventType } from '../eventBus'; - -export const useCloseWebSocket = () => { - const closeWebSocketConnection = useCallback((ids: 'all' | string[]) => { - if (ids === 'all') { - window.main.webSocket.closeAll(); - return; - } - - ids.forEach(id => { - if (isWebSocketRequestId(id)) { - window.main.webSocket.close({ requestId: id }); - } - }); - }, []); - - useEffect(() => { - uiEventBus.on(UIEventType.CLOSE_TAB, closeWebSocketConnection); - - return () => { - uiEventBus.off(UIEventType.CLOSE_TAB, closeWebSocketConnection); - }; - }, [closeWebSocketConnection]); -}; diff --git a/packages/insomnia/src/ui/routes/debug.tsx b/packages/insomnia/src/ui/routes/debug.tsx index aa3e6abd276..3c1de4c791d 100644 --- a/packages/insomnia/src/ui/routes/debug.tsx +++ b/packages/insomnia/src/ui/routes/debug.tsx @@ -105,8 +105,7 @@ import { getMethodShortHand } from '../components/tags/method-tag'; import { RealtimeResponsePane } from '../components/websockets/realtime-response-pane'; import { WebSocketRequestPane } from '../components/websockets/websocket-request-pane'; import { INSOMNIA_TAB_HEIGHT } from '../constant'; -import { useCloseGrpc } from '../hooks/use-close-grpc'; -import { useCloseWebSocket } from '../hooks/use-close-websocket'; +import { useCloseConnection } from '../hooks/use-close-connection'; import { useExecutionState } from '../hooks/use-execution-state'; import { useInsomniaTab } from '../hooks/use-insomnia-tab'; import { useReadyState } from '../hooks/use-ready-state'; @@ -455,8 +454,7 @@ export const Debug: FC = () => { }, }); - useCloseWebSocket(); - useCloseGrpc(); + useCloseConnection(); const isRealtimeRequest = activeRequest &&