Bug 1097862 - Perform the leave notification to the loop-server in a synchronous fashion to give the notification more change of succeeding. r=nperriault a=sylvestre
authorMark Banner <standard8@mozilla.com>
Mon, 15 Dec 2014 20:34:31 +0000
changeset 234385 67828882b7df8847887fe58e1def6b4e2f7f25ab
parent 234384 797d93d137a7be840b9670e67a3abbf1d5729131
child 234386 70f5d0f70c683e0b2d44e0da225a57bec45dde7f
push id7376
push userrjesup@wgate.com
push dateThu, 18 Dec 2014 14:24:23 +0000
treeherdermozilla-aurora@4bb4a4d37338 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnperriault, sylvestre
bugs1097862
milestone36.0a2
Bug 1097862 - Perform the leave notification to the loop-server in a synchronous fashion to give the notification more change of succeeding. r=nperriault a=sylvestre
browser/components/loop/standalone/content/js/standaloneMozLoop.js
browser/components/loop/test/standalone/standaloneMozLoop_test.js
--- a/browser/components/loop/standalone/content/js/standaloneMozLoop.js
+++ b/browser/components/loop/standalone/content/js/standaloneMozLoop.js
@@ -104,49 +104,51 @@ loop.StandaloneMozLoop = (function(mozL1
       }.bind(this));
 
       req.fail(failureHandler.bind(this, callback));
     },
 
     /**
      * Internal function to actually perform a post to a room.
      *
-     * @param {String} roomToken The rom token.
+     * @param {String} roomToken    The room token.
      * @param {String} sessionToken The sessionToken for the room if known
-     * @param {Object} roomData The data to send with the request
+     * @param {Object} roomData     The data to send with the request
      * @param {Array} expectedProps The expected properties we should receive from the
      *                              server
+     * @param {Boolean} async       Set to true for an async request, false for sync.
      * @param {Function} callback The callback for when the request completes. The
      *                            first parameter is non-null on error, the second parameter
      *                            is the response data.
      */
-    _postToRoom: function(roomToken, sessionToken, roomData, expectedProps, callback) {
+    _postToRoom: function(roomToken, sessionToken, roomData, expectedProps,
+                          async, callback) {
       var req = $.ajax({
         url:         this._baseServerUrl + "/rooms/" + roomToken,
         method:      "POST",
         contentType: "application/json",
         dataType:    "json",
         data: JSON.stringify(roomData),
         beforeSend: function(xhr) {
           if (sessionToken) {
             xhr.setRequestHeader("Authorization", "Basic " + btoa(sessionToken));
           }
-        }
+        },
+        async: async,
+        success: function(responseData) {
+          console.log("done");
+          try {
+            callback(null, validate(responseData, expectedProps));
+          } catch (err) {
+            console.error("Error requesting call info", err.message);
+            callback(err);
+          }
+        }.bind(this),
+        error: failureHandler.bind(this, callback)
       });
-
-      req.done(function(responseData) {
-        try {
-          callback(null, validate(responseData, expectedProps));
-        } catch (err) {
-          console.error("Error requesting call info", err.message);
-          callback(err);
-        }
-      }.bind(this));
-
-      req.fail(failureHandler.bind(this, callback));
     },
 
     /**
      * Joins a room
      *
      * @param {String} roomToken  The room token.
      * @param {Function} callback Function that will be invoked once the operation
      *                            finished. The first argument passed will be an
@@ -167,17 +169,17 @@ loop.StandaloneMozLoop = (function(mozL1
         action: "join",
         displayName: mozL10n.get("rooms_display_name_guest"),
         clientMaxSize: ROOM_MAX_CLIENTS
       }, {
         apiKey: String,
         sessionId: String,
         sessionToken: String,
         expires: Number
-      }, callbackWrapper.bind(this));
+      }, true, callbackWrapper.bind(this));
     },
 
     /**
      * Refreshes a room
      *
      * @param {String} roomToken    The room token.
      * @param {String} sessionToken The session token for the session that has been
      *                              joined
@@ -186,17 +188,17 @@ loop.StandaloneMozLoop = (function(mozL1
      *                              `Error` object or `null`.
      */
     refreshMembership: function(roomToken, sessionToken, callback) {
       this._postToRoom(roomToken, sessionToken, {
         action: "refresh",
         sessionToken: sessionToken
       }, {
         expires: Number
-      }, callback);
+      }, true, callback);
     },
 
     /**
      * Leaves a room. Although this is an sync function, no data is returned
      * from the server.
      *
      * @param {String} roomToken    The room token.
      * @param {String} sessionToken The session token for the session that has been
@@ -209,20 +211,21 @@ loop.StandaloneMozLoop = (function(mozL1
       if (!callback) {
         callback = function(error) {
           if (error) {
             console.error(error);
           }
         };
       }
 
+      // We do this as a synchronous request in case this is closing the window.
       this._postToRoom(roomToken, sessionToken, {
         action: "leave",
         sessionToken: sessionToken
-      }, null, callback);
+      }, null, false, callback);
     },
 
     // Dummy functions to reflect those in the desktop mozLoop.rooms that we
     // don't currently use.
     on: function() {},
     once: function() {},
     off: function() {}
   };
--- a/browser/components/loop/test/standalone/standaloneMozLoop_test.js
+++ b/browser/components/loop/test/standalone/standaloneMozLoop_test.js
@@ -208,16 +208,17 @@ describe("loop.StandaloneMozLoop", funct
     });
   });
 
   describe("#rooms.leave", function() {
     it("should POST to the server", function() {
       mozLoop.rooms.leave("fakeToken", "fakeSessionToken", callback);
 
       expect(requests).to.have.length.of(1);
+      expect(requests[0].async).eql(false);
       expect(requests[0].url).eql(fakeBaseServerUrl + "/rooms/fakeToken");
       expect(requests[0].method).eql("POST");
       expect(requests[0].requestHeaders.Authorization)
         .eql("Basic " + btoa("fakeSessionToken"));
 
       var requestData = JSON.parse(requests[0].requestBody);
       expect(requestData.action).eql("leave");
     });