-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unify close connection after tab closed
- Loading branch information
1 parent
2fb0bfa
commit 6469466
Showing
4 changed files
with
42 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters