Skip to content

Commit

Permalink
unify close connection after tab closed
Browse files Browse the repository at this point in the history
  • Loading branch information
CurryYangxx committed Dec 26, 2024
1 parent 2fb0bfa commit 6469466
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 58 deletions.
40 changes: 40 additions & 0 deletions packages/insomnia/src/ui/hooks/use-close-connection.ts
Original file line number Diff line number Diff line change
@@ -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]);
};
27 changes: 0 additions & 27 deletions packages/insomnia/src/ui/hooks/use-close-grpc.ts

This file was deleted.

27 changes: 0 additions & 27 deletions packages/insomnia/src/ui/hooks/use-close-websocket.ts

This file was deleted.

6 changes: 2 additions & 4 deletions packages/insomnia/src/ui/routes/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -455,8 +454,7 @@ export const Debug: FC = () => {
},
});

useCloseWebSocket();
useCloseGrpc();
useCloseConnection();

const isRealtimeRequest =
activeRequest &&
Expand Down

0 comments on commit 6469466

Please sign in to comment.