Skip to content

Commit f03b2dd

Browse files
committed
refactor: network logic
1 parent 63348a4 commit f03b2dd

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

src/core/global.d.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
11
interface XMLHttpRequest {
22
_interceptionId?: string;
33
}
4-
5-
declare module 'react-native/Libraries/WebSocket/NativeWebSocketModule' {
6-
interface NativeWebSocketModule extends NativeModule {
7-
connect(
8-
url: string,
9-
protocols?: string | string[],
10-
options?: {
11-
headers: { [headerName: string]: string };
12-
[optionName: string]: any;
13-
},
14-
socketId: number,
15-
): void;
16-
17-
send(data: string, socketId: number): void;
18-
19-
sendBinary(base64String: string, socketId: number): void;
20-
21-
close(code: number, reason: string, socketId: number): void;
22-
23-
addListener: (eventType: string) => void;
24-
removeListeners: (count: number) => void;
25-
}
26-
27-
const NativeWebSocketModule: NativeWebSocketModule;
28-
export default NativeWebSocketModule;
29-
}

src/hooks/useNetworkInterceptor.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ export default function useNetworkInterceptor({
4343
setNetworkRequests(initRequests);
4444
};
4545

46+
const isMatchedDomain = (url: string) => {
47+
if (!includeDomains?.length) return true;
48+
49+
return includeDomains.some(domain => url.includes(domain));
50+
};
51+
4652
const enableHttpInterceptions = useCallback(() => {
4753
const openCallback: HttpHandlers['open'] = (id, type, method, url) => {
4854
if (!id) return;
4955

50-
const isNotMatchedDomain =
51-
!!joinedIncludeDomains.length && !includeDomains?.some(domain => url.includes(domain));
52-
53-
if (isNotMatchedDomain) return;
56+
if (!isMatchedDomain(url)) return;
5457

5558
setNetworkRequests((draft: NetworkRequests<HttpRequest>) => {
5659
draft.set(id, { type, method, url });
@@ -162,6 +165,8 @@ export default function useNetworkInterceptor({
162165
) => {
163166
if (typeof socketId !== 'number') return;
164167

168+
if (!isMatchedDomain(url)) return;
169+
165170
setNetworkRequests((draft: NetworkRequests<WebSocketRequest>) => {
166171
draft.set(`${socketId}`, {
167172
startTime,
@@ -245,7 +250,8 @@ export default function useNetworkInterceptor({
245250
.set('onError', onErrorCallback)
246251
.set('onClose', onCloseCallback)
247252
.enableInterception();
248-
}, [setNetworkRequests]);
253+
// eslint-disable-next-line react-hooks/exhaustive-deps
254+
}, [joinedIncludeDomains, setNetworkRequests]);
249255

250256
const enableInterception = useCallback(() => {
251257
if (isEnabled()) return;

0 commit comments

Comments
 (0)