1
1
// Copyright 2019-2022 @subwallet/webapp authors & contributors
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
+ import { SWStorage } from '@subwallet/extension-base/storage' ;
5
+
6
+ const storage = SWStorage . instance ;
7
+
4
8
// eslint-disable-next-line header/header
5
9
if ( ! global . chrome ) {
6
10
// @ts -ignore
@@ -11,79 +15,14 @@ if (!global.chrome) {
11
15
if ( ! global . chrome . extension ) {
12
16
// @ts -ignore
13
17
global . chrome . extension = {
14
- getURL : ( input : string ) => input
18
+ getURL : ( input : string ) => ` ${ input } `
15
19
} ;
16
20
}
17
21
18
- function migrateStorageKeys ( ) {
19
- if ( ! localStorage . getItem ( '__is_storage_keys_migrated__' ) ) {
20
- const currentKeys = getLocalStorageKeys ( ) ;
21
-
22
- const newKeys : string [ ] = [ ] ;
23
-
24
- currentKeys . forEach ( ( k ) => {
25
- if ( ! newKeys . includes ( k ) ) {
26
- newKeys . push ( k ) ;
27
- }
28
- } ) ;
29
-
30
- setLocalStorageItem ( '__storage_keys__' , newKeys ) ;
31
-
32
- localStorage . setItem ( '__is_storage_keys_migrated__' , '1' ) ;
33
- }
34
- }
35
-
36
- migrateStorageKeys ( ) ;
37
-
38
- export function getLocalStorageKeys ( ) : string [ ] {
39
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
40
- return getLocalStorageItem ( '__storage_keys__' , [ ] ) ;
41
- }
42
-
43
- export function setLocalStorageKeys ( key : string ) {
44
- const currentKeys = getLocalStorageKeys ( ) ;
45
-
46
- if ( currentKeys . includes ( key ) ) {
47
- return ;
48
- }
49
-
50
- currentKeys . push ( key ) ;
51
- setLocalStorageItem ( '__storage_keys__' , currentKeys ) ;
52
- }
53
-
54
- function setLocalStorageItem ( key : string , value : any ) {
55
- // eslint-disable-next-line eqeqeq
56
- if ( key != '__storage_keys__' ) {
57
- setLocalStorageKeys ( key ) ;
58
- }
59
-
60
- localStorage . setItem ( key , JSON . stringify ( value ) ) ;
61
- }
62
-
63
- function removeLocalStorageItem ( key : string ) {
64
- localStorage . removeItem ( key ) ;
65
- }
66
-
67
- function getLocalStorageItem ( key : string , defaultVal : any = undefined ) {
68
- const value : string | null = localStorage . getItem ( key ) ;
69
-
70
- if ( value ) {
71
- try {
72
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
73
- return JSON . parse ( value ) ;
74
- } catch ( e ) {
75
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
76
- return defaultVal ;
77
- }
78
- } else {
79
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
80
- return defaultVal ;
81
- }
82
- }
83
-
84
22
// @ts -ignore
85
23
global . chrome . runtime = {
86
- lastError : undefined
24
+ lastError : undefined ,
25
+ getURL : ( input : string ) => `/${ input } `
87
26
} ;
88
27
89
28
global . chrome . windows = {
@@ -107,35 +46,49 @@ global.chrome.storage = {
107
46
keys : string [ ] | string | undefined | null ,
108
47
callback : ( val : object ) => void
109
48
) => {
110
- if ( ! keys ) {
111
- keys = getLocalStorageKeys ( ) ;
112
- }
49
+ ( async ( ) => {
50
+ if ( ! keys ) {
51
+ keys = await storage . keys ( ) ;
52
+ }
113
53
114
- if ( typeof keys === 'string' ) {
115
- keys = [ keys ] ;
116
- }
54
+ if ( typeof keys === 'string' ) {
55
+ keys = [ keys ] ;
56
+ }
117
57
118
- const rs : Record < string , any > = { } ;
58
+ const rs : Record < string , any > = { } ;
119
59
120
- keys . forEach ( ( k ) => {
60
+ await Promise . all ( keys . map ( async ( k ) => {
121
61
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
122
- rs [ k ] = getLocalStorageItem ( k ) ;
123
- } ) ;
124
-
125
- callback ( rs ) ;
62
+ const itemData = await storage . getItem ( k ) ;
63
+
64
+ if ( itemData === null ) {
65
+ rs [ k ] = undefined ;
66
+ } else {
67
+ try {
68
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment
69
+ rs [ k ] = JSON . parse ( itemData ) ;
70
+ } catch ( e ) {
71
+ rs [ k ] = itemData ;
72
+ }
73
+ }
74
+ } ) ) ;
75
+
76
+ return rs ;
77
+ } ) ( ) . then ( ( callback ) ) . catch ( console . error ) ;
126
78
} ,
127
79
// @ts -ignore
128
80
set : ( input : object , callback ?: ( ) => void ) => {
129
- Object . entries ( input ) . forEach ( ( [ k , v ] ) => {
130
- setLocalStorageItem ( k , v ) ;
131
- } ) ;
132
-
133
- callback && callback ( ) ;
81
+ Promise . all ( Object . entries ( input ) . map ( async ( [ k , v ] ) => {
82
+ await storage . setItem ( k , JSON . stringify ( v ) ) ;
83
+ } ) ) . then ( ( ) => {
84
+ callback && callback ( ) ;
85
+ } ) . catch ( console . error ) ;
134
86
} ,
135
87
// @ts -ignore
136
88
remove : ( key : string , value : any , callback ?: ( ) => void ) => {
137
- removeLocalStorageItem ( key ) ;
138
- callback && callback ( ) ;
89
+ storage . removeItem ( key ) . then ( ( ) => {
90
+ callback && callback ( ) ;
91
+ } ) . catch ( console . error ) ;
139
92
}
140
93
}
141
94
} ;
0 commit comments