js: simplify counter updating, remove some unused stuff

This commit is contained in:
Andrew Dolgov 2013-02-01 13:09:43 +04:00
parent 1e6463fde9
commit 5b55e9e25c
6 changed files with 44 additions and 145 deletions

View File

@ -104,11 +104,7 @@ class API extends Handler {
/* Method added for ttrss-reader for Android */ /* Method added for ttrss-reader for Android */
function getCounters() { function getCounters() {
print $this->wrap(self::STATUS_OK, getAllCounters($this->link));
/* flct (flc is the default) FIXME: document */
$output_mode = db_escape_string($_REQUEST["output_mode"]);
print $this->wrap(self::STATUS_OK, getAllCounters($this->link, $output_mode));
} }
function getFeeds() { function getFeeds() {

View File

@ -243,20 +243,15 @@ class RPC extends Handler_Protected {
$reply = array(); $reply = array();
if ($seq) $reply['seq'] = $seq; if ($seq) $reply['seq'] = $seq;
if ($last_article_id != getLastArticleId($this->link)) { if ($last_article_id != getLastArticleId($this->link)) {
$omode = $_REQUEST["omode"]; $reply['counters'] = getAllCounters($this->link);
}
if ($omode != "T") $reply['runtime-info'] = make_runtime_info($this->link);
$reply['counters'] = getAllCounters($this->link, $omode);
else
$reply['counters'] = getGlobalCounters($this->link);
}
$reply['runtime-info'] = make_runtime_info($this->link); print json_encode($reply);
print json_encode($reply);
} }
/* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */ /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */

View File

@ -1075,18 +1075,13 @@
} }
} }
function getAllCounters($link, $omode = "flc", $active_feed = false) { function getAllCounters($link) {
if (!$omode) $omode = "flc";
$data = getGlobalCounters($link); $data = getGlobalCounters($link);
$data = array_merge($data, getVirtCounters($link)); $data = array_merge($data, getVirtCounters($link));
$data = array_merge($data, getLabelCounters($link));
if (strchr($omode, "l")) $data = array_merge($data, getLabelCounters($link)); $data = array_merge($data, getFeedCounters($link, $active_feed));
if (strchr($omode, "f")) $data = array_merge($data, getFeedCounters($link, $active_feed)); $data = array_merge($data, getCategoryCounters($link));
if (strchr($omode, "t")) $data = array_merge($data, getTagCounters($link));
if (strchr($omode, "c")) $data = array_merge($data, getCategoryCounters($link));
return $data; return $data;
} }
@ -1374,36 +1369,6 @@
return $ret_arr; return $ret_arr;
} }
function getTagCounters($link) {
$ret_arr = array();
$result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
AND ref_id = id AND unread = true)) AS count FROM ttrss_tags
WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
ORDER BY count DESC LIMIT 55");
$tags = array();
while ($line = db_fetch_assoc($result)) {
$tags[$line["tag_name"]] += $line["count"];
}
foreach (array_keys($tags) as $tag) {
$unread = $tags[$tag];
$tag = htmlspecialchars($tag);
$cv = array("id" => $tag,
"kind" => "tag",
"counter" => $unread);
array_push($ret_arr, $cv);
}
return $ret_arr;
}
function getVirtCounters($link) { function getVirtCounters($link) {
$ret_arr = array(); $ret_arr = array();

View File

@ -3,8 +3,6 @@ var _infscroll_request_sent = 0;
var _search_query = false; var _search_query = false;
var _viewfeed_last = 0; var _viewfeed_last = 0;
var counter_timeout_id = false;
var counters_last_request = 0; var counters_last_request = 0;
function viewCategory(cat) { function viewCategory(cat) {
@ -218,7 +216,8 @@ function feedlist_init() {
hideOrShowFeeds(getInitParam("hide_read_feeds") == 1); hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
setTimeout("timeout()", 5000); request_counters(true);
timeout();
setTimeout("precache_headlines_idle()", 15000); setTimeout("precache_headlines_idle()", 15000);
} catch (e) { } catch (e) {
@ -226,46 +225,34 @@ function feedlist_init() {
} }
} }
function request_counters_real() {
function request_counters(force) {
try { try {
console.log("requesting counters...");
var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
query = query + "&omode=flc";
new Ajax.Request("backend.php", {
parameters: query,
onComplete: function(transport) {
try {
handle_rpc_json(transport);
} catch (e) {
exception_error("viewfeed/getcounters", e);
}
} });
} catch (e) {
exception_error("request_counters_real", e);
}
}
function request_counters() {
try {
//if (getInitParam("bw_limit") == "1") return;
var date = new Date(); var date = new Date();
var timestamp = Math.round(date.getTime() / 1000); var timestamp = Math.round(date.getTime() / 1000);
if (timestamp - counters_last_request > 5) { if (force || timestamp - counters_last_request > 15) {
console.log("scheduling request of counters..."); console.log("scheduling request of counters...");
window.clearTimeout(counter_timeout_id);
counter_timeout_id = window.setTimeout("request_counters_real()", 1000);
counters_last_request = timestamp; counters_last_request = timestamp;
var query = "?op=rpc&method=getAllCounters&seq=" + next_seq();
if (!force)
query = query + "&last_article_id=" + getInitParam("last_article_id");
console.log(query);
new Ajax.Request("backend.php", {
parameters: query,
onComplete: function(transport) {
try {
handle_rpc_json(transport);
} catch (e) {
exception_error("request_counters", e);
}
} });
} else { } else {
console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request)); console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request));
} }
@ -567,6 +554,8 @@ function decrementFeedCounter(feed, is_cat) {
if (ctr > 0) { if (ctr > 0) {
setFeedUnread(feed, is_cat, ctr - 1); setFeedUnread(feed, is_cat, ctr - 1);
global_unread = global_unread - 1;
updateTitle();
if (!is_cat) { if (!is_cat) {
var cat = parseInt(getFeedCategory(feed)); var cat = parseInt(getFeedCategory(feed));

View File

@ -1,14 +1,9 @@
var total_unread = 0;
var global_unread = -1; var global_unread = -1;
var firsttime_update = true;
var _active_feed_id = undefined; var _active_feed_id = undefined;
var _active_feed_is_cat = false; var _active_feed_is_cat = false;
var hotkey_prefix = false; var hotkey_prefix = false;
var hotkey_prefix_pressed = false; var hotkey_prefix_pressed = false;
var _force_scheduled_update = false;
var last_scheduled_update = false;
var _widescreen_mode = false; var _widescreen_mode = false;
var _rpc_seq = 0; var _rpc_seq = 0;
function next_seq() { function next_seq() {
@ -105,8 +100,6 @@ function updateFeedList() {
id: "feedTree", id: "feedTree",
}, "feedTree"); }, "feedTree");
_force_scheduled_update = true;
/* var menu = new dijit.Menu({id: 'feedMenu'}); /* var menu = new dijit.Menu({id: 'feedMenu'});
menu.addChild(new dijit.MenuItem({ menu.addChild(new dijit.MenuItem({
@ -176,51 +169,10 @@ function viewCurrentFeed(method) {
} }
function timeout() { function timeout() {
if (getInitParam("bw_limit") == "1") return; if (getInitParam("bw_limit") != "1") {
request_counters();
try { setTimeout("timeout()", 60*1000);
var date = new Date();
var ts = Math.round(date.getTime() / 1000);
if (ts - last_scheduled_update > 10 || _force_scheduled_update) {
//console.log("timeout()");
window.clearTimeout(counter_timeout_id);
var query_str = "?op=rpc&method=getAllCounters&seq=" + next_seq();
var omode;
if (firsttime_update && !navigator.userAgent.match("Opera")) {
firsttime_update = false;
omode = "T";
} else {
omode = "flc";
}
query_str = query_str + "&omode=" + omode;
if (!_force_scheduled_update)
query_str = query_str + "&last_article_id=" + getInitParam("last_article_id");
//console.log("[timeout]" + query_str);
new Ajax.Request("backend.php", {
parameters: query_str,
onComplete: function(transport) {
handle_rpc_json(transport, !_force_scheduled_update);
_force_scheduled_update = false;
} });
last_scheduled_update = ts;
}
} catch (e) {
exception_error("timeout", e);
} }
setTimeout("timeout()", 3000);
} }
function search() { function search() {
@ -960,7 +912,7 @@ function handle_rpc_json(transport, scheduled_call) {
if (message == "UPDATE_COUNTERS") { if (message == "UPDATE_COUNTERS") {
console.log("need to refresh counters..."); console.log("need to refresh counters...");
setInitParam("last_article_id", -1); setInitParam("last_article_id", -1);
_force_scheduled_update = true; request_counters(true);
} }
} }

View File

@ -1650,6 +1650,8 @@ function cdmClicked(event, id) {
zoomToArticle(event, id); zoomToArticle(event, id);
} }
request_counters();
} catch (e) { } catch (e) {
exception_error("cdmClicked"); exception_error("cdmClicked");
} }
@ -2058,7 +2060,7 @@ function render_local_headlines(feed, is_cat, obj) {
function precache_headlines_idle() { function precache_headlines_idle() {
try { try {
if (!feed_precache_timeout_id) { if (getInitParam("bw_limit") != "1" && !feed_precache_timeout_id) {
if (get_timestamp() - _viewfeed_last > 120) { if (get_timestamp() - _viewfeed_last > 120) {
var feeds = dijit.byId("feedTree").getVisibleUnreadFeeds(); var feeds = dijit.byId("feedTree").getVisibleUnreadFeeds();
@ -2084,8 +2086,8 @@ function precache_headlines_idle() {
function precache_headlines() { function precache_headlines() {
try { try {
if (getInitParam("bw_limit") != "1" && !feed_precache_timeout_id) {
if (!feed_precache_timeout_id) {
feed_precache_timeout_id = window.setTimeout(function() { feed_precache_timeout_id = window.setTimeout(function() {
var nuf = getNextUnreadFeed(getActiveFeedId(), activeFeedIsCat()); var nuf = getNextUnreadFeed(getActiveFeedId(), activeFeedIsCat());
var nf = dijit.byId("feedTree").getNextFeed(getActiveFeedId(), activeFeedIsCat()); var nf = dijit.byId("feedTree").getNextFeed(getActiveFeedId(), activeFeedIsCat());