diff options
author | Paladox none <thomasmulhall410@yahoo.com> | 2024-04-20 21:20:41 +0000 |
---|---|---|
committer | paladox <thomasmulhall410@yahoo.com> | 2024-04-22 12:49:01 +0100 |
commit | c2451865aadcefac4b457f60e28752c62f6637db (patch) | |
tree | 77a0955c67b70c159dc6b0b41180f38d5946b6e4 | |
parent | ccedf64fe81829af4e907f1ee58cb7e198a8a9e0 (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 347e24c021..edbd6a38bf 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 @@ -686,6 +686,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) { @@ -834,7 +837,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, + }); } } |