فهرست منبع

Explicitly sort messages before pruning

to avoid a race-condition where messages aren't sorted and the wrong
message gets pruned.
JC Brand 3 سال پیش
والد
کامیت
82fb263438
2فایلهای تغییر یافته به همراه6 افزوده شده و 3 حذف شده
  1. 2 2
      src/headless/plugins/chat/model.js
  2. 4 1
      src/headless/shared/chat/utils.js

+ 2 - 2
src/headless/plugins/chat/model.js

@@ -6,7 +6,7 @@ import log from '@converse/headless/log';
 import pick from "lodash-es/pick";
 import { Model } from '@converse/skeletor/src/model.js';
 import { _converse, api, converse } from "../../core.js";
-import { debouncedPruneHistory, pruneHistory } from '@converse/headless/shared/chat/utils.js';
+import { debouncedPruneHistory } from '@converse/headless/shared/chat/utils.js';
 import { getMediaURLsMetadata } from '@converse/headless/shared/parsers.js';
 import { getOpenPromise } from '@converse/openpromise';
 import { initStorage } from '@converse/headless/utils/storage.js';
@@ -350,7 +350,7 @@ const ChatBox = ModelWithContact.extend({
             api.settings.get('pruning_behavior') === 'unscrolled' &&
             !this.ui.get('scrolled')
         ) {
-            pruneHistory(this);
+            debouncedPruneHistory(this);
         }
     },
 

+ 4 - 1
src/headless/shared/chat/utils.js

@@ -7,6 +7,9 @@ export function pruneHistory (model) {
     const max_history = api.settings.get('prune_messages_above');
     if (max_history && typeof max_history === 'number') {
         if (model.messages.length > max_history) {
+
+            model.messages.sort(); // Explicitly call sort() to avoid race-conditions
+
             const non_empty_messages = model.messages.filter((m) => !u.isEmptyMessage(m));
             if (non_empty_messages.length > max_history) {
                 while (non_empty_messages.length > max_history) {
@@ -58,4 +61,4 @@ export function getMediaURLs (arr, text, offset=0) {
     }).filter(o => o);
 }
 
-export const debouncedPruneHistory = debounce(pruneHistory, 250);
+export const debouncedPruneHistory = debounce(pruneHistory, 500);