Bug 1358715 - Queue networkUpdateEvent in netmonitor controller r?honza draft
authorRicky Chien <ricky060709@gmail.com>
Sat, 22 Apr 2017 14:36:13 +0800
changeset 566937 7f9b39d2afb47f3d5ffeb73e47ba4638b5af324b
parent 566936 0ff2d7a63bb5ea81f332ef82a718ab4abebc0627
child 566938 74ea42c914ab6c3990fb12512c0846d9062d8413
push id55382
push userbmo:rchien@mozilla.com
push dateMon, 24 Apr 2017 06:32:45 +0000
reviewershonza
bugs1358715
milestone55.0a1
Bug 1358715 - Queue networkUpdateEvent in netmonitor controller r?honza MozReview-Commit-ID: 5uwnS1JDfzE
devtools/client/netmonitor/src/netmonitor-controller.js
--- a/devtools/client/netmonitor/src/netmonitor-controller.js
+++ b/devtools/client/netmonitor/src/netmonitor-controller.js
@@ -289,16 +289,17 @@ var NetMonitorController = {
     }
   },
 };
 
 /**
  * Functions handling target network events.
  */
 function NetworkEventsHandler() {
+  this.payloadQueue = [];
   this.addRequest = this.addRequest.bind(this);
   this.updateRequest = this.updateRequest.bind(this);
   this._onNetworkEvent = this._onNetworkEvent.bind(this);
   this._onNetworkEventUpdate = this._onNetworkEventUpdate.bind(this);
   this._onDocLoadingMarker = this._onDocLoadingMarker.bind(this);
   this._onRequestHeaders = this._onRequestHeaders.bind(this);
   this._onRequestCookies = this._onRequestCookies.bind(this);
   this._onRequestPostData = this._onRequestPostData.bind(this);
@@ -520,16 +521,39 @@ NetworkEventsHandler.prototype = {
         if (reqCookies.length) {
           payload.requestCookies = reqCookies;
         }
       }
     }
     return payload;
   },
 
+  getPayloadFromQueue(id) {
+    let queueItem = this.payloadQueue.find((item) => item.id === id) || {};
+    return queueItem.payload;
+  },
+
+  // Packet order of "networkUpdateEvent" is predictable, as a result we can wait for
+  // the last one "eventTimings" packet arrives to check payload is ready
+  isQueuePayloadReady(id) {
+    let queuedPayload = this.getPayloadFromQueue(id);
+    return queuedPayload && queuedPayload.eventTimings;
+  },
+
+  pushPayloadToQueue(id, payload) {
+    let queuedPayload = this.getPayloadFromQueue(id);
+
+    if (!queuedPayload) {
+      this.payloadQueue.push({ id, payload });
+    } else {
+      // Merge upcoming networkEventUpdate payload into existing one
+      this.payloadQueue.push({ id, payload: Object.assign(queuedPayload, payload) });
+    }
+  },
+
   async updateRequest(id, data) {
     let {
       mimeType,
       responseContent,
       responseCookies,
       responseHeaders,
       requestCookies,
       requestHeaders,
@@ -551,17 +575,22 @@ NetworkEventsHandler.prototype = {
       this.fetchPostData(requestPostData),
       this.fetchRequestCookies(requestCookies),
       this.fetchResponseCookies(responseCookies),
     ]);
 
     let payload = Object.assign({}, data,
                                     imageObj, requestHeadersObj, responseHeadersObj,
                                     postDataObj, requestCookiesObj, responseCookiesObj);
-    await this.actions.updateRequest(id, payload, true);
+
+    this.pushPayloadToQueue(id, payload);
+
+    if (this.isQueuePayloadReady(id)) {
+      await this.actions.updateRequest(id, this.getPayloadFromQueue(id), true);
+    }
   },
 
   /**
    * The "networkEventUpdate" message type handler.
    *
    * @param string type
    *        Message type.
    * @param object packet