Explorar el Código

Use `patch` when saving.

When patching, Backobne.browserStorage doesn't return attributes in its
response, thereby avoiding difficult to debug race conditions.
JC Brand hace 6 años
padre
commit
b6c9f057a6
Se han modificado 4 ficheros con 30 adiciones y 37 borrados
  1. 9 6
      src/converse-minimize.js
  2. 0 1
      src/converse-muc-views.js
  3. 18 27
      src/headless/converse-muc.js
  4. 3 3
      src/headless/utils/core.js

+ 9 - 6
src/converse-minimize.js

@@ -52,21 +52,21 @@ converse.plugins.add('converse-minimize', {
                 this.save({
                     'minimized': this.get('minimized') || false,
                     'time_minimized': this.get('time_minimized') || moment(),
-                });
+                }, {'patch': true});
             },
 
             maximize () {
                 u.safeSave(this, {
                     'minimized': false,
                     'time_opened': moment().valueOf()
-                });
+                }, {'patch': true});
             },
 
             minimize () {
                 u.safeSave(this, {
                     'minimized': true,
                     'time_minimized': moment().format()
-                });
+                }, {'patch': true});
             },
         },
 
@@ -140,7 +140,7 @@ converse.plugins.add('converse-minimize', {
                 if (ev && ev.preventDefault) { ev.preventDefault(); }
                 // save the scroll position to restore it on maximize
                 if (this.model.collection && this.model.collection.browserStorage) {
-                    this.model.save({'scroll': this.content.scrollTop});
+                    this.model.save({'scroll': this.content.scrollTop}, {'patch': true});
                 } else {
                     this.model.set({'scroll': this.content.scrollTop});
                 }
@@ -424,7 +424,10 @@ converse.plugins.add('converse-minimize', {
 
             toggle (ev) {
                 if (ev && ev.preventDefault) { ev.preventDefault(); }
-                this.toggleview.model.save({'collapsed': !this.toggleview.model.get('collapsed')});
+                this.toggleview.model.save(
+                    {'collapsed': !this.toggleview.model.get('collapsed')},
+                    {'patch': true}
+                );
                 u.slideToggleElement(this.el.querySelector('.minimized-chats-flyout'), 200);
             },
 
@@ -469,7 +472,7 @@ converse.plugins.add('converse-minimize', {
             },
 
             updateUnreadMessagesCounter () {
-                this.toggleview.model.save({'num_unread': _.sum(this.model.pluck('num_unread'))});
+                this.toggleview.model.save({'num_unread': _.sum(this.model.pluck('num_unread'))}, {'patch': true});
                 this.render();
             }
         });

+ 0 - 1
src/converse-muc-views.js

@@ -673,7 +673,6 @@ converse.plugins.add('converse-muc-views', {
                  */
                 if (u.isPersistableModel(this.model)) {
                     this.model.clearUnreadMsgCounter();
-                    this.model.save();
                 }
                 this.occupantsview.setOccupantsHeight();
                 this.scrollDown();

+ 18 - 27
src/headless/converse-muc.js

@@ -85,7 +85,7 @@ converse.plugins.add('converse-muc', {
             const { _converse } = this.__super__,
                   groupchats = this.chatboxes.where({'type': _converse.CHATROOMS_TYPE});
 
-            _.each(groupchats, gc => u.safeSave(gc, {'connection_status': converse.ROOMSTATUS.DISCONNECTED}));
+            _.each(groupchats, gc => u.safeSave(gc, {'connection_status': converse.ROOMSTATUS.DISCONNECTED}, {'patch': true}));
             this.__super__.tearDown.call(this, arguments);
         },
 
@@ -296,7 +296,7 @@ converse.plugins.add('converse-muc', {
                 if (password) {
                     stanza.cnode(Strophe.xmlElement("password", [], password));
                 }
-                this.save('connection_status', converse.ROOMSTATUS.CONNECTING);
+                this.save({'connection_status': converse.ROOMSTATUS.CONNECTING}, {'patch': true});
                 _converse.api.send(stanza);
                 return this;
             },
@@ -320,7 +320,7 @@ converse.plugins.add('converse-muc', {
                 if (_converse.connection.connected) {
                     this.sendUnavailablePresence(exit_msg);
                 }
-                u.safeSave(this, {'connection_status': converse.ROOMSTATUS.DISCONNECTED});
+                u.safeSave(this, {'connection_status': converse.ROOMSTATUS.DISCONNECTED}, {'patch': true});
                 this.removeHandlers();
             },
 
@@ -427,7 +427,7 @@ converse.plugins.add('converse-muc', {
                  * For example: groupchat@conference.example.org/nickname
                  */
                 if (nick) {
-                    this.save({'nick': nick});
+                    this.save({'nick': nick}, {'patch': true});
                 } else {
                     nick = this.get('nick');
                 }
@@ -531,7 +531,8 @@ converse.plugins.add('converse-muc', {
                     }
                     attrs[fieldname.replace('muc_', '')] = true;
                 });
-                this.features.save(attrs);
+                attrs.description = _.get(fields.findWhere({'var': "muc#roominfo_description"}), 'attributes.value');
+                this.features.save(attrs, {'patch': true});
             },
 
             requestMemberList (affiliation) {
@@ -694,14 +695,10 @@ converse.plugins.add('converse-muc', {
                     const affiliation = item.getAttribute('affiliation');
                     const role = item.getAttribute('role');
                     if (affiliation) {
-                        this.save(
-                            {'affiliation': affiliation},
-                            {'wait': true, 'patch': true});
+                        this.save({'affiliation': affiliation}, {'patch': true});
                     }
                     if (role) {
-                        this.save(
-                            {'role': role},
-                            {'wait': true, 'patch': true});
+                        this.save({'role': role}, {'patch': true});
                     }
                 }
             },
@@ -822,7 +819,7 @@ converse.plugins.add('converse-muc', {
                 this.save({
                     'reserved_nick': nick,
                     'nick': nick
-                }, {'silent': true});
+                }, {'patch': true, 'silent': true});
                 return iq;
             },
 
@@ -1063,37 +1060,34 @@ converse.plugins.add('converse-muc', {
                     const msg = this.messages.create(attrs);
                     this.incrementUnreadMsgCounter(msg);
                     if (forwarded && msg && msg.get('sender')  === 'me') {
-                        msg.save({'received': moment().format()});
+                        msg.save({'received': moment().format()}, {'patch': true});
                     }
                 }
                 _converse.emit('message', {'stanza': original_stanza, 'chatbox': this});
             },
 
-            async onPresence (pres) {
+            onPresence (pres) {
                 /* Handles all MUC presence stanzas.
                  *
                  * Parameters:
                  *  (XMLElement) pres: The stanza
                  */
                 if (pres.getAttribute('type') === 'error') {
-                    this.save('connection_status', converse.ROOMSTATUS.DISCONNECTED);
+                    this.save({'connection_status': converse.ROOMSTATUS.DISCONNECTED}, {'patch': true});
                     return;
                 }
                 const is_self = pres.querySelector("status[code='110']");
                 if (is_self && pres.getAttribute('type') !== 'unavailable') {
-                    await this.onOwnPresence(pres);
+                    this.onOwnPresence(pres);
                 }
                 this.updateOccupantsOnPresence(pres);
 
                 if (this.get('role') !== 'none' && this.get('connection_status') === converse.ROOMSTATUS.CONNECTING) {
-                    await new Promise((success, error) => this.save(
-                        {'connection_status': converse.ROOMSTATUS.CONNECTED},
-                        {'patch': true, 'wait': true, success, error}
-                    ));
+                    this.save({'connection_status': converse.ROOMSTATUS.CONNECTED}, {'patch': true});
                 }
             },
 
-            async onOwnPresence (pres) {
+            onOwnPresence (pres) {
                 /* Handles a received presence relating to the current
                  * user.
                  *
@@ -1134,10 +1128,7 @@ converse.plugins.add('converse-muc', {
                         this.getRoomFeatures();
                     }
                 }
-                await new Promise((success, error) => this.save(
-                    {'connection_status': converse.ROOMSTATUS.ENTERED},
-                    {'patch': true, 'wait': true, success, error}
-                ));
+                this.save({'connection_status': converse.ROOMSTATUS.ENTERED}, {'patch': true});
             },
 
             isUserMentioned (message) {
@@ -1180,7 +1171,7 @@ converse.plugins.add('converse-muc', {
                 u.safeSave(this, {
                     'num_unread': 0,
                     'num_unread_general': 0
-                });
+                }, {'patch': true});
             }
         });
 
@@ -1398,7 +1389,7 @@ converse.plugins.add('converse-muc', {
              */
             _converse.chatboxes.each(function (model) {
                 if (model.get('type') === _converse.CHATROOMS_TYPE) {
-                    model.save('connection_status', converse.ROOMSTATUS.DISCONNECTED);
+                    model.save({'connection_status': converse.ROOMSTATUS.DISCONNECTED}, {'patch': true});
                 }
             });
         }

+ 3 - 3
src/headless/utils/core.js

@@ -318,11 +318,11 @@ u.onMultipleEvents = function (events=[], callback) {
     _.each(events, (map) => map.object.on(map.event, handler));
 };
 
-u.safeSave = function (model, attributes) {
+u.safeSave = function (model, attributes, options) {
     if (u.isPersistableModel(model)) {
-        model.save(attributes);
+        model.save(attributes, options);
     } else {
-        model.set(attributes);
+        model.set(attributes, options);
     }
 };