From 34a72988a4263f17c3dfa677ad8e9fde5bc3889f Mon Sep 17 00:00:00 2001 From: Daniel Rivers Date: Tue, 3 Sep 2024 11:00:07 +0100 Subject: [PATCH] fix: sessionStore issues --- lib/sessionManager/stores/chromeStore.ts | 17 ++++++++++------- lib/sessionManager/stores/memory.ts | 13 +++++++++---- readme.md | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/sessionManager/stores/chromeStore.ts b/lib/sessionManager/stores/chromeStore.ts index 7b08a4d..f09b40e 100644 --- a/lib/sessionManager/stores/chromeStore.ts +++ b/lib/sessionManager/stores/chromeStore.ts @@ -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]); } @@ -37,11 +37,14 @@ export class ChromeStore implements SessionManager { itemKey: V | StorageKeys, itemValue: unknown, ): Promise { + // clear items first + await this.removeSessionItem(itemKey); + 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, }); }, ); @@ -83,11 +86,11 @@ export class ChromeStore implements SessionManager { // 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++; diff --git a/lib/sessionManager/stores/memory.ts b/lib/sessionManager/stores/memory.ts index db02433..eb78377 100644 --- a/lib/sessionManager/stores/memory.ts +++ b/lib/sessionManager/stores/memory.ts @@ -27,11 +27,16 @@ export class MemoryStorage implements SessionManager { itemKey: V | StorageKeys, itemValue: unknown, ): Promise { + // 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`] = diff --git a/readme.md b/readme.md index 0573a54..5b90ebd 100644 --- a/readme.md +++ b/readme.md @@ -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