From 93c4f4714671ce74d9373004ae5a65f9f482440b Mon Sep 17 00:00:00 2001 From: Simon Woolf Date: Mon, 11 Dec 2017 22:08:30 +0100 Subject: [PATCH] default to closeOnUnload unless we have some indication that a user may want to recover the connection --- common/lib/util/defaults.js | 8 +++++++- spec/browser/connection.test.js | 2 +- spec/rest/defaults.test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/common/lib/util/defaults.js b/common/lib/util/defaults.js index 94d38d45a3..1348ed4231 100644 --- a/common/lib/util/defaults.js +++ b/common/lib/util/defaults.js @@ -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'); diff --git a/spec/browser/connection.test.js b/spec/browser/connection.test.js index c100ba4aaf..50fcd232dc 100644 --- a/spec/browser/connection.test.js +++ b/spec/browser/connection.test.js @@ -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); diff --git a/spec/rest/defaults.test.js b/spec/rest/defaults.test.js index 19708df5df..a614ecd607 100644 --- a/spec/rest/defaults.test.js +++ b/spec/rest/defaults.test.js @@ -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); });