Adrià Vilanova MartÃnez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 1 | export default class ExtSessionStorage { |
| 2 | static set(items) { |
| 3 | return new Promise((res, rej) => { |
| 4 | if (window.extCustomStorage === undefined) window.extCustomStorage = {}; |
| 5 | |
| 6 | for (const [key, value] of Object.entries(items)) |
| 7 | window.extCustomStorage[key] = value; |
| 8 | |
| 9 | res(); |
| 10 | }); |
| 11 | } |
| 12 | |
| 13 | static get(keys) { |
| 14 | return new Promise((res, rej) => { |
| 15 | if (window.extCustomStorage === undefined) window.extCustomStorage = {}; |
| 16 | |
| 17 | if (keys === undefined) { |
| 18 | res(window.extCustomStorage); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | if (typeof keys === 'string') { |
| 23 | const key = keys; |
| 24 | keys = [key]; |
| 25 | } |
| 26 | |
| 27 | if (Array.isArray(keys)) { |
| 28 | let returnObject = {}; |
| 29 | for (const key of keys) { |
| 30 | returnObject[key] = window.extCustomStorage[key]; |
| 31 | } |
| 32 | res(returnObject); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | rej(new Error( |
| 37 | 'The keys passed are not a valid type ' + |
| 38 | '(undefined, string or array).')); |
| 39 | }); |
| 40 | } |
| 41 | } |