sync unread state to server via mutation observer

master
Andrew Dolgov 6 years ago
parent e5efde26ac
commit d9bf0f17c6

@ -298,13 +298,7 @@ define(["dojo/_base/declare"], function (declare) {
Article.unpack(row); Article.unpack(row);
if (row.hasClassName("Unread")) { if (row.hasClassName("Unread")) {
Headlines.toggleUnread(id, 0);
Headlines.catchupBatched(() => {
Feeds.decrementFeedCounter(Feeds.getActive(), Feeds.activeIsCat());
Headlines.toggleUnread(id, 0);
//Headlines.updateFloatingTitle(true);
});
} }
row.addClassName("active"); row.addClassName("active");

@ -215,8 +215,6 @@ define(["dojo/_base/declare"], function (declare) {
document.onkeydown = (event) => { return App.hotkeyHandler(event) }; document.onkeydown = (event) => { return App.hotkeyHandler(event) };
window.onresize = () => { Headlines.scrollHandler(); } window.onresize = () => { Headlines.scrollHandler(); }
window.setInterval(() => { Headlines.catchupBatched() }, 10 * 1000);
if (!this.getActive()) { if (!this.getActive()) {
this.open({feed: -3}); this.open({feed: -3});
} else { } else {
@ -364,17 +362,15 @@ define(["dojo/_base/declare"], function (declare) {
window.clearTimeout(this._viewfeed_wait_timeout); window.clearTimeout(this._viewfeed_wait_timeout);
this._viewfeed_wait_timeout = window.setTimeout(() => { this._viewfeed_wait_timeout = window.setTimeout(() => {
Headlines.catchupBatched(() => { xhrPost("backend.php", query, (transport) => {
xhrPost("backend.php", query, (transport) => { try {
try { window.clearTimeout(this._infscroll_timeout);
window.clearTimeout(this._infscroll_timeout); this.setExpando(feed, is_cat, 'images/blank_icon.gif');
this.setExpando(feed, is_cat, 'images/blank_icon.gif'); Headlines.onLoaded(transport, offset);
Headlines.onLoaded(transport, offset); PluginHost.run(PluginHost.HOOK_FEED_LOADED, [feed, is_cat]);
PluginHost.run(PluginHost.HOOK_FEED_LOADED, [feed, is_cat]); } catch (e) {
} catch (e) { App.Error.report(e);
App.Error.report(e); }
}
});
}); });
}, delayed ? 250 : 0); }, delayed ? 250 : 0);
}, },
@ -394,27 +390,6 @@ define(["dojo/_base/declare"], function (declare) {
App.updateTitle(); App.updateTitle();
} }
}, },
decrementFeedCounter: function(feed, is_cat) {
let ctr = this.getUnread(feed, is_cat);
if (ctr > 0) {
this.setUnread(feed, is_cat, ctr - 1);
App.global_unread -= 1;
App.updateTitle();
if (!is_cat) {
const cat = parseInt(this.getCategory(feed));
if (!isNaN(cat)) {
ctr = this.getUnread(cat, true);
if (ctr > 0) {
this.setUnread(cat, true, ctr - 1);
}
}
}
}
},
catchupFeed: function(feed, is_cat, mode) { catchupFeed: function(feed, is_cat, mode) {
if (is_cat == undefined) is_cat = false; if (is_cat == undefined) is_cat = false;
@ -482,15 +457,9 @@ define(["dojo/_base/declare"], function (declare) {
const rows = $$("#headlines-frame > div[id*=RROW][class*=Unread][data-orig-feed-id='" + id + "']"); const rows = $$("#headlines-frame > div[id*=RROW][class*=Unread][data-orig-feed-id='" + id + "']");
if (rows.length > 0) { rows.each((row) => {
row.removeClassName("Unread");
for (let i = 0; i < rows.length; i++) })
Headlines.catchup_id_batch.push(rows[i].getAttribute("data-article-id"));
Headlines.catchupBatched(() => {
Headlines.updateFloatingTitle(true);
});
}
} }
}, },
getUnread: function(feed, is_cat) { getUnread: function(feed, is_cat) {

@ -6,7 +6,6 @@ define(["dojo/_base/declare"], function (declare) {
_headlines_scroll_timeout: 0, _headlines_scroll_timeout: 0,
headlines: [], headlines: [],
current_first_id: 0, current_first_id: 0,
catchup_id_batch: [],
row_observer: new MutationObserver((mutations) => { row_observer: new MutationObserver((mutations) => {
const modified = []; const modified = [];
@ -45,6 +44,8 @@ define(["dojo/_base/declare"], function (declare) {
const ops = { const ops = {
tmark: [], tmark: [],
tpub: [], tpub: [],
read: [],
unread: [],
}; };
modified.each(function(m) { modified.each(function(m) {
@ -53,6 +54,9 @@ define(["dojo/_base/declare"], function (declare) {
if (m.old.published != m.new.published) if (m.old.published != m.new.published)
ops.tpub.push(m.id); ops.tpub.push(m.id);
if (m.old.unread != m.new.unread)
m.new.unread ? ops.unread.push(m.id) : ops.read.push(m.id);
}); });
if (ops.tmark.length != 0) if (ops.tmark.length != 0)
@ -67,7 +71,19 @@ define(["dojo/_base/declare"], function (declare) {
App.handleRpcJson(transport); App.handleRpcJson(transport);
}); });
}, if (ops.read.length != 0)
xhrPost("backend.php",
{ op: "rpc", method: "catchupSelected", ids: ops.read.toString(), cmode: 0}, (transport) => {
App.handleRpcJson(transport);
});
if (ops.unread.length != 0)
xhrPost("backend.php",
{ op: "rpc", method: "catchupSelected", ids: ops.unread.toString(), cmode: 1}, (transport) => {
App.handleRpcJson(transport);
});
},
click: function (event, id, in_body) { click: function (event, id, in_body) {
in_body = in_body || false; in_body = in_body || false;
@ -188,11 +204,7 @@ define(["dojo/_base/declare"], function (declare) {
const row = rows[i]; const row = rows[i];
if ($("headlines-frame").scrollTop > (row.offsetTop + row.offsetHeight / 2)) { if ($("headlines-frame").scrollTop > (row.offsetTop + row.offsetHeight / 2)) {
const id = row.getAttribute("data-article-id") row.removeClassName("Unread");
if (this.catchup_id_batch.indexOf(id) == -1)
this.catchup_id_batch.push(id);
} else { } else {
break; break;
} }
@ -625,8 +637,7 @@ define(["dojo/_base/declare"], function (declare) {
selectionToggleUnread: function (params) { selectionToggleUnread: function (params) {
params = params || {}; params = params || {};
const cmode = params.cmode || 2; const cmode = params.cmode != undefined ? params.cmode : 2;
const callback = params.callback;
const no_error = params.no_error || false; const no_error = params.no_error || false;
const ids = params.ids || Headlines.getSelected(); const ids = params.ids || Headlines.getSelected();
@ -653,16 +664,6 @@ define(["dojo/_base/declare"], function (declare) {
} }
} }
}); });
const query = {
op: "rpc", method: "catchupSelected",
cmode: cmode, ids: ids.toString()
};
xhrPost("backend.php", query, (transport) => {
App.handleRpcJson(transport);
if (callback) callback(transport);
});
}, },
selectionToggleMarked: function (ids) { selectionToggleMarked: function (ids) {
ids = ids || Headlines.getSelected(); ids = ids || Headlines.getSelected();
@ -798,7 +799,7 @@ define(["dojo/_base/declare"], function (declare) {
const row = $("RROW-" + id); const row = $("RROW-" + id);
if (row) { if (row) {
const origClassName = row.className; //const origClassName = row.className;
if (cmode == undefined) cmode = 2; if (cmode == undefined) cmode = 2;
@ -813,12 +814,6 @@ define(["dojo/_base/declare"], function (declare) {
row.toggleClassName("Unread"); row.toggleClassName("Unread");
break; break;
} }
if (row.className != origClassName)
xhrPost("backend.php",
{op: "rpc", method: "catchupSelected", cmode: cmode, ids: id}, (transport) => {
App.handleRpcJson(transport);
});
} }
}, },
selectionRemoveLabel: function (id, ids) { selectionRemoveLabel: function (id, ids) {
@ -1043,38 +1038,7 @@ define(["dojo/_base/declare"], function (declare) {
return; return;
} }
Headlines.selectionToggleUnread({callback: Feeds.reloadCurrent, no_error: 1}); Headlines.selectionToggleUnread({ids: rows, cmode: 0});
},
catchupBatched: function (callback) {
console.log("catchupBatched, size=", this.catchup_id_batch.length);
if (this.catchup_id_batch.length > 0) {
// make a copy of the array
const batch = this.catchup_id_batch.slice();
const query = {
op: "rpc", method: "catchupSelected",
cmode: 0, ids: batch.toString()
};
xhrPost("backend.php", query, (transport) => {
const reply = App.handleRpcJson(transport);
if (reply) {
const batch = reply.ids;
batch.each(function (id) {
const elem = $("RROW-" + id);
if (elem) elem.removeClassName("Unread");
Headlines.catchup_id_batch.remove(id);
});
}
if (callback) callback();
});
} else {
if (callback) callback();
}
}, },
catchupRelativeTo: function (below, id) { catchupRelativeTo: function (below, id) {

Loading…
Cancel
Save