From 918828bbf8e63de630909422a54bb1c6a2f9f92d Mon Sep 17 00:00:00 2001 From: Khang Hoang Date: Sun, 28 Jul 2019 20:04:24 -0700 Subject: [PATCH] polish the code --- front_end/ndb/NetworkInterceptor.js | 72 +++++++++++++---------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/front_end/ndb/NetworkInterceptor.js b/front_end/ndb/NetworkInterceptor.js index 3a2b8a49..57abb7a7 100644 --- a/front_end/ndb/NetworkInterceptor.js +++ b/front_end/ndb/NetworkInterceptor.js @@ -35,7 +35,7 @@ Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor { _sendRawMessage(rawMessage) {} - _listen() { + async _listen() { InspectorFrontendHost.sendMessageToBackend = rawMessage => { const message = JSON.parse(rawMessage); @@ -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); + } } };