From 56c1476e676109644219e7c7d2ae583c95968a61 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 12 Dec 2017 17:35:38 +0300 Subject: [PATCH] code cosmetics --- popup/search-results.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/popup/search-results.js b/popup/search-results.js index abf619cf..b174365e 100755 --- a/popup/search-results.js +++ b/popup/search-results.js @@ -625,7 +625,8 @@ window.addEventListener('showStyles:done', function _() { * @returns {Promise} An object containing info about the style, e.g. name, author, etc. */ function fetchStyle(userstylesId) { - return readCache(userstylesId).then(json => json || + return readCache(userstylesId).then(json => + json || download(BASE_URL + '/api/v1/styles/' + userstylesId, { method: 'GET', headers: { @@ -634,13 +635,7 @@ window.addEventListener('showStyles:done', function _() { }, responseType: 'json', body: null - }).then(json => { - for (const prop of CACHE_EXCEPT_PROPS) { - delete json[prop]; - } - writeCache(json); - return json; - })); + }).then(writeCache)); } /** @@ -667,7 +662,8 @@ window.addEventListener('showStyles:done', function _() { const cacheKey = category + '/' + searchCurrentPage; return readCache(cacheKey) - .then(json => json || + .then(json => + json || download(searchURL, { method: 'GET', headers: { @@ -676,11 +672,7 @@ window.addEventListener('showStyles:done', function _() { }, responseType: 'json', body: null - }).then(json => { - json.id = cacheKey; - writeCache(json); - return json; - })) + }).then(writeCache)) .then(json => { searchCurrentPage = json.current_page + 1; searchTotalPages = json.total_pages; @@ -707,8 +699,13 @@ window.addEventListener('showStyles:done', function _() { } function writeCache(data, debounced) { + data.id = data.id || category + '/' + data.current_page; + for (const prop of CACHE_EXCEPT_PROPS) { + delete data[prop]; + } if (!debounced) { - debounce(writeCache, 100, data, true); + // using plain setTimeout because debounce() replaces previous parameters + setTimeout(writeCache, 100, data, true); return data; } else { debounce(cleanupCache, CACHE_CLEANUP_THROTTLE);