Przeglądaj źródła

Use async/await

JC Brand 6 lat temu
rodzic
commit
fa85fc71b9
2 zmienionych plików z 45 dodań i 66 usunięć
  1. 22 32
      dist/converse.js
  2. 23 34
      src/converse-mam-views.js

+ 22 - 32
dist/converse.js

@@ -52439,7 +52439,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
         });
       },
 
-      fetchArchivedMessages(options) {
+      async fetchArchivedMessages(options) {
         const _converse = this.__super__._converse;
 
         if (this.disable_mam) {
@@ -52457,42 +52457,32 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
           message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes);
         }
 
-        _converse.api.disco.supports(Strophe.NS.MAM, mam_jid).then(results => {
-          // Success
-          if (!results.length) {
-            return;
-          }
+        const supported = await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid);
 
-          this.addSpinner();
+        if (!supported.length) {
+          return;
+        }
 
-          _converse.api.archive.query( // TODO: only query from the last message we have
-          // in our history
-          _.extend({
-            'groupchat': is_groupchat,
-            'before': '',
-            // Page backwards from the most recent message
-            'max': _converse.archived_messages_page_size,
-            'with': this.model.get('jid')
-          }, options), messages => {
-            // Success
-            this.clearSpinner();
-
-            _.each(messages, message_handler);
-          }, e => {
-            // Error
-            this.clearSpinner();
-
-            _converse.log("Error or timeout while trying to fetch " + "archived messages", Strophe.LogLevel.ERROR);
-
-            _converse.log(e, Strophe.LogLevel.ERROR);
-          });
-        }, () => {
+        this.addSpinner();
+
+        _converse.api.archive.query(_.extend({
+          'groupchat': is_groupchat,
+          'before': '',
+          // Page backwards from the most recent message
+          'max': _converse.archived_messages_page_size,
+          'with': this.model.get('jid')
+        }, options), messages => {
+          // Success
+          this.clearSpinner();
+
+          _.each(messages, message_handler);
+        }, e => {
           // Error
-          _converse.log("Error or timeout while checking for MAM support", Strophe.LogLevel.ERROR);
-        }).catch(msg => {
           this.clearSpinner();
 
-          _converse.log(msg, Strophe.LogLevel.FATAL);
+          _converse.log("Error or timeout while trying to fetch " + "archived messages", Strophe.LogLevel.ERROR);
+
+          _converse.log(e, Strophe.LogLevel.ERROR);
         });
       },
 

+ 23 - 34
src/converse-mam-views.js

@@ -80,7 +80,7 @@ converse.plugins.add('converse-mam-views', {
                 });
             },
 
-            fetchArchivedMessages (options) {
+            async fetchArchivedMessages (options) {
                 const { _converse } = this.__super__;
                 if (this.disable_mam) { return; }
 
@@ -95,42 +95,31 @@ converse.plugins.add('converse-mam-views', {
                     message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes)
                 }
 
-                _converse.api.disco.supports(Strophe.NS.MAM, mam_jid).then(
-                    (results) => { // Success
-                        if (!results.length) { return; }
-                        this.addSpinner();
-                        _converse.api.archive.query(
-                            // TODO: only query from the last message we have
-                            // in our history
-                            _.extend({
-                                'groupchat': is_groupchat,
-                                'before': '', // Page backwards from the most recent message
-                                'max': _converse.archived_messages_page_size,
-                                'with': this.model.get('jid'),
-                            }, options),
-                            (messages) => { // Success
-                                this.clearSpinner();
-                                _.each(messages, message_handler);
-                            },
-                            e => { // Error
-                                this.clearSpinner();
-                                _converse.log(
-                                    "Error or timeout while trying to fetch "+
-                                    "archived messages", Strophe.LogLevel.ERROR);
-                                _converse.log(e, Strophe.LogLevel.ERROR);
-                            }
-                        );
+                const supported = await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid);
+                if (!supported.length) {
+                    return;
+                }
+                this.addSpinner();
+                _converse.api.archive.query(
+                    _.extend({
+                        'groupchat': is_groupchat,
+                        'before': '', // Page backwards from the most recent message
+                        'max': _converse.archived_messages_page_size,
+                        'with': this.model.get('jid'),
+                    }, options),
+
+                    messages => { // Success
+                        this.clearSpinner();
+                        _.each(messages, message_handler);
                     },
-                    () => { // Error
+                    e => { // Error
+                        this.clearSpinner();
                         _converse.log(
-                            "Error or timeout while checking for MAM support",
-                            Strophe.LogLevel.ERROR
-                        );
+                            "Error or timeout while trying to fetch "+
+                            "archived messages", Strophe.LogLevel.ERROR);
+                        _converse.log(e, Strophe.LogLevel.ERROR);
                     }
-                ).catch((msg) => {
-                    this.clearSpinner();
-                    _converse.log(msg, Strophe.LogLevel.FATAL);
-                });
+                );
             },
 
             onScroll (ev) {