瀏覽代碼

Revert "Use getter to lazily create Strophe.Connection"

This reverts commit 7af73c3471791a1e0627ae12279ec21e2a80d50d.

Doesn't look like the right approach for adding support for XEP-0156.
Work on that will continue in a branch in the meantime.
JC Brand 6 年之前
父節點
當前提交
d341c6a13b
共有 9 個文件被更改,包括 107 次插入100 次删除
  1. 32 37
      dist/converse.js
  2. 22 13
      spec/converse.js
  3. 2 2
      spec/login.js
  4. 14 12
      spec/register.js
  5. 4 3
      src/converse-controlbox.js
  6. 4 2
      src/converse-minimize.js
  7. 1 1
      src/converse-rosterview.js
  8. 27 29
      src/headless/converse-core.js
  9. 1 1
      tests/utils.js

+ 32 - 37
dist/converse.js

@@ -51479,7 +51479,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
       }
     });
 
-    _converse.api.waitUntil('chatBoxViewsInitialized').then(_converse.addControlBox).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
+    Promise.all([_converse.api.waitUntil('connectionInitialized'), _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,8 +53126,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_1__["default"].plugins
       }
 
     });
-
-    _converse.api.waitUntil('chatBoxViewsInitialized').then(() => {
+    Promise.all([_converse.api.waitUntil('connectionInitialized'), _converse.api.waitUntil('chatBoxViewsInitialized')]).then(() => {
       _converse.minimized_chats = new _converse.MinimizedChats({
         model: _converse.chatboxes
       });
@@ -59689,7 +59688,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
         _converse.on('rosterGroupsFetched', this.sortAndPositionAllItems.bind(this));
 
         _converse.on('rosterContactsFetched', () => {
-          _converse.roster.each(c => this.addRosterContact(c, {
+          _converse.roster.each(contact => this.addRosterContact(contact, {
             'silent': true
           }));
 
@@ -62537,32 +62536,7 @@ const BOSH_WAIT = 59;
 
 const _converse = {
   'templates': {},
-  '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;
-  }
-
+  'promises': {}
 };
 _converse.VERSION_NAME = "v4.0.5";
 
@@ -62847,6 +62821,28 @@ 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);
@@ -62866,6 +62862,9 @@ function setUpXMLLogging() {
 function finishInitialization() {
   initPlugins();
   initClientConfig();
+
+  _converse.initConnection();
+
   setUpXMLLogging();
 
   _converse.logIn();
@@ -62881,8 +62880,6 @@ function finishInitialization() {
       _converse.api.disco.own.features.add(strophe_js__WEBPACK_IMPORTED_MODULE_0__["Strophe"].NS.IDLE);
     });
   }
-
-  _converse.initialized = true;
 }
 
 function cleanup() {
@@ -62903,9 +62900,7 @@ function cleanup() {
   delete _converse.controlboxtoggle;
   delete _converse.chatboxviews;
 
-  if (_converse._connection) {
-    _converse._connection.reset();
-  }
+  _converse.connection.reset();
 
   _converse.stopListening();
 
@@ -62923,7 +62918,7 @@ _converse.initialize = function (settings, callback) {
 
   _lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.each(PROMISES, addPromise);
 
-  if (_converse.initialized) {
+  if (!_lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.isUndefined(_converse.connection)) {
     cleanup();
   }
 
@@ -63771,7 +63766,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') {

+ 22 - 13
spec/converse.js

@@ -12,24 +12,20 @@
         
         describe("Authentication", function () {
             it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(function (_converse) {
-                const url = _converse.bosh_service_url;
+                var url = _converse.bosh_service_url;
+                var connection = _converse.connection;
                 delete _converse.bosh_service_url;
-                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.");
+                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;
             }));
 
             describe("with prebind", function () {
-
                 it("needs a jid when also using keepalive", mock.initConverse(function (_converse) {
-                    const authentication = _converse.authentication;
-                    const jid = _converse.jid;
+                    var authentication = _converse.authentication;
+                    var jid = _converse.jid;
                     delete _converse.jid;
                     _converse.keepalive = true;
                     _converse.authentication = "prebind";
@@ -37,6 +33,9 @@
                         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) {
@@ -209,20 +208,30 @@
 
             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;
             }));
         });
 

+ 2 - 2
spec/login.js

@@ -6,7 +6,7 @@
 
         it("contains a checkbox to indicate whether the computer is trusted or not",
             mock.initConverseWithPromises(
-                null, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', '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, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', 'chatBoxesInitialized'],
                 { auto_login: false,
                   trusted: false,
                   allow_registration: false },

+ 14 - 12
spec/register.js

@@ -9,21 +9,23 @@
 
         it("is not available unless allow_registration=true",
             mock.initConverseWithPromises(
-                null, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', 'chatBoxesInitialized'],
                 { auto_login: false,
                   allow_registration: false },
-                async function (done, _converse) {
+                function (done, _converse) {
 
-            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();
+            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();
+            });
         }));
 
         it("can be opened by clicking on the registration tab",
             mock.initConverseWithPromises(
-                null, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', 'chatBoxesInitialized'],
                 { auto_login: false,
                   allow_registration: true },
                 function (done, _converse) {
@@ -50,7 +52,7 @@
 
         it("allows the user to choose an XMPP provider's domain",
             mock.initConverseWithPromises(
-                null, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', 'chatBoxesInitialized'],
                 { auto_login: false,
                   allow_registration: true },
                 function (done, _converse) {
@@ -90,7 +92,7 @@
 
         it("will render a registration form as received from the XMPP provider",
             mock.initConverseWithPromises(
-                null, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', 'chatBoxesInitialized'],
                 { auto_login: false,
                   allow_registration: true },
                 function (done, _converse) {
@@ -149,7 +151,7 @@
 
         it("will set form_type to legacy and submit it as legacy",
             mock.initConverseWithPromises(
-                null, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', 'chatBoxesInitialized'],
                 { auto_login: false,
                   allow_registration: true },
                 function (done, _converse) {
@@ -210,7 +212,7 @@
 
         it("will set form_type to xform and submit it as xform",
             mock.initConverseWithPromises(
-                null, ['chatBoxesInitialized'],
+                null, ['connectionInitialized', 'chatBoxesInitialized'],
                 { auto_login: false,
                   allow_registration: true },
                 function (done, _converse) {

+ 4 - 3
src/converse-controlbox.js

@@ -598,9 +598,10 @@ converse.plugins.add('converse-controlbox', {
             }
         });
 
-        _converse.api.waitUntil('chatBoxViewsInitialized')
-            .then(_converse.addControlBox)
-            .catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
+        Promise.all([
+            _converse.api.waitUntil('connectionInitialized'),
+            _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();

+ 4 - 2
src/converse-minimize.js

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

+ 1 - 1
src/converse-rosterview.js

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

+ 27 - 29
src/headless/converse-core.js

@@ -69,29 +69,7 @@ const BOSH_WAIT = 59;
  */
 const _converse = {
     'templates': {},
-    '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;
-    }
+    'promises': {}
 }
 
 _converse.VERSION_NAME = "v4.0.5";
@@ -395,6 +373,27 @@ 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) {
@@ -414,6 +413,7 @@ function setUpXMLLogging () {
 function finishInitialization () {
     initPlugins();
     initClientConfig();
+    _converse.initConnection();
     setUpXMLLogging();
     _converse.logIn();
     _converse.registerGlobalEventHandlers();
@@ -425,7 +425,6 @@ function finishInitialization () {
             _converse.api.disco.own.features.add(Strophe.NS.IDLE);
         });
     }
-    _converse.initialized = true;
 }
 
 
@@ -442,9 +441,7 @@ function cleanup () {
     }
     delete _converse.controlboxtoggle;
     delete _converse.chatboxviews;
-    if (_converse._connection) {
-        _converse._connection.reset();
-    }
+    _converse.connection.reset();
     _converse.stopListening();
     _converse.tearDown();
     delete _converse.config;
@@ -457,7 +454,7 @@ _converse.initialize = function (settings, callback) {
     settings = !_.isUndefined(settings) ? settings : {};
     const init_promise = u.getResolveablePromise();
     _.each(PROMISES, addPromise);
-    if (_converse.initialized) {
+    if (!_.isUndefined(_converse.connection)) {
         cleanup();
     }
 
@@ -1215,11 +1212,12 @@ _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) &&

+ 1 - 1
tests/utils.js

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