Project

General

Profile

Feature #44194 ยป 0001-Cache-mention-autocomplete-responses-to-avoid-redund.patch

Go MAEDA, 2026-06-19 07:53

View differences:

app/assets/javascripts/application-legacy.js
1347 1347
      xhr.send();
1348 1348
    }, 200);
1349 1349

  
1350
    const autocompleteSearchCache = {};
1351
    const latestAutocompleteSearchQuery = {};
1352

  
1353
    const cachedAutocompleteSearch = function(url, text) {
1354
      const cache = autocompleteSearchCache[url];
1355

  
1356
      if (!cache) {
1357
        return null;
1358
      }
1359

  
1360
      if (text === cache.query) {
1361
        return cache.results;
1362
      }
1363

  
1364
      if (cache.query && text.startsWith(cache.query) && cache.results.length === 0) {
1365
        return [];
1366
      }
1367

  
1368
      return null;
1369
    }
1370

  
1350 1371
    const tribute = new Tribute({
1351 1372
      collection: [
1352 1373
        {
......
1404 1425
          },
1405 1426
          values: function (text, cb) {
1406 1427
            const url = getDataSource('users');
1407
            if (url) {
1408
              remoteSearch(url + encodeURIComponent(text), function (users) {
1409
                return cb(users);
1410
              });
1428

  
1429
            if (!url) {
1430
              return cb([]);
1431
            }
1432

  
1433
            const cachedUsers = cachedAutocompleteSearch(url, text);
1434
            if (cachedUsers !== null) {
1435
              return cb(cachedUsers);
1411 1436
            }
1437

  
1438
            latestAutocompleteSearchQuery[url] = text;
1439

  
1440
            remoteSearch(url + encodeURIComponent(text), function (users) {
1441
              if (latestAutocompleteSearchQuery[url] !== text) {
1442
                return;
1443
              }
1444

  
1445
              autocompleteSearchCache[url] = {
1446
                query: text,
1447
                results: users
1448
              };
1449
              return cb(users);
1450
            });
1412 1451
          },
1413 1452
          menuItemTemplate: function (user) {
1414 1453
            return sanitizeHTML(user.original.name);
    (1-1/1)