Skip to content

Commit 673054a

Browse files
committed
Update fallback for web-runner & webapp
1 parent 8208743 commit 673054a

File tree

3 files changed

+70
-119
lines changed

3 files changed

+70
-119
lines changed

packages/web-runner/src/fallback.ts

+27-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Copyright 2019-2022 @subwallet/web-runner authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
// eslint-disable-next-line header/header
54
import { SWStorage } from '@subwallet/extension-base/storage';
65

6+
const storage = SWStorage.instance;
7+
8+
// eslint-disable-next-line header/header
79
if (!global.chrome) {
810
// @ts-ignore
911
global.chrome = {};
@@ -13,15 +15,14 @@ if (!global.chrome) {
1315
if (!global.chrome.extension) {
1416
// @ts-ignore
1517
global.chrome.extension = {
16-
getURL: (input: string) => input
18+
getURL: (input: string) => `${input}`
1719
};
1820
}
1921

20-
const storage = SWStorage.instance;
21-
2222
// @ts-ignore
2323
global.chrome.runtime = {
24-
lastError: undefined
24+
lastError: undefined,
25+
getURL: (input: string) => `/${input}`
2526
};
2627

2728
global.chrome.windows = {
@@ -35,8 +36,7 @@ global.chrome.tabs = {
3536
// @ts-ignore
3637
query: () => {
3738
// void
38-
},
39-
getURL: (input: string) => (input)
39+
}
4040
};
4141

4242
global.chrome.storage = {
@@ -55,42 +55,40 @@ global.chrome.storage = {
5555
keys = [keys];
5656
}
5757

58-
const rs = await storage.getMap(keys);
58+
const rs: Record<string, any> = {};
5959

60-
return Object.entries(rs).reduce((result, [key, value]) => {
61-
result[key] = value;
60+
await Promise.all(keys.map(async (k) => {
61+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
62+
const itemData = await storage.getItem(k);
6263

63-
if (typeof value === 'string') {
64+
if (itemData === null) {
65+
rs[k] = undefined;
66+
} else {
6467
try {
65-
result[key] = JSON.parse(value);
68+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment
69+
rs[k] = JSON.parse(itemData);
6670
} catch (e) {
67-
// void
71+
rs[k] = itemData;
6872
}
6973
}
74+
}));
7075

71-
return result;
72-
}, {} as Record<string, unknown>);
73-
})().catch(console.error);
76+
return rs;
77+
})().then((callback)).catch(console.error);
7478
},
7579
// @ts-ignore
7680
set: (input: object, callback?: () => void) => {
77-
(async () => {
78-
const map = Object.entries(input).reduce((map, [key, value]) => {
79-
map[key] = JSON.stringify(value);
80-
81-
return map;
82-
}, {} as Record<string, string>);
83-
84-
await storage.setMap(map);
81+
Promise.all(Object.entries(input).map(async ([k, v]) => {
82+
await storage.setItem(k, JSON.stringify(v));
83+
})).then(() => {
8584
callback && callback();
86-
})().catch(console.error);
85+
}).catch(console.error);
8786
},
8887
// @ts-ignore
89-
remove: (key: string, callback?: () => void) => {
90-
(async () => {
91-
await storage.removeItem(key);
88+
remove: (key: string, value: any, callback?: () => void) => {
89+
storage.removeItem(key).then(() => {
9290
callback && callback();
93-
})().catch(console.error);
91+
}).catch(console.error);
9492
}
9593
}
9694
};

packages/web-runner/src/messageHandle.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export function responseMessage (response: TransportResponseMessage<keyof Reques
2424
}
2525
}
2626

27-
export function setupHandlers () {
28-
const handlers = SWHandler.instance.handle;
27+
const swHandler = SWHandler.instance;
2928

29+
export function setupHandlers () {
3030
window.addEventListener('message', (ev) => {
3131
const data = ev.data as TransportRequestMessage<keyof RequestSignatures>;
3232
const port = {
@@ -50,7 +50,7 @@ export function setupHandlers () {
5050
}
5151

5252
// @ts-ignore
53-
handlers(data, port);
53+
swHandler.handle(data, port);
5454
}
5555
});
5656
}

packages/webapp/src/fallback.ts

+40-87
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright 2019-2022 @subwallet/webapp authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { SWStorage } from '@subwallet/extension-base/storage';
5+
6+
const storage = SWStorage.instance;
7+
48
// eslint-disable-next-line header/header
59
if (!global.chrome) {
610
// @ts-ignore
@@ -11,79 +15,14 @@ if (!global.chrome) {
1115
if (!global.chrome.extension) {
1216
// @ts-ignore
1317
global.chrome.extension = {
14-
getURL: (input: string) => input
18+
getURL: (input: string) => `${input}`
1519
};
1620
}
1721

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-
8422
// @ts-ignore
8523
global.chrome.runtime = {
86-
lastError: undefined
24+
lastError: undefined,
25+
getURL: (input: string) => `/${input}`
8726
};
8827

8928
global.chrome.windows = {
@@ -107,35 +46,49 @@ global.chrome.storage = {
10746
keys: string[] | string | undefined | null,
10847
callback: (val: object) => void
10948
) => {
110-
if (!keys) {
111-
keys = getLocalStorageKeys();
112-
}
49+
(async () => {
50+
if (!keys) {
51+
keys = await storage.keys();
52+
}
11353

114-
if (typeof keys === 'string') {
115-
keys = [keys];
116-
}
54+
if (typeof keys === 'string') {
55+
keys = [keys];
56+
}
11757

118-
const rs: Record<string, any> = {};
58+
const rs: Record<string, any> = {};
11959

120-
keys.forEach((k) => {
60+
await Promise.all(keys.map(async (k) => {
12161
// 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);
12678
},
12779
// @ts-ignore
12880
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);
13486
},
13587
// @ts-ignore
13688
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);
13992
}
14093
}
14194
};

0 commit comments

Comments
 (0)