-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(tabs): keep connection for tabs-[INS-4778] #8266
base: feat/multiple-tab
Are you sure you want to change the base?
Changes from all commits
7653979
d9a735e
ef37009
8dfe1b6
0b3c330
ceecabc
c67f36a
217393d
599cb59
14a48cb
8f4715d
64fe62f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useCallback, useEffect } from 'react'; | ||
|
||
import * as models from '../../models'; | ||
import { isGrpcRequestId } from '../../models/grpc-request'; | ||
import { isEventStreamRequest, isGraphqlSubscriptionRequest, isRequestId } from '../../models/request'; | ||
import { isWebSocketRequestId } from '../../models/websocket-request'; | ||
import { useInsomniaTabContext } from '../context/app/insomnia-tab-context'; | ||
import uiEventBus from '../eventBus'; | ||
|
||
// this hook is use for control when to close connections(websocket & SSE & grpc stream & graphql subscription) | ||
export const useCloseConnection = ({ organizationId }: { organizationId: string }) => { | ||
|
||
const closeConnectionById = async (id: string) => { | ||
if (isGrpcRequestId(id)) { | ||
window.main.grpc.cancel(id); | ||
} else if (isWebSocketRequestId(id)) { | ||
window.main.webSocket.close({ requestId: id }); | ||
} else if (isRequestId(id)) { | ||
const request = await models.request.getById(id); | ||
if (request && isEventStreamRequest(request)) { | ||
window.main.curl.close({ requestId: id }); | ||
} else if (request && isGraphqlSubscriptionRequest(request)) { | ||
window.main.webSocket.close({ requestId: id }); | ||
} | ||
} | ||
}; | ||
|
||
// close websocket&grpc&SSE connections | ||
const handleTabClose = useCallback((_: string, ids: 'all' | string[]) => { | ||
if (ids === 'all') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If user just closse all tabs in one org, will this also close connections from other organizations? |
||
window.main.webSocket.closeAll(); | ||
window.main.grpc.closeAll(); | ||
window.main.curl.closeAll(); | ||
return; | ||
} | ||
|
||
ids.forEach(async id => { | ||
await closeConnectionById(id); | ||
}); | ||
}, []); | ||
|
||
const { currentOrgTabs } = useInsomniaTabContext(); | ||
|
||
const handleActiveEnvironmentChange = useCallback((workspaceId: string) => { | ||
const { tabList } = currentOrgTabs; | ||
const tabs = tabList.filter(tab => tab.workspaceId === workspaceId); | ||
tabs.forEach(async tab => { | ||
const id = tab.id; | ||
await closeConnectionById(id); | ||
}); | ||
}, [currentOrgTabs]); | ||
|
||
useEffect(() => { | ||
uiEventBus.on('CLOSE_TAB', handleTabClose); | ||
uiEventBus.on('CHANGE_ACTIVE_ENV', handleActiveEnvironmentChange); | ||
|
||
return () => { | ||
uiEventBus.off('CLOSE_TAB', handleTabClose); | ||
uiEventBus.off('CHANGE_ACTIVE_ENV', handleActiveEnvironmentChange); | ||
}; | ||
}, [handleTabClose, handleActiveEnvironmentChange]); | ||
|
||
// close all connections when organizationId change | ||
useEffect(() => { | ||
return () => { | ||
window.main.webSocket.closeAll(); | ||
window.main.grpc.closeAll(); | ||
window.main.curl.closeAll(); | ||
}; | ||
}, [organizationId]); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +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 { 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'; | ||
|
@@ -452,13 +453,10 @@ export const Debug: FC = () => { | |
} | ||
}, | ||
}); | ||
// Close all websocket connections when the active environment changes | ||
useEffect(() => { | ||
return () => { | ||
window.main.webSocket.closeAll(); | ||
window.main.grpc.closeAll(); | ||
}; | ||
}, [activeEnvironment?._id]); | ||
Comment on lines
-455
to
-461
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discuss with @ihexxa. I will try to emit an event through the event bus when modifying the active environment. So that we could get the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated in this commit: f367b65
Comment on lines
-455
to
-461
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this use effect was clear about what happened and why, we close all connections when active env changes, the new hook is unclear about both of these questions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under the multiple-tabs scenario, when we switch to another tab that belongs to another workspace, |
||
|
||
useCloseConnection({ | ||
organizationId, | ||
}); | ||
|
||
const isRealtimeRequest = | ||
activeRequest && | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a react hook is a mechanism to wrap react features into a observable function for reuse.
close all connections appears to be a fire and forget behavior.
why introduce all this complexity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This scenario is indeed a bit complicated, we can't simply know when to close a connection, such as clicking a button and then closing the connection in the callback.
The connection may be closed because the user closed the tab manually or the user deleted the request or workspace. Using eventbus can decouple the connection control logic from the tab. Also I think abstract these logic into this custom hook could make the
debug.tsx
component simpler to read, and put the connection control logic together for easy maintenance.