Bug 1097862 - Perform the leave notification to the loop-server in a synchronous fashion to give the notification more change of succeeding. r=nperriault
authorMark Banner <standard8@mozilla.com>
Mon, 15 Dec 2014 20:34:31 +0000
changeset 219889 240000a647f1d31b9a3f2f61ef01cd528f2d21cb
parent 219888 df4c50342efb5af88274829157f4a0be83ec730e
child 219890 cc2573030f5b72495122784eeb8adc0dfb45fe1f
push id27971
push usercbook@mozilla.com
push dateTue, 16 Dec 2014 12:40:03 +0000
treeherdermozilla-central@249dbdbe09e0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnperriault
bugs1097862
milestone37.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1097862 - Perform the leave notification to the loop-server in a synchronous fashion to give the notification more change of succeeding. r=nperriault
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");
     });