@@ -2,11 +2,11 @@ import { storageSettings } from "../index.js";
2
2
import { StorageKeys , type SessionManager } from "../types.js" ;
3
3
import { splitString } from "../utils.js" ;
4
4
5
- function getStorageValue ( key : string ) {
5
+ function getStorageValue ( key : string ) : unknown | undefined {
6
6
return new Promise ( ( resolve , reject ) => {
7
7
chrome . storage . local . get ( [ key ] , function ( result ) {
8
8
if ( chrome . runtime . lastError ) {
9
- reject ( chrome . runtime . lastError ) ;
9
+ reject ( undefined ) ;
10
10
} else {
11
11
resolve ( result [ key ] ) ;
12
12
}
@@ -37,11 +37,14 @@ export class ChromeStore<V = StorageKeys> implements SessionManager<V> {
37
37
itemKey : V | StorageKeys ,
38
38
itemValue : unknown ,
39
39
) : Promise < void > {
40
+ // clear items first
41
+ await this . removeSessionItem ( itemKey ) ;
42
+
40
43
if ( typeof itemValue === "string" ) {
41
44
splitString ( itemValue , storageSettings . maxLength ) . forEach (
42
- async ( _ , index ) => {
45
+ async ( splitValue , index ) => {
43
46
await chrome . storage . local . set ( {
44
- [ `${ storageSettings . keyPrefix } ${ itemKey } ${ index } ` ] : itemValue ,
47
+ [ `${ storageSettings . keyPrefix } ${ itemKey } ${ index } ` ] : splitValue ,
45
48
} ) ;
46
49
} ,
47
50
) ;
@@ -83,11 +86,11 @@ export class ChromeStore<V = StorageKeys> implements SessionManager<V> {
83
86
// remove items from the chrome.storage
84
87
let index = 0 ;
85
88
while (
86
- chrome . storage . local . get (
89
+ ( await getStorageValue (
87
90
`${ storageSettings . keyPrefix } ${ String ( itemKey ) } ${ index } ` ,
88
- ) !== undefined
91
+ ) ) !== undefined
89
92
) {
90
- chrome . storage . local . remove (
93
+ await chrome . storage . local . remove (
91
94
`${ storageSettings . keyPrefix } ${ String ( itemKey ) } ${ index } ` ,
92
95
) ;
93
96
index ++ ;
0 commit comments