Jelajahi Sumber

Attempt to restore BOSH session or auto_login before...

triggering connectionInitialized.

That way, when listening for `connectionInitialized`, we'll know when it
fires whether we've attached to a BOSH connection or not.
JC Brand 6 tahun lalu
induk
melakukan
b6b085189b
2 mengubah file dengan 21 tambahan dan 16 penghapusan
  1. 9 6
      spec/converse.js
  2. 12 10
      src/headless/converse-core.js

+ 9 - 6
spec/converse.js

@@ -12,16 +12,19 @@
 
         describe("Authentication", function () {
 
-            it("needs either a bosh_service_url a websocket_url or both", mock.initConverse((done, _converse) => {
+            it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(async (done, _converse) => {
                 const url = _converse.bosh_service_url;
                 const connection = _converse.connection;
                 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;
-                done();
+                try {
+                    await _converse.initConnection();
+                } catch (e) {
+                    _converse.bosh_service_url = url;
+                    _converse.connection = connection;
+                    expect(e.message).toBe("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
+                    done();
+                }
             }));
         });
 

+ 12 - 10
src/headless/converse-core.js

@@ -521,9 +521,11 @@ function clearSession  () {
 }
 
 
-_converse.initConnection = function () {
-    /* Creates a new Strophe.Connection instance if we don't already have one.
-     */
+/**
+ * Creates a new Strophe.Connection instance and if applicable, attempt to
+ * restore the BOSH session or if `auto_login` is true, attempt to log in.
+ */
+_converse.initConnection = async function () {
     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.");
@@ -546,6 +548,11 @@ _converse.initConnection = function () {
             throw new Error("initConnection: this browser does not support "+
                             "websockets and bosh_service_url wasn't specified.");
         }
+        if (_converse.auto_login) {
+            _converse.api.user.login();
+        } else if (_converse.api.connection.isType('bosh')) {
+            await _converse.restoreBOSHSession();
+        }
     }
     setUpXMLLogging();
     /**
@@ -644,15 +651,10 @@ function setUpXMLLogging () {
 }
 
 
-function finishInitialization () {
+async function finishInitialization () {
     initClientConfig();
     initPlugins();
-    _converse.initConnection();
-    if (_converse.auto_login) {
-        _converse.api.user.login();
-    } else if (_converse.api.connection.isType('bosh')) {
-        _converse.restoreBOSHSession();
-    }
+    await _converse.initConnection();
     _converse.registerGlobalEventHandlers();
     if (!Backbone.history.started) {
         Backbone.history.start();