From 9ad5f04e514c13768b594231ca1327b58945f0c3 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 24 Feb 2021 10:31:03 +0300 Subject: [PATCH] only request counters once for headline mutations --- js/Headlines.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/js/Headlines.js b/js/Headlines.js index 1ba52a6b8..559da8b95 100755 --- a/js/Headlines.js +++ b/js/Headlines.js @@ -149,38 +149,51 @@ const Headlines = { const promises = []; if (ops.tmark.length != 0) - promises.push(xhr.json("backend.php", + promises.push(xhr.post("backend.php", {op: "rpc", method: "markSelected", ids: ops.tmark.toString(), cmode: 2})); if (ops.tpub.length != 0) - promises.push(xhr.json("backend.php", + promises.push(xhr.post("backend.php", {op: "rpc", method: "publishSelected", ids: ops.tpub.toString(), cmode: 2})); if (ops.read.length != 0) - promises.push(xhr.json("backend.php", + promises.push(xhr.post("backend.php", {op: "rpc", method: "catchupSelected", ids: ops.read.toString(), cmode: 0})); if (ops.unread.length != 0) - promises.push(xhr.json("backend.php", + promises.push(xhr.post("backend.php", {op: "rpc", method: "catchupSelected", ids: ops.unread.toString(), cmode: 1})); const scores = Object.keys(ops.rescore); if (scores.length != 0) { scores.forEach((score) => { - promises.push(xhr.json("backend.php", + promises.push(xhr.post("backend.php", {op: "article", method: "setScore", id: ops.rescore[score].toString(), score: score})); }); } - /*if (promises.length > 0) - Promise.all([promises]).then(() => { - window.clearTimeout(this._observer_counters_timeout); + Promise.all(promises).then((results) => { + let feeds = []; - this._observer_counters_timeout = setTimeout(() => { - Feeds.requestCounters(); - }, 1000); - });*/ + results.forEach((res) => { + if (res) { + try { + const obj = JSON.parse(res); + + if (obj.feeds) + feeds = feeds.concat(obj.feeds); + } catch (e) { + console.warn(e, res); + } + } + }); + + if (feeds.length > 0) + console.log('requesting counters for', feeds); + Feeds.requestCounters(feeds); + } + ); }, click: function (event, id, in_body) {