diff --git a/src/matrix/storage/idb/StorageFactory.ts b/src/matrix/storage/idb/StorageFactory.ts index 1f64baf3..44d481eb 100644 --- a/src/matrix/storage/idb/StorageFactory.ts +++ b/src/matrix/storage/idb/StorageFactory.ts @@ -16,11 +16,12 @@ limitations under the License. import {IDOMStorage} from "./types"; import {Storage} from "./Storage"; -import { openDatabase, reqAsPromise } from "./utils"; -import { exportSession, importSession, Export } from "./export"; -import { schema } from "./schema"; -import { detectWebkitEarlyCloseTxnBug } from "./quirks"; -import { ILogItem } from "../../../logging/types"; +import {openDatabase, reqAsPromise} from "./utils"; +import {exportSession, importSession, Export} from "./export"; +import {schema} from "./schema"; +import {detectWebkitEarlyCloseTxnBug} from "./quirks"; +import {ILogItem} from "../../../logging/types"; +import {clearKeysFromLocalStorage} from "./stores/SessionStore"; const sessionName = (sessionId: string) => `hydrogen_session_${sessionId}`; const openDatabaseWithSessionId = function(sessionId: string, idbFactory: IDBFactory, localStorage: IDOMStorage, log: ILogItem) { @@ -79,6 +80,7 @@ export class StorageFactory { delete(sessionId: string): Promise { const databaseName = sessionName(sessionId); + clearKeysFromLocalStorage(this._localStorage, databaseName); const req = this._idbFactory.deleteDatabase(databaseName); return reqAsPromise(req); } diff --git a/src/matrix/storage/idb/stores/SessionStore.ts b/src/matrix/storage/idb/stores/SessionStore.ts index 7faedc41..9ae9bb7e 100644 --- a/src/matrix/storage/idb/stores/SessionStore.ts +++ b/src/matrix/storage/idb/stores/SessionStore.ts @@ -24,6 +24,23 @@ export interface SessionEntry { value: any; } +function getLocalStorageKeyPrefix(databaseName: string): string { + return `${databaseName}.session.`; +} + +export function clearKeysFromLocalStorage(localStorage: IDOMStorage, databaseName: string): void { + const keys: string[] = []; + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i); + if (key?.startsWith(getLocalStorageKeyPrefix(databaseName))) { + keys.push(key); + } + } + for (const key of keys) { + localStorage.removeItem(key); + } +} + export class SessionStore { private _sessionStore: Store private _localStorage: IDOMStorage; @@ -34,7 +51,7 @@ export class SessionStore { } private get _localStorageKeyPrefix(): string { - return `${this._sessionStore.databaseName}.session.`; + return getLocalStorageKeyPrefix(this._sessionStore.databaseName); } async get(key: string): Promise {