From 5f4a8e6129340e6e52d4122862b07b4ef6d7d5bf Mon Sep 17 00:00:00 2001 From: NTaylorMullen Date: Tue, 13 Aug 2013 12:26:56 -0700 Subject: [PATCH] Modified connection.stop to no longer deffer itself until the page has loaded. - It now unbinds the load event handler for the connection start. - This allows for multiple starts/stops to occur during page load. - This issue cannot be unit/functionally tested directly because it requires the page to be in the loading state. #2189 --- .../jquery.signalR.core.js | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js b/src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js index 3e178a2a81..eb57dc3aed 100644 --- a/src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js +++ b/src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js @@ -340,9 +340,11 @@ // Check to see if start is being called prior to page load // If waitForPageLoad is true we then want to re-direct function call to the window load event if (!_pageLoaded && config.waitForPageLoad === true) { - _pageWindow.load(function () { + connection._.deferredStartHandler = function () { connection.start(options, callback); - }); + }; + _pageWindow.load(connection._.deferredStartHandler); + return deferred.promise(); } @@ -703,12 +705,16 @@ /// var connection = this; - // Verify that we should wait for page load to call stop. + // This needs to be checked despite the connection state because a connection start can be deferred until page load. + // If we've deferred the start due to a page load we need to unbind the "onLoad" -> start event. if (!_pageLoaded && (!connection._.config || connection._.config.waitForPageLoad === true)) { - // Can only stop connections after the page has loaded - _pageWindow.load(function () { - connection.stop(async, notifyServer); - }); + connection.log("Stopping connection prior to negotiate."); + + // Unbind the event so it's not triggered. + _pageWindow.unbind("load", connection._.deferredStartHandler); + + // Reject any promises for the current connections deferred. + connection._deferral.reject("The connection was stopped during page load."); return; } @@ -717,6 +723,11 @@ return; } + // Always clean up the private non-timeout based state. + delete connection._deferral; + delete connection._.config; + delete connection._.deferredStartHandler; + try { connection.log("Stopping connection."); @@ -740,11 +751,7 @@ delete connection.messageId; delete connection.groupsToken; - - // Remove the ID and the deferral on stop, this is to ensure that if a connection is restarted it takes on a new id/deferral. delete connection.id; - delete connection._deferral; - delete connection._.config; delete connection._.pingIntervalId; } finally {