Skip to content

Commit

Permalink
fix: sessionStore issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRivers committed Sep 3, 2024
1 parent 104f3f8 commit 34a7298
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
17 changes: 10 additions & 7 deletions lib/sessionManager/stores/chromeStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { storageSettings } from "../index.js";
import { StorageKeys, type SessionManager } from "../types.js";
import { splitString } from "../utils.js";

function getStorageValue(key: string) {
function getStorageValue(key: string): unknown | undefined {
return new Promise((resolve, reject) => {
chrome.storage.local.get([key], function (result) {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
reject(undefined);
} else {
resolve(result[key]);
}
Expand Down Expand Up @@ -37,11 +37,14 @@ export class ChromeStore<V = StorageKeys> implements SessionManager<V> {
itemKey: V | StorageKeys,
itemValue: unknown,
): Promise<void> {

Check warning on line 39 in lib/sessionManager/stores/chromeStore.ts

View check run for this annotation

Codecov / codecov/patch

lib/sessionManager/stores/chromeStore.ts#L37-L39

Added lines #L37 - L39 were not covered by tests
// clear items first
await this.removeSessionItem(itemKey);

Check warning on line 41 in lib/sessionManager/stores/chromeStore.ts

View check run for this annotation

Codecov / codecov/patch

lib/sessionManager/stores/chromeStore.ts#L41

Added line #L41 was not covered by tests

if (typeof itemValue === "string") {
splitString(itemValue, storageSettings.maxLength).forEach(
async (_, index) => {
async (splitValue, index) => {
await chrome.storage.local.set({
[`${storageSettings.keyPrefix}${itemKey}${index}`]: itemValue,
[`${storageSettings.keyPrefix}${itemKey}${index}`]: splitValue,
});
},
);
Expand Down Expand Up @@ -83,11 +86,11 @@ export class ChromeStore<V = StorageKeys> implements SessionManager<V> {
// remove items from the chrome.storage
let index = 0;
while (
chrome.storage.local.get(
(await getStorageValue(
`${storageSettings.keyPrefix}${String(itemKey)}${index}`,
) !== undefined
)) !== undefined
) {
chrome.storage.local.remove(
await chrome.storage.local.remove(
`${storageSettings.keyPrefix}${String(itemKey)}${index}`,
);
index++;
Expand Down
13 changes: 9 additions & 4 deletions lib/sessionManager/stores/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ export class MemoryStorage<V = StorageKeys> implements SessionManager<V> {
itemKey: V | StorageKeys,
itemValue: unknown,
): Promise<void> {
// clear items first
await this.removeSessionItem(itemKey);

if (typeof itemValue === "string") {
splitString(itemValue, storageSettings.maxLength).forEach((_, index) => {
this.memCache[`${storageSettings.keyPrefix}${itemKey}${index}`] =
itemValue;
});
splitString(itemValue, storageSettings.maxLength).forEach(
(splitValue, index) => {
this.memCache[`${storageSettings.keyPrefix}${itemKey}${index}`] =
splitValue;
},
);
return;
}
this.memCache[`${storageSettings.keyPrefix}${String(itemKey)}0`] =
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports `storageSettings` which can be used to configure the storage methods.
}
```

#### Session storage types.
#### Session storage types

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

Expand Down

0 comments on commit 34a7298

Please sign in to comment.