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 */
function getCounters() {
/* 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));
print $this->wrap(self::STATUS_OK, getAllCounters($this->link));
}
function getFeeds() {

View File

@ -246,12 +246,7 @@ class RPC extends Handler_Protected {
if ($seq) $reply['seq'] = $seq;
if ($last_article_id != getLastArticleId($this->link)) {
$omode = $_REQUEST["omode"];
if ($omode != "T")
$reply['counters'] = getAllCounters($this->link, $omode);
else
$reply['counters'] = getGlobalCounters($this->link);
$reply['counters'] = getAllCounters($this->link);
}
$reply['runtime-info'] = make_runtime_info($this->link);

View File

@ -1075,18 +1075,13 @@
}
}
function getAllCounters($link, $omode = "flc", $active_feed = false) {
if (!$omode) $omode = "flc";
function getAllCounters($link) {
$data = getGlobalCounters($link);
$data = array_merge($data, getVirtCounters($link));
if (strchr($omode, "l")) $data = array_merge($data, getLabelCounters($link));
if (strchr($omode, "f")) $data = array_merge($data, getFeedCounters($link, $active_feed));
if (strchr($omode, "t")) $data = array_merge($data, getTagCounters($link));
if (strchr($omode, "c")) $data = array_merge($data, getCategoryCounters($link));
$data = array_merge($data, getLabelCounters($link));
$data = array_merge($data, getFeedCounters($link, $active_feed));
$data = array_merge($data, getCategoryCounters($link));
return $data;
}
@ -1374,36 +1369,6 @@
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) {
$ret_arr = array();

View File

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

View File

@ -1,14 +1,9 @@
var total_unread = 0;
var global_unread = -1;
var firsttime_update = true;
var _active_feed_id = undefined;
var _active_feed_is_cat = false;
var hotkey_prefix = false;
var hotkey_prefix_pressed = false;
var _force_scheduled_update = false;
var last_scheduled_update = false;
var _widescreen_mode = false;
var _rpc_seq = 0;
function next_seq() {
@ -105,8 +100,6 @@ function updateFeedList() {
id: "feedTree",
}, "feedTree");
_force_scheduled_update = true;
/* var menu = new dijit.Menu({id: 'feedMenu'});
menu.addChild(new dijit.MenuItem({
@ -176,51 +169,10 @@ function viewCurrentFeed(method) {
}
function timeout() {
if (getInitParam("bw_limit") == "1") return;
try {
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";
if (getInitParam("bw_limit") != "1") {
request_counters();
setTimeout("timeout()", 60*1000);
}
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() {
@ -960,7 +912,7 @@ function handle_rpc_json(transport, scheduled_call) {
if (message == "UPDATE_COUNTERS") {
console.log("need to refresh counters...");
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);
}
request_counters();
} catch (e) {
exception_error("cdmClicked");
}
@ -2058,7 +2060,7 @@ function render_local_headlines(feed, is_cat, obj) {
function precache_headlines_idle() {
try {
if (!feed_precache_timeout_id) {
if (getInitParam("bw_limit") != "1" && !feed_precache_timeout_id) {
if (get_timestamp() - _viewfeed_last > 120) {
var feeds = dijit.byId("feedTree").getVisibleUnreadFeeds();
@ -2084,8 +2086,8 @@ function precache_headlines_idle() {
function precache_headlines() {
try {
if (getInitParam("bw_limit") != "1" && !feed_precache_timeout_id) {
if (!feed_precache_timeout_id) {
feed_precache_timeout_id = window.setTimeout(function() {
var nuf = getNextUnreadFeed(getActiveFeedId(), activeFeedIsCat());
var nf = dijit.byId("feedTree").getNextFeed(getActiveFeedId(), activeFeedIsCat());