scroll handler: performance improvements

This commit is contained in:
Andrew Dolgov 2018-11-30 14:20:00 +03:00
parent b9585004e6
commit 0c06bb5fe1
2 changed files with 35 additions and 46 deletions

View File

@ -176,6 +176,7 @@ function feedlist_init() {
document.onkeydown = hotkey_handler; document.onkeydown = hotkey_handler;
setInterval(hotkey_prefix_timeout, 5*1000); setInterval(hotkey_prefix_timeout, 5*1000);
setInterval(catchupBatchedArticles, 3*1000);
if (!getActiveFeedId()) { if (!getActiveFeedId()) {
viewfeed({feed: -3}); viewfeed({feed: -3});

View File

@ -1007,22 +1007,24 @@ function postMouseOut(id) {
function unpackVisibleHeadlines() { function unpackVisibleHeadlines() {
if (!isCdmMode()) return; if (!isCdmMode()) return;
$$("#headlines-frame div[id*=RROW][data-content]").each((row) => { const rows = $$("#headlines-frame div[id*=RROW][data-content]");
//console.log('checking', row.id);
if (row.offsetTop <= $("headlines-frame").scrollTop + $("headlines-frame").offsetHeight) { for (let i = 0; i < rows.length; i++) {
const row = rows[i];
if (row.offsetTop <= $("headlines-frame").scrollTop + $("headlines-frame").offsetHeight) {
console.log("unpacking: " + row.id); console.log("unpacking: " + row.id);
const content = row.getAttribute("data-content"); const content = row.getAttribute("data-content");
row.select(".cdmContentInner")[0].innerHTML = content; row.select(".cdmContentInner")[0].innerHTML = content;
row.removeAttribute("data-content"); row.removeAttribute("data-content");
PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, row); PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, row);
} else { } else {
throw $break; break;
} }
}); }
} }
function headlines_scroll_handler(e) { function headlines_scroll_handler(e) {
@ -1035,8 +1037,6 @@ function headlines_scroll_handler(e) {
_headlines_scroll_offset = e.scrollTop; _headlines_scroll_offset = e.scrollTop;
const hsp = $("headlines-spacer");
unpackVisibleHeadlines(); unpackVisibleHeadlines();
// set topmost child in the buffer as active // set topmost child in the buffer as active
@ -1046,18 +1046,18 @@ function headlines_scroll_handler(e) {
const rows = $$("#headlines-frame > div[id*=RROW]"); const rows = $$("#headlines-frame > div[id*=RROW]");
for (let i = 0; i < rows.length; i++) { for (let i = 0; i < rows.length; i++) {
const child = rows[i]; const row = rows[i];
if ($("headlines-frame").scrollTop <= child.offsetTop && if ($("headlines-frame").scrollTop <= row.offsetTop &&
child.offsetTop - $("headlines-frame").scrollTop < 100 && row.offsetTop - $("headlines-frame").scrollTop < 100 &&
child.getAttribute("data-article-id") != _active_article_id) { row.getAttribute("data-article-id") != _active_article_id) {
if (_active_article_id) { if (_active_article_id) {
const row = $("RROW-" + _active_article_id); const row = $("RROW-" + _active_article_id);
if (row) row.removeClassName("active"); if (row) row.removeClassName("active");
} }
_active_article_id = child.getAttribute("data-article-id"); _active_article_id = row.getAttribute("data-article-id");
showArticleInHeadlines(_active_article_id, true); showArticleInHeadlines(_active_article_id, true);
updateSelectedPrompt(); updateSelectedPrompt();
break; break;
@ -1066,6 +1066,8 @@ function headlines_scroll_handler(e) {
} }
if (!_infscroll_disable) { if (!_infscroll_disable) {
const hsp = $("headlines-spacer");
if (hsp && hsp.offsetTop - 250 <= e.scrollTop + e.offsetHeight) { if (hsp && hsp.offsetTop - 250 <= e.scrollTop + e.offsetHeight) {
hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " + hsp.innerHTML = "<span class='loading'><img src='images/indicator_tiny.gif'> " +
@ -1081,33 +1083,31 @@ function headlines_scroll_handler(e) {
updateFloatingTitle(); updateFloatingTitle();
} }
catchupCurrentBatchIfNeeded();
if (getInitParam("cdm_auto_catchup") == 1) { if (getInitParam("cdm_auto_catchup") == 1) {
// let's get DOM some time to settle down let rows = $$("#headlines-frame > div[id*=RROW][class*=Unread]");
const ts = new Date().getTime();
if (ts - _last_headlines_update < 100) return;
$$("#headlines-frame > div[id*=RROW][class*=Unread]").each( for (let i = 0; i < rows.length; i++) {
function(child) { const row = rows[i];
if ($("headlines-frame").scrollTop > (child.offsetTop + child.offsetHeight/2)) {
const id = child.getAttribute("data-article-id") if ($("headlines-frame").scrollTop > (row.offsetTop + row.offsetHeight/2)) {
if (catchup_id_batch.indexOf(id) == -1) const id = row.getAttribute("data-article-id")
catchup_id_batch.push(id);
//console.log("auto_catchup_batch: " + catchup_id_batch.toString()); if (catchup_id_batch.indexOf(id) == -1)
} catchup_id_batch.push(id);
}); //console.log("auto_catchup_batch: " + catchup_id_batch.toString());
} else {
break;
}
}
if (_infscroll_disable) { if (_infscroll_disable) {
const child = $$("#headlines-frame div[id*=RROW]").last(); const row = $$("#headlines-frame div[id*=RROW]").last();
if (child && $("headlines-frame").scrollTop > if (row && $("headlines-frame").scrollTop >
(child.offsetTop + child.offsetHeight - 50)) { (row.offsetTop + row.offsetHeight - 50)) {
console.log("we seem to be at an end"); console.log("we seem to be at an end");
@ -1132,7 +1132,7 @@ function openNextUnreadFeed() {
function catchupBatchedArticles() { function catchupBatchedArticles() {
if (catchup_id_batch.length > 0 && !_infscroll_request_sent && !_catchup_request_sent) { if (catchup_id_batch.length > 0 && !_infscroll_request_sent && !_catchup_request_sent) {
console.log("catchupBatchedArticles: working"); console.log("catchupBatchedArticles, size=", catchup_id_batch.length);
// make a copy of the array // make a copy of the array
const batch = catchup_id_batch.slice(); const batch = catchup_id_batch.slice();
@ -1150,7 +1150,6 @@ function catchupBatchedArticles() {
const batch = reply.ids; const batch = reply.ids;
batch.each(function (id) { batch.each(function (id) {
console.log(id);
const elem = $("RROW-" + id); const elem = $("RROW-" + id);
if (elem) elem.removeClassName("Unread"); if (elem) elem.removeClassName("Unread");
catchup_id_batch.remove(id); catchup_id_batch.remove(id);
@ -1801,14 +1800,3 @@ function updateFloatingTitle(unread_only) {
} }
} }
} }
function catchupCurrentBatchIfNeeded() {
if (catchup_id_batch.length > 0) {
window.clearTimeout(catchup_timeout_id);
catchup_timeout_id = window.setTimeout(catchupBatchedArticles, 1000);
if (catchup_id_batch.length >= 10) {
catchupBatchedArticles();
}
}
}