code cosmetics

This commit is contained in:
tophf 2017-12-12 17:35:38 +03:00
parent 3ffbce0bc7
commit 56c1476e67

View File

@ -625,7 +625,8 @@ window.addEventListener('showStyles:done', function _() {
* @returns {Promise<Object>} An object containing info about the style, e.g. name, author, etc. * @returns {Promise<Object>} An object containing info about the style, e.g. name, author, etc.
*/ */
function fetchStyle(userstylesId) { function fetchStyle(userstylesId) {
return readCache(userstylesId).then(json => json || return readCache(userstylesId).then(json =>
json ||
download(BASE_URL + '/api/v1/styles/' + userstylesId, { download(BASE_URL + '/api/v1/styles/' + userstylesId, {
method: 'GET', method: 'GET',
headers: { headers: {
@ -634,13 +635,7 @@ window.addEventListener('showStyles:done', function _() {
}, },
responseType: 'json', responseType: 'json',
body: null body: null
}).then(json => { }).then(writeCache));
for (const prop of CACHE_EXCEPT_PROPS) {
delete json[prop];
}
writeCache(json);
return json;
}));
} }
/** /**
@ -667,7 +662,8 @@ window.addEventListener('showStyles:done', function _() {
const cacheKey = category + '/' + searchCurrentPage; const cacheKey = category + '/' + searchCurrentPage;
return readCache(cacheKey) return readCache(cacheKey)
.then(json => json || .then(json =>
json ||
download(searchURL, { download(searchURL, {
method: 'GET', method: 'GET',
headers: { headers: {
@ -676,11 +672,7 @@ window.addEventListener('showStyles:done', function _() {
}, },
responseType: 'json', responseType: 'json',
body: null body: null
}).then(json => { }).then(writeCache))
json.id = cacheKey;
writeCache(json);
return json;
}))
.then(json => { .then(json => {
searchCurrentPage = json.current_page + 1; searchCurrentPage = json.current_page + 1;
searchTotalPages = json.total_pages; searchTotalPages = json.total_pages;
@ -707,8 +699,13 @@ window.addEventListener('showStyles:done', function _() {
} }
function writeCache(data, debounced) { function writeCache(data, debounced) {
data.id = data.id || category + '/' + data.current_page;
for (const prop of CACHE_EXCEPT_PROPS) {
delete data[prop];
}
if (!debounced) { if (!debounced) {
debounce(writeCache, 100, data, true); // using plain setTimeout because debounce() replaces previous parameters
setTimeout(writeCache, 100, data, true);
return data; return data;
} else { } else {
debounce(cleanupCache, CACHE_CLEANUP_THROTTLE); debounce(cleanupCache, CACHE_CLEANUP_THROTTLE);