Explorar el Código

Move `setUserJID` to init.js

JC Brand hace 3 años
padre
commit
b16d035975

+ 6 - 30
src/headless/core.js

@@ -33,7 +33,7 @@ import {
     cleanup,
     initClientConfig,
     initPlugins,
-    initSession,
+    setUserJID,
     initSessionStorage,
     registerGlobalEventHandlers
 } from './utils/init.js';
@@ -152,7 +152,7 @@ export const api = _converse.api = {
                 // We also call `_proto._doDisconnect` so that connection event handlers
                 // for the old transport are removed.
                 if (api.connection.isType('websocket') && api.settings.get('bosh_service_url')) {
-                    await _converse.setUserJID(_converse.bare_jid);
+                    await setUserJID(_converse.bare_jid);
                     _converse.connection._proto._doDisconnect();
                     _converse.connection._proto = new Strophe.Bosh(_converse.connection);
                     _converse.connection.service = api.settings.get('bosh_service_url');
@@ -161,9 +161,9 @@ export const api = _converse.api = {
                         // When reconnecting anonymously, we need to connect with only
                         // the domain, not the full JID that we had in our previous
                         // (now failed) session.
-                        await _converse.setUserJID(api.settings.get("jid"));
+                        await setUserJID(api.settings.get("jid"));
                     } else {
-                        await _converse.setUserJID(_converse.bare_jid);
+                        await setUserJID(_converse.bare_jid);
                     }
                     _converse.connection._proto._doDisconnect();
                     _converse.connection._proto = new Strophe.Websocket(_converse.connection);
@@ -173,7 +173,7 @@ export const api = _converse.api = {
                 // When reconnecting anonymously, we need to connect with only
                 // the domain, not the full JID that we had in our previous
                 // (now failed) session.
-                await _converse.setUserJID(api.settings.get("jid"));
+                await setUserJID(api.settings.get("jid"));
             }
 
             if (_converse.connection?.reconnecting) {
@@ -302,7 +302,7 @@ export const api = _converse.api = {
                 return;
             }
             if (jid) {
-                jid = await _converse.setUserJID(jid);
+                jid = await setUserJID(jid);
             }
 
             // See whether there is a BOSH session to re-attach to
@@ -647,30 +647,6 @@ _converse.initConnection = function () {
 }
 
 
-/**
- * Stores the passed in JID for the current user, potentially creating a
- * resource if the JID is bare.
- *
- * Given that we can only create an XMPP connection if we know the domain of
- * the server connect to and we only know this once we know the JID, we also
- * call {@link _converse.initConnection } (if necessary) to make sure that the
- * connection is set up.
- *
- * @method _converse#setUserJID
- * @emits _converse#setUserJID
- * @params { String } jid
- */
-_converse.setUserJID = async function (jid) {
-    await initSession(_converse, jid);
-    /**
-     * Triggered whenever the user's JID has been updated
-     * @event _converse#setUserJID
-     */
-    _converse.api.trigger('setUserJID');
-    return jid;
-}
-
-
 function setUpXMLLogging () {
     const lmap = {}
     lmap[Strophe.LogLevel.DEBUG] = 'debug';

+ 5 - 5
src/headless/plugins/bosh.js

@@ -1,13 +1,13 @@
 /**
- * @module converse-bosh
  * @copyright The Converse.js contributors
  * @license Mozilla Public License (MPLv2)
  * @description Converse.js plugin which add support for XEP-0206: XMPP Over BOSH
  */
 import 'strophe.js/src/bosh';
+import log from "../log.js";
 import { Model } from '@converse/skeletor/src/model.js';
 import { _converse, api, converse } from "../core.js";
-import log from "../log.js";
+import { setUserJID, } from '@converse/headless/utils/init.js';
 
 const { Strophe } = converse.env;
 
@@ -36,13 +36,13 @@ converse.plugins.add('converse-bosh', {
             }
             if (_converse.jid) {
                 if (_converse.bosh_session.get('jid') !== _converse.jid) {
-                    const jid = await _converse.setUserJID(_converse.jid);
+                    const jid = await setUserJID(_converse.jid);
                     _converse.bosh_session.clear({'silent': true });
                     _converse.bosh_session.save({jid});
                 }
             } else { // Keepalive
                 const jid = _converse.bosh_session.get('jid');
-                jid && await _converse.setUserJID(jid);
+                jid && await setUserJID(jid);
             }
             return _converse.bosh_session;
         }
@@ -58,7 +58,7 @@ converse.plugins.add('converse-bosh', {
             xhr.onload = async function () {
                 if (xhr.status >= 200 && xhr.status < 400) {
                     const data = JSON.parse(xhr.responseText);
-                    const jid = await _converse.setUserJID(data.jid);
+                    const jid = await setUserJID(data.jid);
                     _converse.connection.attach(
                         jid,
                         data.sid,

+ 2 - 2
src/headless/shared/connection.js

@@ -5,7 +5,7 @@ import sizzle from 'sizzle';
 import { Strophe } from 'strophe.js/src/core';
 import { _converse, api, clearSession, tearDown } from "../core.js";
 import { getOpenPromise } from '@converse/openpromise';
-
+import { setUserJID, } from '@converse/headless/utils/init.js';
 
 const BOSH_WAIT = 59;
 
@@ -134,7 +134,7 @@ export class Connection extends Strophe.Connection {
     async onConnected (reconnecting) {
         delete this.reconnecting;
         this.flush(); // Solves problem of returned PubSub BOSH response not received by browser
-        await _converse.setUserJID(this.jid);
+        await setUserJID(this.jid);
 
         /**
          * Synchronous event triggered after we've sent an IQ to bind the

+ 23 - 1
src/headless/utils/init.js

@@ -143,6 +143,28 @@ function saveJIDtoSession (_converse, jid) {
     _converse.connection.jid = jid;
 }
 
+/**
+ * Stores the passed in JID for the current user, potentially creating a
+ * resource if the JID is bare.
+ *
+ * Given that we can only create an XMPP connection if we know the domain of
+ * the server connect to and we only know this once we know the JID, we also
+ * call {@link _converse.initConnection } (if necessary) to make sure that the
+ * connection is set up.
+ *
+ * @emits _converse#setUserJID
+ * @params { String } jid
+ */
+export async function setUserJID (jid) {
+    await initSession(_converse, jid);
+    /**
+     * Triggered whenever the user's JID has been updated
+     * @event _converse#setUserJID
+     */
+    _converse.api.trigger('setUserJID');
+    return jid;
+}
+
 export async function initSession (_converse, jid) {
     const is_shared_session = _converse.api.settings.get('connection_options').worker;
 
@@ -237,7 +259,7 @@ function fetchLoginCredentials (wait=0) {
             xhr.onload = () => {
                 if (xhr.status >= 200 && xhr.status < 400) {
                     const data = JSON.parse(xhr.responseText);
-                    _converse.setUserJID(data.jid).then(() => {
+                    setUserJID(data.jid).then(() => {
                         resolve({
                             jid: data.jid,
                             password: data.password