summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaladox none <thomasmulhall410@yahoo.com>2024-04-20 21:20:41 +0000
committerPaladox none <thomasmulhall410@yahoo.com>2024-04-22 11:28:48 +0000
commit8d839163c2b09c6ecd04e239df72023d048574a9 (patch)
tree695bb192f1fb1cc2fcf4bd979b58bda3419b4f2e
parenta9514d93f89a8b25962a09682fc7798620c23a7a (diff)
Fix clearing cache in gr-rest-api
We include the base url in urlWithParams so we have to do the same inside gr-rest-api by calling getBaseUrl(). Also clear cache in savePreferences(). Release-Notes: Fix clearing cache in gr-rest-api Bug: 335613081 Change-Id: Ic0fb6af3316c8cd77bcaa64b802cd777cff339d7 (cherry picked from commit 6915b8aa40620176313ec3b55aef90e28a3e6936)
-rw-r--r--polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts36
-rw-r--r--polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts8
2 files changed, 28 insertions, 16 deletions
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts
index 85f2c062b9..2da1fb8708 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts
@@ -45,6 +45,13 @@ export function parsePrefixedJSON(jsonWithPrefix: string): ParsedJSON {
return JSON.parse(jsonWithPrefix.substring(JSON_PREFIX.length)) as ParsedJSON;
}
+// Adds base url if not added in cache key
+// or doesn't add it if it already is there.
+function addBaseUrl(key: string) {
+ if (!getBaseUrl()) return key;
+ return key.startsWith(getBaseUrl()) ? key : getBaseUrl() + key;
+}
+
/**
* Wrapper around Map for caching server responses. Site-based so that
* changes to CANONICAL_PATH will result in a different cache going into
@@ -66,44 +73,43 @@ export class SiteBasedCache {
// TODO(kamilm): This implies very strict format of what is stored in
// INITIAL_DATA which is not clear from the name, consider renaming.
Object.entries(window.INITIAL_DATA).forEach(e =>
- this._cache().set(e[0], e[1] as unknown as ParsedJSON)
+ this._cache().set(addBaseUrl(e[0]), e[1] as unknown as ParsedJSON)
);
}
}
// Returns the cache for the current canonical path.
_cache(): Map<string, ParsedJSON> {
- const canonical_path = window.CANONICAL_PATH ?? '';
- if (!this.data.has(canonical_path)) {
- this.data.set(canonical_path, new Map<string, ParsedJSON>());
+ if (!this.data.has(getBaseUrl())) {
+ this.data.set(getBaseUrl(), new Map<string, ParsedJSON>());
}
- return this.data.get(canonical_path)!;
+ return this.data.get(getBaseUrl())!;
}
has(key: string) {
- return this._cache().has(key);
+ return this._cache().has(addBaseUrl(key));
}
get(key: string): ParsedJSON | undefined {
- return this._cache().get(key);
+ return this._cache().get(addBaseUrl(key));
}
set(key: string, value: ParsedJSON) {
- this._cache().set(key, value);
+ this._cache().set(addBaseUrl(key), value);
}
delete(key: string) {
- this._cache().delete(key);
+ this._cache().delete(addBaseUrl(key));
}
invalidatePrefix(prefix: string) {
const newMap = new Map<string, ParsedJSON>();
for (const [key, value] of this._cache().entries()) {
- if (!key.startsWith(prefix)) {
+ if (!key.startsWith(addBaseUrl(prefix))) {
newMap.set(key, value);
}
}
- this.data.set(window.CANONICAL_PATH ?? '', newMap);
+ this.data.set(getBaseUrl(), newMap);
}
}
@@ -129,11 +135,11 @@ export class FetchPromisesCache {
* @return true only if a value for a key sets and it is not undefined
*/
has(key: string): boolean {
- return !!this.data[key];
+ return !!this.data[addBaseUrl(key)];
}
get(key: string) {
- return this.data[key];
+ return this.data[addBaseUrl(key)];
}
/**
@@ -141,13 +147,13 @@ export class FetchPromisesCache {
* mark key as deleted.
*/
set(key: string, value: Promise<ParsedJSON | undefined> | undefined) {
- this.data[key] = value;
+ this.data[addBaseUrl(key)] = value;
}
invalidatePrefix(prefix: string) {
const newData: FetchPromisesCacheData = {};
Object.entries(this.data).forEach(([key, value]) => {
- if (!key.startsWith(prefix)) {
+ if (!key.startsWith(addBaseUrl(prefix))) {
newData[key] = value;
}
});
diff --git a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts
index 2b5842aca2..2d43c80cd3 100644
--- a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts
+++ b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts
@@ -664,6 +664,9 @@ export class GrRestApiServiceImpl implements RestApiService, Finalizable {
savePreferences(
prefs: PreferencesInput
): Promise<PreferencesInfo | undefined> {
+ // Invalidate the cache.
+ this._cache.delete('/accounts/self/preferences');
+
// Note (Issue 5142): normalize the download scheme with lower case before
// saving.
if (prefs.download_scheme) {
@@ -858,7 +861,10 @@ export class GrRestApiServiceImpl implements RestApiService, Finalizable {
const cachedAccount = this._cache.get('/accounts/self/detail');
if (cachedAccount) {
// Replace object in cache with new object to force UI updates.
- this._cache.set('/accounts/self/detail', {...cachedAccount, ...obj});
+ this._cache.set('/accounts/self/detail', {
+ ...cachedAccount,
+ ...obj,
+ });
}
}