Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.

Commit 918828b

Browse files
committed
polish the code
1 parent 184065c commit 918828b

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

front_end/ndb/NetworkInterceptor.js

+33-39
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor {
3535

3636
_sendRawMessage(rawMessage) {}
3737

38-
_listen() {
38+
async _listen() {
3939
InspectorFrontendHost.sendMessageToBackend = rawMessage => {
4040
const message = JSON.parse(rawMessage);
4141

@@ -61,45 +61,39 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor {
6161
}
6262
};
6363

64-
// we need to setTimeout here because the httpMonkeyPatchingSource is loaded
65-
// after this script
66-
setTimeout(async() => {
67-
while (this._target) {
68-
try {
69-
const raw = await this._target
70-
.runtimeAgent()
71-
.invoke_evaluate({
72-
expression: `process._fetchNetworkMessages()`,
73-
awaitPromise: true,
74-
returnByValue: true
75-
});
76-
77-
const {
78-
result: { value: messages }
79-
} = raw;
80-
81-
if (!messages) return;
82-
83-
// messages is array-like
84-
const messagesArr = Array.from(JSON.parse(messages));
85-
86-
for (const message of messagesArr) {
87-
const { type, payload } = message;
88-
this._cacheRequests.push(message);
89-
90-
// this is on the way back, this way doesn't work
91-
if (type !== 'Network.getResponseBody') {
92-
// but this does
93-
SDK._mainConnection._onMessage(JSON.stringify({
94-
method: type,
95-
params: payload
96-
}));
97-
}
98-
}
99-
} catch (err) {
100-
console.log({ err });
64+
while (this._target) {
65+
const rawResponse = await this._target
66+
.runtimeAgent()
67+
.invoke_evaluate({
68+
expression: `process._fetchNetworkMessages()`,
69+
awaitPromise: true,
70+
returnByValue: true
71+
});
72+
73+
if (!rawResponse || !rawResponse.result) return;
74+
75+
const {
76+
result: { value: messages }
77+
} = rawResponse;
78+
79+
if (!messages) return;
80+
81+
// messages is array-like
82+
const messagesArr = Array.from(JSON.parse(messages));
83+
84+
for (const message of messagesArr) {
85+
const { type, payload } = message;
86+
this._cacheRequests.push(message);
87+
88+
// this is on the way back, this way doesn't work
89+
if (type !== 'Network.getResponseBody') {
90+
// but this does
91+
SDK._mainConnection._onMessage(JSON.stringify({
92+
method: type,
93+
params: payload
94+
}));
10195
}
10296
}
103-
}, 0);
97+
}
10498
}
10599
};

0 commit comments

Comments
 (0)