Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default to closeOnUnload unless we have some indication that a user may want to recover the connection #439

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion common/lib/util/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ Defaults.normaliseOptions = function(options) {
}

if(typeof options.recover === 'function' && options.closeOnUnload === true) {
Logger.logAction(LOG_ERROR, 'Defaults.normaliseOptions', 'closeOnUnload was true and a session recovery function was set - these are mutually exclusive, so unsetting the latter');
Logger.logAction(Logger.LOG_ERROR, 'Defaults.normaliseOptions', 'closeOnUnload was true and a session recovery function was set - these are mutually exclusive, so unsetting the latter');
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);
});