Update storage.js

This commit is contained in:
Harvey committed 2021-05-06 13:03:44 +08:00
1 parent 97dbdf16a5
commit 324672a243
1 file changed
+21 -4
+21 -4
View File
@@ -1,27 +1,44 @@
export default class Storage { export default class Storage {
constructor() { constructor() {
this.name = 'artplayer_settings'; this.name = 'artplayer_settings';
this.settings = {};
} }
get(key) { get(key) {
const storage = JSON.parse(localStorage.getItem(this.name)) || {}; try {
const storage = JSON.parse(window.localStorage.getItem(this.name)) || {};
return key ? storage[key] : storage; return key ? storage[key] : storage;
} catch (error) {
return key ? this.settings[key] : this.settings;
}
} }
set(key, value) { set(key, value) {
try {
const storage = Object.assign({}, this.get(), { const storage = Object.assign({}, this.get(), {
[key]: value, [key]: value,
}); });
localStorage.setItem(this.name, JSON.stringify(storage)); window.localStorage.setItem(this.name, JSON.stringify(storage));
} catch (error) {
this.settings[key] = value;
}
} }
del(key) { del(key) {
try {
const storage = this.get(); const storage = this.get();
delete storage[key]; delete storage[key];
localStorage.setItem(this.name, JSON.stringify(storage)); window.localStorage.setItem(this.name, JSON.stringify(storage));
} catch (error) {
delete this.settings[key];
}
} }
clean() { clean() {
localStorage.removeItem(this.name); try {
window.localStorage.removeItem(this.name);
} catch (error) {
this.settings = {};
}
} }
} }