hopefully optimize watch.js a lot. fixes #24

This commit is contained in:
Marcin Łabanowski 2014-08-10 18:44:23 +02:00
parent c3662d4a59
commit 91519d9052

View File

@ -152,12 +152,21 @@ $(function(){
}; };
var update_pinned = function() { var update_pinned = function() {
if (updating_suspended) return;
if (typeof update_title != "undefined") update_title(); if (typeof update_title != "undefined") update_title();
var bl = $('.boardlist').first(); var bl = $('.boardlist').first();
$('#watch-pinned, .watch-menu').remove(); $('#watch-pinned, .watch-menu').remove();
var pinned = $('<div id="watch-pinned"></div>').appendTo(bl); var pinned = $('<div id="watch-pinned"></div>').appendTo(bl);
if (device_type == "desktop")
bl.off().on("mouseenter", function() {
updating_suspended = true;
}).on("mouseleave", function() {
updating_suspended = false;
});
var st = storage(); var st = storage();
for (var i in st) { for (var i in st) {
if (is_pinned(st[i])) { if (is_pinned(st[i])) {
@ -198,7 +207,6 @@ $(function(){
if (device_type == "desktop") if (device_type == "desktop")
link.off().mouseenter(function() { link.off().mouseenter(function() {
updating_suspended = true;
$('.cb-menu').remove(); $('.cb-menu').remove();
var board = $(this).attr("data-board"); var board = $(this).attr("data-board");
@ -216,7 +224,6 @@ $(function(){
wl.find("a.cb-menuitem").each(init_hover); wl.find("a.cb-menuitem").each(init_hover);
}).mouseleave(function() { }).mouseleave(function() {
updating_suspended = false;
$('.boardlist .cb-menu').remove(); $('.boardlist .cb-menu').remove();
}); });
} }
@ -232,30 +239,45 @@ $(function(){
} }
}; };
var fetch_jsons = function() { var fetch_jsons = function() {
if (updating_suspended) return;
if (window_active) check_scroll(); if (window_active) check_scroll();
var st = storage(); var st = storage();
var sched = 0;
var sched_diff = 100;
for (var i in st) { for (var i in st) {
if (st[i].watched) { if (st[i].watched) {
var r = $.getJSON(configRoot+i+"/threads.json", function(j, x, r) { (function(i) {
handle_board_json(r.board, j); setTimeout(function() {
}); var r = $.getJSON(configRoot+i+"/threads.json", function(j, x, r) {
r.board = i; handle_board_json(r.board, j);
});
r.board = i;
}, sched);
sched += sched_diff;
})(i);
} }
else if (st[i].threads) { else if (st[i].threads) {
for (var j in st[i].threads) { for (var j in st[i].threads) {
var r = $.getJSON(configRoot+i+"/res/"+j+".json", function(k, x, r) { (function(i,j) {
handle_thread_json(r.board, r.thread, k); setTimeout(function() {
}).error(function(r) { var r = $.getJSON(configRoot+i+"/res/"+j+".json", function(k, x, r) {
if(r.status == 404) handle_thread_404(r.board, r.thread); handle_thread_json(r.board, r.thread, k);
}); }).error(function(r) {
if(r.status == 404) handle_thread_404(r.board, r.thread);
});
r.board = i; r.board = i;
r.thread = j; r.thread = j;
}, sched);
})(i,j);
sched += sched_diff;
} }
} }
} }
setTimeout(fetch_jsons, sched + sched_diff);
}; };
var handle_board_json = function(board, json) { var handle_board_json = function(board, json) {
@ -293,21 +315,27 @@ $(function(){
status = status || {}; status = status || {};
status[board] = status[board] || {}; status[board] = status[board] || {};
status[board].last_thread = last_thread; if (status[board].last_thread != last_thread || status[board].new_threads != new_threads) {
status[board].new_threads = new_threads; status[board].last_thread = last_thread;
update_pinned(); status[board].new_threads = new_threads;
update_pinned();
}
}; };
var handle_thread_json = function(board, threadid, json) { var handle_thread_json = function(board, threadid, json) {
var new_posts = 0;
for (var i in json.posts) { for (var i in json.posts) {
var post = json.posts[i]; var post = json.posts[i];
var new_posts = 0;
if (post.time > storage()[board].threads[threadid] / 1000) { if (post.time > storage()[board].threads[threadid] / 1000) {
new_posts++; new_posts++;
} }
status = status || {}; }
status[board] = status[board] || {};
status[board].threads = status[board].threads || {}; status = status || {};
status[board] = status[board] || {};
status[board].threads = status[board].threads || {};
if (status[board].threads[threadid] != new_posts) {
status[board].threads[threadid] = new_posts; status[board].threads[threadid] = new_posts;
update_pinned(); update_pinned();
} }
@ -316,8 +344,10 @@ $(function(){
status = status || {}; status = status || {};
status[board] = status[board] || {}; status[board] = status[board] || {};
status[board].threads = status[board].threads || {}; status[board].threads = status[board].threads || {};
status[board].threads[threadid] = -404; //notify 404 if (status[board].threads[threadid] != -404) {
update_pinned(); status[board].threads[threadid] = -404; //notify 404
update_pinned();
}
}; };
if (active_page == "thread") { if (active_page == "thread") {
@ -386,7 +416,7 @@ $(function(){
$(window).scroll(function() { $(window).scroll(function() {
var refresh = check_scroll(); var refresh = check_scroll();
if (refresh) { if (refresh) {
fetch_jsons(); //fetch_jsons();
refresh = false; refresh = false;
} }
}); });
@ -417,5 +447,4 @@ $(function(){
update_pinned(); update_pinned();
fetch_jsons(); fetch_jsons();
setInterval(fetch_jsons, 10000);
}); });