Skip to content

Commit

Permalink
default to closeOnUnload unless we have some indication that a user m…
Browse files Browse the repository at this point in the history
…ay want to recover the connection
  • Loading branch information
SimonWoolf committed Jan 30, 2018
1 parent f60aae1 commit 293aed1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions common/lib/util/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ Defaults.normaliseOptions = function(options) {
options.recover = null;
}

if(!('closeOnUnload' in options)) {
/* Have closeOnUnload default to true unless we have any indication that
* the user may want to recover the connection */
options.closeOnUnload = !options.recover;
}

if(options.transports && Utils.arrIn(options.transports, 'xhr')) {
Logger.deprecated('transports: ["xhr"]', 'transports: ["xhr_streaming"]');
Utils.arrDeleteValue(options.transports, 'xhr');
Expand Down
2 changes: 1 addition & 1 deletion spec/browser/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
};

exports.page_refresh_with_manual_recovery = function(test) {
var realtime = helper.AblyRealtime(),
var realtime = helper.AblyRealtime({ closeOnUnload: false }),
refreshEvent = new Event('beforeunload', {'bubbles': true});

test.expect(2);
Expand Down
28 changes: 28 additions & 0 deletions spec/rest/defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,33 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
test.done();
};

exports.defaults_closeOnUnload = function(test) {
test.expect(6);
var options;

/* Default to true */
options = Defaults.normaliseOptions({});
test.equal(options.closeOnUnload, true);

/* Default to false if using manual recovery */
options = Defaults.normaliseOptions({recover: 'someRecoveryKey'});
test.equal(options.closeOnUnload, false);

/* Default to false if using autorecovery */
options = Defaults.normaliseOptions({recover: function(){}});
test.equal(options.closeOnUnload, false);

/* can override default with manual recovery */
options = Defaults.normaliseOptions({recover: 'someRecoveryKey', closeOnUnload: true});
test.equal(options.closeOnUnload, true);

/* can override default with autorecovery only at the cost of unsetting autorecovery */
options = Defaults.normaliseOptions({recover: function(){}, closeOnUnload: true});
test.equal(options.closeOnUnload, true);
test.ok(!options.recover);

test.done();
};

return module.exports = helper.withTimeout(exports);
});

0 comments on commit 293aed1

Please sign in to comment.