Skip to content

Commit

Permalink
Modified connection.stop to no longer deffer itself until the page ha…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
NTaylorMullen authored and halter73 committed Oct 1, 2013
1 parent 73809fb commit 213fcd8
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js
Expand Up @@ -321,9 +321,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();
}

Expand Down Expand Up @@ -681,12 +683,16 @@
/// <returns type="signalR" />
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;
}
Expand All @@ -695,6 +701,11 @@
return;
}

// Always clean up the private non-timeout based state.
delete connection._deferral;
delete connection._.config;
delete connection._.deferredStartHandler;

try {
connection.log("SignalR: Stopping connection.");

Expand All @@ -716,11 +727,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;
}
finally {
changeState(connection, connection.state, signalR.connectionState.disconnected);
Expand Down

0 comments on commit 213fcd8

Please sign in to comment.