Skip to content

Commit

Permalink
add remove item and reset store
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Jan 31, 2024
1 parent 5b338b9 commit 5e35f5f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/openlogin-utils/src/browserStorage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { IStorage, storageAvailable } from "./utils";

export class MemoryStore implements IStorage {
store: Record<string, string> = {};
store: Map<string, string> = new Map();

getItem(key: string): string {
return this.store[key] || null;
return this.store.get(key) || null;
}

setItem(key: string, value: string): void {
this.store[key] = value;
this.store.set(key, value);
}

removeItem(key: string): void {
this.store.delete(key);
}
}

Expand All @@ -31,7 +35,7 @@ export class BrowserStorage {
}
}

static getInstance(key: string, storageKey: "session" | "local" = "local"): BrowserStorage {
static getInstance(key: string, storageKey: "session" | "local" | "memory" = "local"): BrowserStorage {
if (!this.instanceMap.has(key)) {
let storage: IStorage;
if (storageKey === "local" && storageAvailable("localStorage")) {
Expand All @@ -53,7 +57,7 @@ export class BrowserStorage {

resetStore(): Record<string, unknown> {
const currStore = this.getStore();
this.storage.setItem(this._storeKey, JSON.stringify({}));
this.storage.removeItem(this._storeKey);
return currStore;
}

Expand Down
1 change: 1 addition & 0 deletions packages/openlogin-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function jsonToBase64<T = Record<string, unknown>>(json: T): string {
export interface IStorage {
getItem(key: string): string;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}

export function storageAvailable(type: string): boolean {
Expand Down

0 comments on commit 5e35f5f

Please sign in to comment.