diff options
author | Paladox none <thomasmulhall410@yahoo.com> | 2024-04-20 21:20:41 +0000 |
---|---|---|
committer | paladox <thomasmulhall410@yahoo.com> | 2024-04-22 12:51:15 +0100 |
commit | b1c11af64a57601426c09bcace181ed838388eed (patch) | |
tree | 4aa6d8405e562e755e683ea2528ae63162d1bca0 | |
parent | 86be8e24bef2fdf474239c6eee747ffae6646d4b (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.ts | 41 | ||||
-rw-r--r-- | polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts | 8 |
2 files changed, 28 insertions, 21 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 108451217a..c048489f6a 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 @@ -59,6 +59,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 @@ -78,27 +85,21 @@ export class SiteBasedCache { // so that we spare more round trips to the server when the app loads // initially. 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, unknown> { - if (!this.data.has(window.CANONICAL_PATH)) { - this.data.set( - window.CANONICAL_PATH, - new Map<string, ParsedJSON | null>() - ); + if (!this.data.has(getBaseUrl())) { + this.data.set(getBaseUrl(), new Map<string, ParsedJSON | null>()); } - return this.data.get(window.CANONICAL_PATH) as Map< - string, - ParsedJSON | null - >; + return this.data.get(getBaseUrl()) as Map<string, ParsedJSON | null>; } has(key: string) { - return this._cache().has(key); + return this._cache().has(addBaseUrl(key)); } get(key: '/accounts/self/emails'): EmailInfo[] | null; @@ -108,7 +109,7 @@ export class SiteBasedCache { get(key: string): ParsedJSON | null; get(key: string): unknown { - return this._cache().get(key); + return this._cache().get(addBaseUrl(key)); } set(key: '/accounts/self/emails', value: EmailInfo[]): void; @@ -118,21 +119,21 @@ export class SiteBasedCache { set(key: string, value: ParsedJSON | null): void; set(key: string, value: unknown) { - 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, unknown>(); 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); } } @@ -155,11 +156,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)]; } /** @@ -167,13 +168,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 bbb47c26ee..1657c23438 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 @@ -691,6 +691,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) { @@ -871,7 +874,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, + }); } } |