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

Commit

Permalink
polish the code
Browse files Browse the repository at this point in the history
  • Loading branch information
khanghoang committed Jul 29, 2019
1 parent 184065c commit 918828b
Showing 1 changed file with 33 additions and 39 deletions.
72 changes: 33 additions & 39 deletions front_end/ndb/NetworkInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor {

_sendRawMessage(rawMessage) {}

_listen() {
async _listen() {
InspectorFrontendHost.sendMessageToBackend = rawMessage => {
const message = JSON.parse(rawMessage);

Expand All @@ -61,45 +61,39 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor {
}
};

// we need to setTimeout here because the httpMonkeyPatchingSource is loaded
// after this script
setTimeout(async() => {
while (this._target) {
try {
const raw = await this._target
.runtimeAgent()
.invoke_evaluate({
expression: `process._fetchNetworkMessages()`,
awaitPromise: true,
returnByValue: true
});

const {
result: { value: messages }
} = raw;

if (!messages) return;

// messages is array-like
const messagesArr = Array.from(JSON.parse(messages));

for (const message of messagesArr) {
const { type, payload } = message;
this._cacheRequests.push(message);

// this is on the way back, this way doesn't work
if (type !== 'Network.getResponseBody') {
// but this does
SDK._mainConnection._onMessage(JSON.stringify({
method: type,
params: payload
}));
}
}
} catch (err) {
console.log({ err });
while (this._target) {
const rawResponse = await this._target
.runtimeAgent()
.invoke_evaluate({
expression: `process._fetchNetworkMessages()`,
awaitPromise: true,
returnByValue: true
});

if (!rawResponse || !rawResponse.result) return;

const {
result: { value: messages }
} = rawResponse;

if (!messages) return;

// messages is array-like
const messagesArr = Array.from(JSON.parse(messages));

for (const message of messagesArr) {
const { type, payload } = message;
this._cacheRequests.push(message);

// this is on the way back, this way doesn't work
if (type !== 'Network.getResponseBody') {
// but this does
SDK._mainConnection._onMessage(JSON.stringify({
method: type,
params: payload
}));
}
}
}, 0);
}
}
};

0 comments on commit 918828b

Please sign in to comment.