From 9d912206657c3899e9626ded618291abc94b28d7 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 2 Jun 2011 18:56:20 +0200 Subject: [PATCH 1/4] Bind storage event for the thread watcher list auto-refresh instead of checking every seconds. --- 4chan_x.user.js | 25 +++++++++---------------- script.coffee | 10 +--------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 64bc685e0..e87d0568c 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1531,14 +1531,12 @@ $.before(input, favicon); } watcher.refresh($.getValue('watched', {})); - return setInterval((function() { - if (watcher.lastUpdated < $.getValue('watcher.lastUpdated', 0)) { - return watcher.refresh($.getValue('watched', {})); - } - }), 1000); + return $.bind(window, 'storage', (function() { + return watcher.refresh($.getValue('watched', {})); + })); }, refresh: function(watched) { - var board, dialog, div, favicon, id, link, props, watchedBoard, x, _i, _j, _len, _len2, _ref, _ref2, _ref3; + var board, dialog, div, favicon, id, link, props, watchedBoard, x, _i, _j, _len, _len2, _ref, _ref2, _ref3, _results; dialog = $('#watcher'); _ref = $$('div:not(.move)', dialog); for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -1561,16 +1559,13 @@ } watchedBoard = watched[g.BOARD] || {}; _ref3 = $$('img.favicon'); + _results = []; for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) { favicon = _ref3[_j]; id = favicon.nextSibling.name; - if (id in watchedBoard) { - favicon.src = Favicon["default"]; - } else { - favicon.src = Favicon.empty; - } + _results.push(id in watchedBoard ? favicon.src = Favicon["default"] : favicon.src = Favicon.empty); } - return watcher.lastUpdated = Date.now(); + return _results; }, cb: { toggle: function(e) { @@ -1597,8 +1592,7 @@ watched = $.getValue('watched', {}); delete watched[board][id]; $.setValue('watched', watched); - watcher.refresh(watched); - return $.setValue('watcher.lastUpdated', Date.now()); + return watcher.refresh(watched); }, watch: function(thread, id) { var props, tc, watched, _name; @@ -1611,8 +1605,7 @@ watched[_name = g.BOARD] || (watched[_name] = {}); watched[g.BOARD][id] = props; $.setValue('watched', watched); - watcher.refresh(watched); - return $.setValue('watcher.lastUpdated', Date.now()); + return watcher.refresh(watched); } }; anonymize = { diff --git a/script.coffee b/script.coffee index a68586a82..08b1e34f4 100644 --- a/script.coffee +++ b/script.coffee @@ -1245,10 +1245,7 @@ watcher = #populate watcher, display watch buttons watcher.refresh $.getValue 'watched', {} - setInterval (-> - if watcher.lastUpdated < $.getValue 'watcher.lastUpdated', 0 - watcher.refresh $.getValue 'watched', {} - ), 1000 + $.bind window, 'storage', (-> watcher.refresh $.getValue 'watched', {}) refresh: (watched) -> dialog = $ '#watcher' @@ -1272,7 +1269,6 @@ watcher = favicon.src = Favicon.default else favicon.src = Favicon.empty - watcher.lastUpdated = Date.now() cb: toggle: (e) -> @@ -1294,9 +1290,7 @@ watcher = watched = $.getValue 'watched', {} delete watched[board][id] $.setValue 'watched', watched - watcher.refresh watched - $.setValue 'watcher.lastUpdated', Date.now() watch: (thread, id) -> tc = $('span.filetitle', thread).textContent or $('blockquote', thread).textContent @@ -1308,9 +1302,7 @@ watcher = watched[g.BOARD] or= {} watched[g.BOARD][id] = props $.setValue 'watched', watched - watcher.refresh watched - $.setValue 'watcher.lastUpdated', Date.now() anonymize = init: -> From d90fd1e016bc254a201ad12599455f4788c273e7 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 2 Jun 2011 20:30:57 +0200 Subject: [PATCH 2/4] getValue into refresh --- 4chan_x.user.js | 13 +++++++------ script.coffee | 11 ++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index e87d0568c..fe3fc0daa 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1530,13 +1530,14 @@ $.bind(favicon, 'click', watcher.cb.toggle); $.before(input, favicon); } - watcher.refresh($.getValue('watched', {})); + watcher.refresh(); return $.bind(window, 'storage', (function() { - return watcher.refresh($.getValue('watched', {})); + return watcher.refresh(); })); }, - refresh: function(watched) { - var board, dialog, div, favicon, id, link, props, watchedBoard, x, _i, _j, _len, _len2, _ref, _ref2, _ref3, _results; + refresh: function() { + var board, dialog, div, favicon, id, link, props, watched, watchedBoard, x, _i, _j, _len, _len2, _ref, _ref2, _ref3, _results; + watched = $.getValue('watched', {}); dialog = $('#watcher'); _ref = $$('div:not(.move)', dialog); for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -1592,7 +1593,7 @@ watched = $.getValue('watched', {}); delete watched[board][id]; $.setValue('watched', watched); - return watcher.refresh(watched); + return watcher.refresh(); }, watch: function(thread, id) { var props, tc, watched, _name; @@ -1605,7 +1606,7 @@ watched[_name = g.BOARD] || (watched[_name] = {}); watched[g.BOARD][id] = props; $.setValue('watched', watched); - return watcher.refresh(watched); + return watcher.refresh(); } }; anonymize = { diff --git a/script.coffee b/script.coffee index 08b1e34f4..df0d118b1 100644 --- a/script.coffee +++ b/script.coffee @@ -1243,11 +1243,12 @@ watcher = $.before input, favicon #populate watcher, display watch buttons - watcher.refresh $.getValue 'watched', {} + watcher.refresh() - $.bind window, 'storage', (-> watcher.refresh $.getValue 'watched', {}) + $.bind window, 'storage', (-> watcher.refresh() ) - refresh: (watched) -> + refresh: -> + watched = $.getValue 'watched', {} dialog = $ '#watcher' for div in $$ 'div:not(.move)', dialog $.rm div @@ -1290,7 +1291,7 @@ watcher = watched = $.getValue 'watched', {} delete watched[board][id] $.setValue 'watched', watched - watcher.refresh watched + watcher.refresh() watch: (thread, id) -> tc = $('span.filetitle', thread).textContent or $('blockquote', thread).textContent @@ -1302,7 +1303,7 @@ watcher = watched[g.BOARD] or= {} watched[g.BOARD][id] = props $.setValue 'watched', watched - watcher.refresh watched + watcher.refresh() anonymize = init: -> From 99b1cfed0b99e40bf59814a1aca635984e9cad1b Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 2 Jun 2011 21:21:37 +0200 Subject: [PATCH 3/4] Refresh only if the watcher list has changed. --- 4chan_x.user.js | 6 ++++-- script.coffee | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index fe3fc0daa..4ce17c816 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1531,8 +1531,10 @@ $.before(input, favicon); } watcher.refresh(); - return $.bind(window, 'storage', (function() { - return watcher.refresh(); + return $.bind(window, 'storage', (function(e) { + if (e.key === 'AEOS.4chan_x.watched') { + return watcher.refresh(); + } })); }, refresh: function() { diff --git a/script.coffee b/script.coffee index df0d118b1..692f81278 100644 --- a/script.coffee +++ b/script.coffee @@ -1245,7 +1245,7 @@ watcher = #populate watcher, display watch buttons watcher.refresh() - $.bind window, 'storage', (-> watcher.refresh() ) + $.bind window, 'storage', ((e) -> watcher.refresh() if e.key is 'AEOS.4chan_x.watched') refresh: -> watched = $.getValue 'watched', {} From 3761376322fa11bb1cae53f0722b0f29f204d363 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 2 Jun 2011 21:27:01 +0200 Subject: [PATCH 4/4] Bitchy. --- 4chan_x.user.js | 4 ++-- script.coffee | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 4ce17c816..567409d3f 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1531,11 +1531,11 @@ $.before(input, favicon); } watcher.refresh(); - return $.bind(window, 'storage', (function(e) { + return $.bind(window, 'storage', function(e) { if (e.key === 'AEOS.4chan_x.watched') { return watcher.refresh(); } - })); + }); }, refresh: function() { var board, dialog, div, favicon, id, link, props, watched, watchedBoard, x, _i, _j, _len, _len2, _ref, _ref2, _ref3, _results; diff --git a/script.coffee b/script.coffee index 692f81278..c63860aa0 100644 --- a/script.coffee +++ b/script.coffee @@ -1245,7 +1245,7 @@ watcher = #populate watcher, display watch buttons watcher.refresh() - $.bind window, 'storage', ((e) -> watcher.refresh() if e.key is 'AEOS.4chan_x.watched') + $.bind window, 'storage', (e) -> watcher.refresh() if e.key is 'AEOS.4chan_x.watched' refresh: -> watched = $.getValue 'watched', {}