Browse Source

Move connection api into separate file

JC Brand 3 năm trước cách đây
mục cha
commit
0a07cad2cb

+ 5 - 64
src/headless/core.js

@@ -5,6 +5,7 @@
 import URI from 'urijs';
 import _converse from '@converse/headless/shared/_converse';
 import advancedFormat from 'dayjs/plugin/advancedFormat';
+import connection_api from '@converse/headless/shared/connection/api.js';
 import dayjs from 'dayjs';
 import i18n from '@converse/headless/shared/i18n';
 import invoke from 'lodash-es/invoke';
@@ -13,10 +14,10 @@ import log from '@converse/headless/log.js';
 import pluggable from 'pluggable.js/src/pluggable.js';
 import sizzle from 'sizzle';
 import u, { setUnloadEvent, replacePromise } from '@converse/headless/utils/core.js';
+import { CHAT_STATES, KEYCODES } from './shared/constants.js';
 import { Collection } from "@converse/skeletor/src/collection";
-import { Connection, MockConnection } from '@converse/headless/shared/connection.js';
+import { Connection, MockConnection } from '@converse/headless/shared/connection/index.js';
 import { Events } from '@converse/skeletor/src/events.js';
-import { CHAT_STATES, KEYCODES } from './shared/constants.js';
 import { Model } from '@converse/skeletor/src/model.js';
 import { Strophe, $build, $iq, $msg, $pres } from 'strophe.js/src/strophe';
 import { TimeoutError } from '@converse/headless/shared/errors';
@@ -101,66 +102,9 @@ pluggable.enable(_converse, '_converse', 'pluggable');
  * @memberOf _converse
  */
 export const api = _converse.api = {
-    /**
-     * This grouping collects API functions related to the XMPP connection.
-     *
-     * @namespace _converse.api.connection
-     * @memberOf _converse.api
-     */
-    connection: {
-        /**
-         * @method _converse.api.connection.connected
-         * @memberOf _converse.api.connection
-         * @returns {boolean} Whether there is an established connection or not.
-         */
-        connected () {
-            return _converse?.connection?.connected && true;
-        },
-
-        /**
-         * Terminates the connection.
-         *
-         * @method _converse.api.connection.disconnect
-         * @memberOf _converse.api.connection
-         */
-        disconnect () {
-            if (_converse.connection) {
-                _converse.connection.disconnect();
-            }
-        },
 
-        /**
-         * Can be called once the XMPP connection has dropped and we want
-         * to attempt reconnection.
-         * Only needs to be called once, if reconnect fails Converse will
-         * attempt to reconnect every two seconds, alternating between BOSH and
-         * Websocket if URLs for both were provided.
-         * @method reconnect
-         * @memberOf _converse.api.connection
-         */
-        reconnect () {
-            const { __, connection } = _converse;
-            connection.setConnectionStatus(
-                Strophe.Status.RECONNECTING,
-                __('The connection has dropped, attempting to reconnect.')
-            );
-            if (connection?.reconnecting) {
-                return connection.debouncedReconnect();
-            } else {
-                return connection.reconnect();
-            }
-        },
-
-        /**
-         * Utility method to determine the type of connection we have
-         * @method isType
-         * @memberOf _converse.api.connection
-         * @returns {boolean}
-         */
-        isType (type) {
-            return _converse.connection.isType(type);
-        }
-    },
+    connection: connection_api,
+    settings: settings_api,
 
     /**
      * Lets you trigger events, which can be listened to via
@@ -318,9 +262,6 @@ export const api = _converse.api = {
         }
     },
 
-    settings: settings_api,
-
-
     /**
      * Converse and its plugins trigger various events which you can listen to via the
      * {@link _converse.api.listen} namespace.

+ 63 - 0
src/headless/shared/connection/api.js

@@ -0,0 +1,63 @@
+import _converse from '@converse/headless/shared/_converse.js';
+import { Strophe } from 'strophe.js/src/strophe.js';
+
+/**
+ * This grouping collects API functions related to the XMPP connection.
+ *
+ * @namespace _converse.api.connection
+ * @memberOf _converse.api
+ */
+export default {
+    /**
+     * @method _converse.api.connection.connected
+     * @memberOf _converse.api.connection
+     * @returns {boolean} Whether there is an established connection or not.
+     */
+    connected () {
+        return _converse?.connection?.connected && true;
+    },
+
+    /**
+     * Terminates the connection.
+     *
+     * @method _converse.api.connection.disconnect
+     * @memberOf _converse.api.connection
+     */
+    disconnect () {
+        if (_converse.connection) {
+            _converse.connection.disconnect();
+        }
+    },
+
+    /**
+     * Can be called once the XMPP connection has dropped and we want
+     * to attempt reconnection.
+     * Only needs to be called once, if reconnect fails Converse will
+     * attempt to reconnect every two seconds, alternating between BOSH and
+     * Websocket if URLs for both were provided.
+     * @method reconnect
+     * @memberOf _converse.api.connection
+     */
+    reconnect () {
+        const { __, connection } = _converse;
+        connection.setConnectionStatus(
+            Strophe.Status.RECONNECTING,
+            __('The connection has dropped, attempting to reconnect.')
+        );
+        if (connection?.reconnecting) {
+            return connection.debouncedReconnect();
+        } else {
+            return connection.reconnect();
+        }
+    },
+
+    /**
+     * Utility method to determine the type of connection we have
+     * @method isType
+     * @memberOf _converse.api.connection
+     * @returns {boolean}
+     */
+    isType (type) {
+        return _converse.connection.isType(type);
+    }
+};

+ 3 - 3
src/headless/shared/connection.js → src/headless/shared/connection/index.js

@@ -1,10 +1,10 @@
 import debounce from 'lodash-es/debounce';
 import isElement from 'lodash-es/isElement';
-import log from "../log.js";
+import log from "@converse/headless/log.js";
 import sizzle from 'sizzle';
 import { BOSH_WAIT } from '@converse/headless/shared/constants.js';
-import { Strophe } from 'strophe.js/src/core';
-import { _converse, api, clearSession } from "../core.js";
+import { Strophe } from 'strophe.js/src/core.js';
+import { _converse, api, clearSession } from "@converse/headless/core.js";
 import { getOpenPromise } from '@converse/openpromise';
 import { setUserJID, } from '@converse/headless/utils/init.js';
 import { tearDown } from '@converse/headless/utils/core.js';

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

@@ -5,7 +5,7 @@ import localDriver from 'localforage-webextensionstorage-driver/local';
 import log from '@converse/headless/log';
 import syncDriver from 'localforage-webextensionstorage-driver/sync';
 import { CORE_PLUGINS } from '@converse/headless/shared/constants.js';
-import { Connection } from '@converse/headless/shared/connection.js';
+import { Connection } from '@converse/headless/shared/connection/index.js';
 import { Model } from '@converse/skeletor/src/model.js';
 import { Strophe } from 'strophe.js/src/strophe';
 import { createStore, initStorage } from '@converse/headless/utils/storage.js';

+ 4 - 3
src/plugins/muc-views/heading.js

@@ -177,9 +177,10 @@ export default class MUCHeading extends CustomElement {
             });
         }
 
-        const chatview = _converse.chatboxviews.get(this.getAttribute('jid'));
-        if (chatview) {
-            return _converse.api.hook('getHeadingButtons', chatview, buttons);
+        const el = _converse.chatboxviews.get(this.getAttribute('jid'));
+        if (el) {
+            // This hook is described in src/plugins/chatview/heading.js
+            return _converse.api.hook('getHeadingButtons', el, buttons);
         } else {
             return Promise.resolve(buttons); // Happens during tests
         }