UNPKG

2.8 kBJavaScriptView Raw
1/**
2 * @return {LocalStorage}
3 * @param {{type: string}} config Set to "session" to use sessionStorage
4 * @constructor
5 */
6export default class LocalStorage {
7 static async safePromise(resolver) {
8 try {
9 return await new Promise(resolver);
10 } catch (e) {
11 if (e && e.name === 'NS_ERROR_FILE_CORRUPTED') {
12 /* eslint-disable no-alert */
13 window.alert('Sorry, it looks like your browser storage is corrupted. ' +
14 'Please clear your storage by going to Tools -> Clear Recent History -> Cookies' +
15 ' and setting time range to "Everything". This will remove the corrupted browser storage across all sites.');
16 /* eslint-enable no-alert */
17 }
18 throw e;
19 }
20 }
21
22 constructor(config = {}) {
23 this.storageType = config.type === 'session' ? 'sessionStorage' : 'localStorage';
24 }
25
26 /**
27 * @param {string} name
28 * @return {Promise}
29 */
30 get(name) {
31 return this.constructor.safePromise(resolve => {
32 const value = window[this.storageType].getItem(name);
33 try {
34 resolve(JSON.parse(value));
35 } catch (e) {
36 resolve(value);
37 }
38 });
39 }
40
41 /**
42 * @param {string} name
43 * @param {object} value
44 * @return {Promise}
45 */
46 set(name, value) {
47 return this.constructor.safePromise(resolve => {
48 window[this.storageType].setItem(name, JSON.stringify(value));
49 resolve(value);
50 });
51 }
52
53 /**
54 * @param {string} name
55 * @return {Promise}
56 */
57 remove(name) {
58 const storageType = this.storageType;
59
60 return this.constructor.safePromise(resolve => {
61 if (window[storageType].hasOwnProperty(name)) {
62 window[storageType].removeItem(name);
63 }
64 resolve();
65 });
66 }
67
68 /**
69 * @param callback
70 * @return {Promise}
71 */
72 each(callback) {
73 const storageType = this.storageType;
74
75 return this.constructor.safePromise(resolve => {
76 const promises = [];
77
78 for (const item in window[storageType]) {
79 if (window[storageType].hasOwnProperty(item)) {
80 let value = window[storageType].getItem(item);
81 try {
82 value = JSON.parse(value);
83 } catch (e) {
84 // Do nothing
85 }
86
87 promises.push(Promise.resolve(callback(item, value)));
88 }
89 }
90
91 resolve(Promise.all(promises));
92 });
93 }
94
95 /**
96 * @param {string} name
97 * @param {Function} callback
98 * @return {Function}
99 */
100 on(name, callback) {
101 function handleStorage(e) {
102 if (e.key === name) {
103 try {
104 callback(JSON.parse(e.newValue));
105 } catch (err) {
106 callback(e.newValue);
107 }
108 }
109 }
110
111 window.addEventListener('storage', handleStorage, false);
112
113 return () => window.removeEventListener('storage', handleStorage, false);
114 }
115}