client/settings: Cache calls to settings.get()

This commit is contained in:
Shyam Sunder 2019-05-23 20:27:59 -04:00
parent 7b236b02c9
commit e0fc790822
2 changed files with 22 additions and 14 deletions

View file

@ -20,19 +20,12 @@ const defaultSettings = {
}; };
class Settings extends events.EventTarget { class Settings extends events.EventTarget {
save(newSettings, silent) { constructor() {
newSettings = Object.assign(this.get(), newSettings); super();
localStorage.setItem('settings', JSON.stringify(newSettings)); this.cache = this._getFromLocalStorage();
if (silent !== true) {
this.dispatchEvent(new CustomEvent('change', {
detail: {
settings: this.get(),
},
}));
}
} }
get() { _getFromLocalStorage() {
let ret = Object.assign({}, defaultSettings); let ret = Object.assign({}, defaultSettings);
try { try {
Object.assign(ret, JSON.parse(localStorage.getItem('settings'))); Object.assign(ret, JSON.parse(localStorage.getItem('settings')));
@ -40,6 +33,23 @@ class Settings extends events.EventTarget {
} }
return ret; return ret;
} }
save(newSettings, silent) {
newSettings = Object.assign(this.cache, newSettings);
localStorage.setItem('settings', JSON.stringify(newSettings));
this.cache = this._getFromLocalStorage();
if (silent !== true) {
this.dispatchEvent(new CustomEvent('change', {
detail: {
settings: this.cache,
},
}));
}
}
get() {
return this.cache;
}
}; };
module.exports = new Settings(); module.exports = new Settings();

View file

@ -4,8 +4,6 @@ const markdown = require('./markdown.js');
const uri = require('./uri.js'); const uri = require('./uri.js');
const settings = require('../models/settings.js'); const settings = require('../models/settings.js');
const tagUnderscoresAsSpaces = settings.get().tagUnderscoresAsSpaces;
function decamelize(str, sep) { function decamelize(str, sep) {
sep = sep === undefined ? '-' : sep; sep = sep === undefined ? '-' : sep;
return str return str
@ -201,7 +199,7 @@ function dataURItoBlob(dataURI) {
} }
function getPrettyTagName(tag) { function getPrettyTagName(tag) {
if (tagUnderscoresAsSpaces) { if (settings.get().tagUnderscoresAsSpaces) {
return tag.replace(/_/g, " "); return tag.replace(/_/g, " ");
} }
return tag; return tag;