Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Clear local storage settings handler cache on logout #8454

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { setSentryUser } from "./sentry";
import SdkConfig from "./SdkConfig";
import { DialogOpener } from "./utils/DialogOpener";
import { Action } from "./dispatcher/actions";
import AbstractLocalStorageSettingsHandler from "./settings/handlers/AbstractLocalStorageSettingsHandler";

const HOMESERVER_URL_KEY = "mx_hs_url";
const ID_SERVER_URL_KEY = "mx_is_url";
Expand Down Expand Up @@ -878,6 +879,7 @@ async function clearStorage(opts?: { deleteEverything?: boolean }): Promise<void
const registrationTime = window.localStorage.getItem("mx_registration_time");

window.localStorage.clear();
AbstractLocalStorageSettingsHandler.clear();

try {
await StorageManager.idbDelete("account", "mx_access_token");
Expand Down
56 changes: 34 additions & 22 deletions src/settings/handlers/AbstractLocalStorageSettingsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,76 @@ import SettingsHandler from "./SettingsHandler";
* by caching the values and listening for localStorage updates from other tabs.
*/
export default abstract class AbstractLocalStorageSettingsHandler extends SettingsHandler {
private itemCache = new Map<string, any>();
private objectCache = new Map<string, object>();
// Shared cache between all subclass instances
private static itemCache = new Map<string, any>();
private static objectCache = new Map<string, object>();
private static storageListenerBound = false;

private static onStorageEvent = (e: StorageEvent) => {
if (e.key === null) {
AbstractLocalStorageSettingsHandler.clear();
} else {
AbstractLocalStorageSettingsHandler.itemCache.delete(e.key);
AbstractLocalStorageSettingsHandler.objectCache.delete(e.key);
}
};

// Expose the clear event for Lifecycle to call, the storage listener only fires for changes from other tabs
public static clear() {
AbstractLocalStorageSettingsHandler.itemCache.clear();
AbstractLocalStorageSettingsHandler.objectCache.clear();
}

protected constructor() {
super();

// Listen for storage changes from other tabs to bust the cache
window.addEventListener("storage", (e: StorageEvent) => {
if (e.key === null) {
this.itemCache.clear();
this.objectCache.clear();
} else {
this.itemCache.delete(e.key);
this.objectCache.delete(e.key);
}
});
if (!AbstractLocalStorageSettingsHandler.storageListenerBound) {
AbstractLocalStorageSettingsHandler.storageListenerBound = true;
// Listen for storage changes from other tabs to bust the cache
window.addEventListener("storage", AbstractLocalStorageSettingsHandler.onStorageEvent);
}
}

protected getItem(key: string): any {
if (!this.itemCache.has(key)) {
if (!AbstractLocalStorageSettingsHandler.itemCache.has(key)) {
const value = localStorage.getItem(key);
this.itemCache.set(key, value);
AbstractLocalStorageSettingsHandler.itemCache.set(key, value);
return value;
}

return this.itemCache.get(key);
return AbstractLocalStorageSettingsHandler.itemCache.get(key);
}

protected getObject<T extends object>(key: string): T | null {
if (!this.objectCache.has(key)) {
if (!AbstractLocalStorageSettingsHandler.objectCache.has(key)) {
try {
const value = JSON.parse(localStorage.getItem(key));
this.objectCache.set(key, value);
AbstractLocalStorageSettingsHandler.objectCache.set(key, value);
return value;
} catch (err) {
console.error("Failed to parse localStorage object", err);
return null;
}
}

return this.objectCache.get(key) as T;
return AbstractLocalStorageSettingsHandler.objectCache.get(key) as T;
}

protected setItem(key: string, value: any): void {
this.itemCache.set(key, value);
AbstractLocalStorageSettingsHandler.itemCache.set(key, value);
localStorage.setItem(key, value);
}

protected setObject(key: string, value: object): void {
this.objectCache.set(key, value);
AbstractLocalStorageSettingsHandler.objectCache.set(key, value);
localStorage.setItem(key, JSON.stringify(value));
}

// handles both items and objects
protected removeItem(key: string): void {
localStorage.removeItem(key);
this.itemCache.delete(key);
this.objectCache.delete(key);
AbstractLocalStorageSettingsHandler.itemCache.delete(key);
AbstractLocalStorageSettingsHandler.objectCache.delete(key);
}

public isSupported(): boolean {
Expand Down