Skip to content

Commit 34a7298

Browse files
committed
fix: sessionStore issues
1 parent 104f3f8 commit 34a7298

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

lib/sessionManager/stores/chromeStore.ts

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

5-
function getStorageValue(key: string) {
5+
function getStorageValue(key: string): unknown | undefined {
66
return new Promise((resolve, reject) => {
77
chrome.storage.local.get([key], function (result) {
88
if (chrome.runtime.lastError) {
9-
reject(chrome.runtime.lastError);
9+
reject(undefined);
1010
} else {
1111
resolve(result[key]);
1212
}
@@ -37,11 +37,14 @@ export class ChromeStore<V = StorageKeys> implements SessionManager<V> {
3737
itemKey: V | StorageKeys,
3838
itemValue: unknown,
3939
): Promise<void> {
40+
// clear items first
41+
await this.removeSessionItem(itemKey);
42+
4043
if (typeof itemValue === "string") {
4144
splitString(itemValue, storageSettings.maxLength).forEach(
42-
async (_, index) => {
45+
async (splitValue, index) => {
4346
await chrome.storage.local.set({
44-
[`${storageSettings.keyPrefix}${itemKey}${index}`]: itemValue,
47+
[`${storageSettings.keyPrefix}${itemKey}${index}`]: splitValue,
4548
});
4649
},
4750
);
@@ -83,11 +86,11 @@ export class ChromeStore<V = StorageKeys> implements SessionManager<V> {
8386
// remove items from the chrome.storage
8487
let index = 0;
8588
while (
86-
chrome.storage.local.get(
89+
(await getStorageValue(
8790
`${storageSettings.keyPrefix}${String(itemKey)}${index}`,
88-
) !== undefined
91+
)) !== undefined
8992
) {
90-
chrome.storage.local.remove(
93+
await chrome.storage.local.remove(
9194
`${storageSettings.keyPrefix}${String(itemKey)}${index}`,
9295
);
9396
index++;

lib/sessionManager/stores/memory.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ export class MemoryStorage<V = StorageKeys> implements SessionManager<V> {
2727
itemKey: V | StorageKeys,
2828
itemValue: unknown,
2929
): Promise<void> {
30+
// clear items first
31+
await this.removeSessionItem(itemKey);
32+
3033
if (typeof itemValue === "string") {
31-
splitString(itemValue, storageSettings.maxLength).forEach((_, index) => {
32-
this.memCache[`${storageSettings.keyPrefix}${itemKey}${index}`] =
33-
itemValue;
34-
});
34+
splitString(itemValue, storageSettings.maxLength).forEach(
35+
(splitValue, index) => {
36+
this.memCache[`${storageSettings.keyPrefix}${itemKey}${index}`] =
37+
splitValue;
38+
},
39+
);
3540
return;
3641
}
3742
this.memCache[`${storageSettings.keyPrefix}${String(itemKey)}0`] =

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ exports `storageSettings` which can be used to configure the storage methods.
4242
}
4343
```
4444

45-
#### Session storage types.
45+
#### Session storage types
4646

4747
`MemoryStorage` - This holds the data in a simple memory store
4848

0 commit comments

Comments
 (0)