Skip to content

Commit f2384a1

Browse files
committed
fix: update LocalStorage and fix types
1 parent e7ba3ac commit f2384a1

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/sessionManager/stores/localStorage.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import { storageSettings } from "../index.js";
2-
import { StorageKeys, type SessionManager } from "../types.js";
2+
import { SessionBase, StorageKeys, type SessionManager } from "../types.js";
33
import { splitString } from "../utils.js";
44

55
/**
66
* Provides a localStorage based session manager implementation for the browser.
77
* @class LocalStorage
88
*/
9-
export class LocalStorage<V = StorageKeys> implements SessionManager<V> {
9+
export class LocalStorage<V extends string = StorageKeys> extends SessionBase<V> implements SessionManager<V> {
1010
constructor() {
11+
super();
1112
console.warn("LocalStorage store should not be used in production");
1213
}
1314

14-
setItems: Set<V | StorageKeys> = new Set<V>();
15+
private internalItems: Set<V | StorageKeys> = new Set<V>();
1516

1617
/**
1718
* Clears all items from session store.
1819
* @returns {void}
1920
*/
2021
async destroySession(): Promise<void> {
21-
this.setItems.forEach((key) => {
22+
this.internalItems.forEach((key) => {
2223
this.removeSessionItem(key);
2324
});
2425
}
@@ -35,7 +36,7 @@ export class LocalStorage<V = StorageKeys> implements SessionManager<V> {
3536
): Promise<void> {
3637
// clear items first
3738
await this.removeSessionItem(itemKey);
38-
this.setItems.add(itemKey);
39+
this.internalItems.add(itemKey);
3940

4041
if (typeof itemValue === "string") {
4142
splitString(itemValue, storageSettings.maxLength).forEach(
@@ -97,6 +98,6 @@ export class LocalStorage<V = StorageKeys> implements SessionManager<V> {
9798

9899
index++;
99100
}
100-
this.setItems.delete(itemKey);
101+
this.internalItems.delete(itemKey);
101102
}
102103
}

lib/sessionManager/types.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export abstract class SessionBase<V extends string = StorageKeys>
3131
abstract removeSessionItem(itemKey: V | StorageKeys): Awaitable<void>;
3232
abstract destroySession(): Awaitable<void>;
3333

34-
async setItems(items: Partial<Record<V, unknown>>): Promise<void> {
34+
async setItems(items: Partial<Record<V, unknown>>): Awaitable<void> {
3535
await Promise.all(
3636
Object.entries(items).map(([key, value]) => {
3737
return this.setSessionItem(key as V | StorageKeys, value);
@@ -72,5 +72,11 @@ export interface SessionManager<V extends string = StorageKeys> {
7272
*/
7373
destroySession: () => Awaitable<void>;
7474

75-
setItems(items: Record<V, unknown>): void;
75+
76+
/**
77+
* Sets multiple items simultaneously.
78+
* @param {Record<V | StorageKeys, unknown>} items - Object containing key-value pairs to store
79+
* @returns {Promise<void>}
80+
*/
81+
setItems(items: Partial<Record<V, unknown>>): Awaitable<void>;
7682
}

0 commit comments

Comments
 (0)