Use getter to lazily create Strophe.Connection

This commit is contained in:
JC Brand 2018-11-18 18:04:18 +01:00
parent 86593dc378
commit 7af73c3471
9 changed files with 100 additions and 107 deletions

69
dist/converse.js vendored
View File

@ -51479,7 +51479,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
}
});
Promise.all([_converse.api.waitUntil('connectionInitialized'), _converse.api.waitUntil('chatBoxViewsInitialized')]).then(_converse.addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
_converse.api.waitUntil('chatBoxViewsInitialized').then(_converse.addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
_converse.on('chatBoxesFetched', () => {
const controlbox = _converse.chatboxes.get('controlbox') || _converse.addControlBox();
@ -53126,7 +53126,8 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_1__["default"].plugins
}
});
Promise.all([_converse.api.waitUntil('connectionInitialized'), _converse.api.waitUntil('chatBoxViewsInitialized')]).then(() => {
_converse.api.waitUntil('chatBoxViewsInitialized').then(() => {
_converse.minimized_chats = new _converse.MinimizedChats({
model: _converse.chatboxes
});
@ -59688,7 +59689,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
_converse.on('rosterGroupsFetched', this.sortAndPositionAllItems.bind(this));
_converse.on('rosterContactsFetched', () => {
_converse.roster.each(contact => this.addRosterContact(contact, {
_converse.roster.each(c => this.addRosterContact(c, {
'silent': true
}));
@ -62536,7 +62537,32 @@ const BOSH_WAIT = 59;
const _converse = {
'templates': {},
'promises': {}
'promises': {},
get connection() {
/* Creates a new Strophe.Connection instance if we don't already have one.
*/
if (!this._connection) {
if (!this.bosh_service_url && !this.websocket_url) {
throw new Error("connection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this._connection = new strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].Connection(this.websocket_url, this.connection_options);
} else if (this.bosh_service_url) {
this._connection = new strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].Connection(this.bosh_service_url, _lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.assignIn(this.connection_options, {
'keepalive': this.keepalive
}));
} else {
throw new Error("connection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
_converse.emit('connectionInitialized');
}
return this._connection;
}
};
_converse.VERSION_NAME = "v4.0.5";
@ -62821,28 +62847,6 @@ function initClientConfig() {
_converse.emit('clientConfigInitialized');
}
_converse.initConnection = function () {
/* Creates a new Strophe.Connection instance if we don't already have one.
*/
if (!_converse.connection) {
if (!_converse.bosh_service_url && !_converse.websocket_url) {
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && _converse.websocket_url) {
_converse.connection = new strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].Connection(_converse.websocket_url, _converse.connection_options);
} else if (_converse.bosh_service_url) {
_converse.connection = new strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].Connection(_converse.bosh_service_url, _lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.assignIn(_converse.connection_options, {
'keepalive': _converse.keepalive
}));
} else {
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
}
_converse.emit('connectionInitialized');
};
function setUpXMLLogging() {
strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].log = function (level, msg) {
_converse.log(msg, level);
@ -62862,9 +62866,6 @@ function setUpXMLLogging() {
function finishInitialization() {
initPlugins();
initClientConfig();
_converse.initConnection();
setUpXMLLogging();
_converse.logIn();
@ -62880,6 +62881,8 @@ function finishInitialization() {
_converse.api.disco.own.features.add(strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].NS.IDLE);
});
}
_converse.initialized = true;
}
function cleanup() {
@ -62900,7 +62903,9 @@ function cleanup() {
delete _converse.controlboxtoggle;
delete _converse.chatboxviews;
_converse.connection.reset();
if (_converse._connection) {
_converse._connection.reset();
}
_converse.stopListening();
@ -62918,7 +62923,7 @@ _converse.initialize = function (settings, callback) {
_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.each(PROMISES, addPromise);
if (!_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.isUndefined(_converse.connection)) {
if (_converse.initialized) {
cleanup();
}
@ -63766,7 +63771,7 @@ _converse.initialize = function (settings, callback) {
if (settings.connection) {
this.connection = settings.connection;
this._connection = settings.connection;
}
if (!_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.isUndefined(_converse.connection) && _converse.connection.service === 'jasmine tests') {

View File

@ -12,20 +12,24 @@
describe("Authentication", function () {
it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(function (_converse) {
var url = _converse.bosh_service_url;
var connection = _converse.connection;
const url = _converse.bosh_service_url;
delete _converse.bosh_service_url;
delete _converse.connection;
expect(_converse.initConnection).toThrow(
new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both."));
_converse.bosh_service_url = url;
_converse.connection = connection;
delete _converse._connection;
let err;
try {
_converse.connection;
} catch (e) {
err = e;
}
expect(_.get(err, 'message'))
.toBe("connection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}));
describe("with prebind", function () {
it("needs a jid when also using keepalive", mock.initConverse(function (_converse) {
var authentication = _converse.authentication;
var jid = _converse.jid;
const authentication = _converse.authentication;
const jid = _converse.jid;
delete _converse.jid;
_converse.keepalive = true;
_converse.authentication = "prebind";
@ -33,9 +37,6 @@
new Error(
"restoreBOSHSession: tried to restore a \"keepalive\" session "+
"but we don't have the JID for the user!"));
_converse.authentication= authentication;
_converse.jid = jid;
_converse.keepalive = false;
}));
it("needs jid, rid and sid values when not using keepalive", mock.initConverse(function (_converse) {
@ -208,30 +209,20 @@
it("has a method for retrieving the next RID", mock.initConverse(function (_converse) {
test_utils.createContacts(_converse, 'current');
var old_connection = _converse.connection;
_converse.connection._proto.rid = '1234';
_converse.expose_rid_and_sid = false;
expect(_converse.api.tokens.get('rid')).toBe(null);
_converse.expose_rid_and_sid = true;
expect(_converse.api.tokens.get('rid')).toBe('1234');
_converse.connection = undefined;
expect(_converse.api.tokens.get('rid')).toBe(null);
// Restore the connection
_converse.connection = old_connection;
}));
it("has a method for retrieving the SID", mock.initConverse(function (_converse) {
test_utils.createContacts(_converse, 'current');
var old_connection = _converse.connection;
_converse.connection._proto.sid = '1234';
_converse.expose_rid_and_sid = false;
expect(_converse.api.tokens.get('sid')).toBe(null);
_converse.expose_rid_and_sid = true;
expect(_converse.api.tokens.get('sid')).toBe('1234');
_converse.connection = undefined;
expect(_converse.api.tokens.get('sid')).toBe(null);
// Restore the connection
_converse.connection = old_connection;
}));
});

View File

@ -6,7 +6,7 @@
it("contains a checkbox to indicate whether the computer is trusted or not",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
allow_registration: false },
function (done, _converse) {
@ -44,7 +44,7 @@
it("checkbox can be set to false by default",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
trusted: false,
allow_registration: false },

View File

@ -9,23 +9,21 @@
it("is not available unless allow_registration=true",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
allow_registration: false },
function (done, _converse) {
async function (done, _converse) {
test_utils.waitUntil(() => _converse.chatboxviews.get('controlbox'))
.then(function () {
test_utils.openControlBox();
var cbview = _converse.chatboxviews.get('controlbox');
expect($(cbview.el.querySelector('a.register-account')).length).toBe(0);
done();
});
await test_utils.waitUntil(() => _converse.chatboxviews.get('controlbox'));
test_utils.openControlBox();
const cbview = _converse.chatboxviews.get('controlbox');
expect(cbview.el.querySelectorAll('a.register-account').length).toBe(0);
done();
}));
it("can be opened by clicking on the registration tab",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
allow_registration: true },
function (done, _converse) {
@ -52,7 +50,7 @@
it("allows the user to choose an XMPP provider's domain",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
allow_registration: true },
function (done, _converse) {
@ -92,7 +90,7 @@
it("will render a registration form as received from the XMPP provider",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
allow_registration: true },
function (done, _converse) {
@ -151,7 +149,7 @@
it("will set form_type to legacy and submit it as legacy",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
allow_registration: true },
function (done, _converse) {
@ -212,7 +210,7 @@
it("will set form_type to xform and submit it as xform",
mock.initConverseWithPromises(
null, ['connectionInitialized', 'chatBoxesInitialized'],
null, ['chatBoxesInitialized'],
{ auto_login: false,
allow_registration: true },
function (done, _converse) {

View File

@ -598,10 +598,9 @@ converse.plugins.add('converse-controlbox', {
}
});
Promise.all([
_converse.api.waitUntil('connectionInitialized'),
_converse.api.waitUntil('chatBoxViewsInitialized')
]).then(_converse.addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
_converse.api.waitUntil('chatBoxViewsInitialized')
.then(_converse.addControlBox)
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
_converse.on('chatBoxesFetched', () => {
const controlbox = _converse.chatboxes.get('controlbox') || _converse.addControlBox();

View File

@ -509,10 +509,8 @@ converse.plugins.add('converse-minimize', {
}
});
Promise.all([
_converse.api.waitUntil('connectionInitialized'),
_converse.api.waitUntil('chatBoxViewsInitialized')
]).then(() => {
_converse.api.waitUntil('chatBoxViewsInitialized')
.then(() => {
_converse.minimized_chats = new _converse.MinimizedChats({
model: _converse.chatboxes
});

View File

@ -782,7 +782,7 @@ converse.plugins.add('converse-rosterview', {
_converse.on('rosterGroupsFetched', this.sortAndPositionAllItems.bind(this));
_converse.on('rosterContactsFetched', () => {
_converse.roster.each((contact) => this.addRosterContact(contact, {'silent': true}));
_converse.roster.each(c => this.addRosterContact(c, {'silent': true}));
this.update();
this.updateFilter();
this.trigger('rosterContactsFetchedAndProcessed');

View File

@ -69,7 +69,29 @@ const BOSH_WAIT = 59;
*/
const _converse = {
'templates': {},
'promises': {}
'promises': {},
get connection () {
/* Creates a new Strophe.Connection instance if we don't already have one.
*/
if (!this._connection) {
if (!this.bosh_service_url && ! this.websocket_url) {
throw new Error("connection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this._connection = new Strophe.Connection(this.websocket_url, this.connection_options);
} else if (this.bosh_service_url) {
this._connection = new Strophe.Connection(
this.bosh_service_url,
_.assignIn(this.connection_options, {'keepalive': this.keepalive})
);
} else {
throw new Error("connection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
_converse.emit('connectionInitialized');
}
return this._connection;
}
}
_converse.VERSION_NAME = "v4.0.5";
@ -373,27 +395,6 @@ function initClientConfig () {
_converse.emit('clientConfigInitialized');
}
_converse.initConnection = function () {
/* Creates a new Strophe.Connection instance if we don't already have one.
*/
if (!_converse.connection) {
if (!_converse.bosh_service_url && ! _converse.websocket_url) {
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && _converse.websocket_url) {
_converse.connection = new Strophe.Connection(_converse.websocket_url, _converse.connection_options);
} else if (_converse.bosh_service_url) {
_converse.connection = new Strophe.Connection(
_converse.bosh_service_url,
_.assignIn(_converse.connection_options, {'keepalive': _converse.keepalive})
);
} else {
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
}
_converse.emit('connectionInitialized');
}
function setUpXMLLogging () {
Strophe.log = function (level, msg) {
@ -413,7 +414,6 @@ function setUpXMLLogging () {
function finishInitialization () {
initPlugins();
initClientConfig();
_converse.initConnection();
setUpXMLLogging();
_converse.logIn();
_converse.registerGlobalEventHandlers();
@ -425,6 +425,7 @@ function finishInitialization () {
_converse.api.disco.own.features.add(Strophe.NS.IDLE);
});
}
_converse.initialized = true;
}
@ -441,7 +442,9 @@ function cleanup () {
}
delete _converse.controlboxtoggle;
delete _converse.chatboxviews;
_converse.connection.reset();
if (_converse._connection) {
_converse._connection.reset();
}
_converse.stopListening();
_converse.tearDown();
delete _converse.config;
@ -454,7 +457,7 @@ _converse.initialize = function (settings, callback) {
settings = !_.isUndefined(settings) ? settings : {};
const init_promise = u.getResolveablePromise();
_.each(PROMISES, addPromise);
if (!_.isUndefined(_converse.connection)) {
if (_converse.initialized) {
cleanup();
}
@ -1212,12 +1215,11 @@ _converse.initialize = function (settings, callback) {
return _converse;
};
// Initialization
// --------------
// This is the end of the initialize method.
if (settings.connection) {
this.connection = settings.connection;
this._connection = settings.connection;
}
if (!_.isUndefined(_converse.connection) &&

View File

@ -66,7 +66,7 @@
};
utils.openControlBox = function () {
var toggle = document.querySelector(".toggle-controlbox");
const toggle = document.querySelector(".toggle-controlbox");
if (!u.isVisible(document.querySelector("#controlbox"))) {
if (!u.isVisible(toggle)) {
u.removeClass('hidden', toggle);