1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Cache failed capabilities lookups for shorter amounts of time

This should fix https://github.com/vector-im/riot-web/issues/9225 for showing up too often/too long.
This commit is contained in:
Travis Ralston
2019-04-08 12:24:00 -06:00
parent 420b4d119d
commit 663c096400

View File

@@ -445,9 +445,10 @@ MatrixClient.prototype.setNotifTimelineSet = function(notifTimelineSet) {
* @return {module:http-api.MatrixError} Rejects: with an error response. * @return {module:http-api.MatrixError} Rejects: with an error response.
*/ */
MatrixClient.prototype.getCapabilities = function() { MatrixClient.prototype.getCapabilities = function() {
const now = new Date().getTime();
if (this._cachedCapabilities) { if (this._cachedCapabilities) {
const now = new Date().getTime(); if (now < this._cachedCapabilities.expiration) {
if (now - this._cachedCapabilities.lastUpdated <= CAPABILITIES_CACHE_MS) {
console.log("Returning cached capabilities"); console.log("Returning cached capabilities");
return Promise.resolve(this._cachedCapabilities.capabilities); return Promise.resolve(this._cachedCapabilities.capabilities);
} }
@@ -459,9 +460,16 @@ MatrixClient.prototype.getCapabilities = function() {
).catch(() => null).then((r) => { ).catch(() => null).then((r) => {
if (!r) r = {}; if (!r) r = {};
const capabilities = r["capabilities"] || {}; const capabilities = r["capabilities"] || {};
// If the capabilities missed the cache, cache it for a shorter amount
// of time to try and refresh them later.
const cacheMs = Object.keys(capabilities).length
? CAPABILITIES_CACHE_MS
: 60000 + (Math.random() * 5000);
this._cachedCapabilities = { this._cachedCapabilities = {
capabilities: capabilities, capabilities: capabilities,
lastUpdated: new Date().getTime(), expiration: now + cacheMs,
}; };
console.log("Caching capabilities: ", capabilities); console.log("Caching capabilities: ", capabilities);