$.cache - closes #112

This commit is contained in:
James Campos 2011-05-24 23:02:42 -07:00
parent 40a4db4aab
commit 0cec573e18
2 changed files with 60 additions and 68 deletions

View File

@ -264,6 +264,29 @@
r.send(); r.send();
return r; return r;
}, },
cache: function(url, cb) {
var req;
if (req = $.cache.requests[url]) {
if (req.readyState === 4) {
return cb.call(req);
} else {
return req.callbacks.push(cb);
}
} else {
req = $.get(url, (function() {
var cb, _i, _len, _ref, _results;
_ref = this.callbacks;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
cb = _ref[_i];
_results.push(cb.call(this));
}
return _results;
}));
req.callbacks = [cb];
return $.cache.requests[url] = req;
}
},
cb: { cb: {
checked: function() { checked: function() {
return $.setValue(this.name, this.checked); return $.setValue(this.name, this.checked);
@ -391,6 +414,7 @@
} }
} }
}); });
$.cache.requests = {};
if (typeof GM_deleteValue !== "undefined" && GM_deleteValue !== null) { if (typeof GM_deleteValue !== "undefined" && GM_deleteValue !== null) {
$.extend($, { $.extend($, {
deleteValue: function(name) { deleteValue: function(name) {
@ -467,20 +491,14 @@
return _results; return _results;
}, },
expand: function(e) { expand: function(e) {
var a, replyID, req, threadID, _, _ref; var a, replyID, threadID, _, _ref;
e.preventDefault(); e.preventDefault();
_ref = this.href.match(/(\d+)#(\d+)/), _ = _ref[0], threadID = _ref[1], replyID = _ref[2]; _ref = this.href.match(/(\d+)#(\d+)/), _ = _ref[0], threadID = _ref[1], replyID = _ref[2];
this.textContent = "Loading " + replyID + "..."; this.textContent = "Loading " + replyID + "...";
if (req = g.requests[threadID]) { a = this;
if (req.readyState === 4) { return $.cache(this.href, (function() {
return expandComment.parse(req, this, threadID, replyID); return expandComment.parse(this, a, threadID, replyID);
} }));
} else {
a = this;
return g.requests[threadID] = $.get(this.href, (function() {
return expandComment.parse(this, a, threadID, replyID);
}));
}
}, },
parse: function(req, a, threadID, replyID) { parse: function(req, a, threadID, replyID) {
var body, bq, reply, _i, _len, _ref; var body, bq, reply, _i, _len, _ref;
@ -530,25 +548,18 @@
} }
}, },
toggle: function(thread) { toggle: function(thread) {
var a, num, prev, req, table, threadID, _results; var a, num, prev, table, threadID, _results;
threadID = thread.firstChild.id; threadID = thread.firstChild.id;
a = $('a.omittedposts', thread); a = $('a.omittedposts', thread);
switch (a.textContent[0]) { switch (a.textContent[0]) {
case '+': case '+':
a.textContent = a.textContent.replace('+', 'X Loading...'); a.textContent = a.textContent.replace('+', 'X Loading...');
if (req = g.requests[threadID]) { return $.cache("res/" + threadID, (function() {
if (req.readyState === 4) { return expandThread.parse(this, thread, a);
return expandThread.parse(req, thread, a); }));
}
} else {
return g.requests[threadID] = $.get("res/" + threadID, (function() {
return expandThread.parse(this, thread, a);
}));
}
break;
case 'X': case 'X':
a.textContent = a.textContent.replace('X Loading...', '+'); a.textContent = a.textContent.replace('X Loading...', '+');
return g.requests[id].abort(); return $.cache["res/" + threadID].abort();
case '-': case '-':
a.textContent = a.textContent.replace('-', '+'); a.textContent = a.textContent.replace('-', '+');
num = g.BOARD === 'b' ? 3 : 5; num = g.BOARD === 'b' ? 3 : 5;
@ -1750,7 +1761,7 @@
return _results; return _results;
}, },
toggle: function(e) { toggle: function(e) {
var el, id, inline, req, root, table, threadID; var el, id, inline, root, table, threadID;
id = this.hash.slice(1); id = this.hash.slice(1);
e.preventDefault(); e.preventDefault();
root = $.x('ancestor::td[1]', this); root = $.x('ancestor::td[1]', this);
@ -1778,15 +1789,9 @@
}); });
$.after(this.parentNode, inline); $.after(this.parentNode, inline);
threadID = this.pathname.split('/').pop() || $.x('ancestor::div[@class="thread"]/div', this).id; threadID = this.pathname.split('/').pop() || $.x('ancestor::div[@class="thread"]/div', this).id;
if (req = g.requests[threadID]) { $.cache(this.href, (function() {
if (req.readyState === 4) { return quoteInline.parse(this, id, threadID, inline);
quoteInline.parse(req, id, threadID, inline); }));
}
} else {
g.requests[threadID] = $.get(this.href, (function() {
return quoteInline.parse(this, id, threadID, inline);
}));
}
} }
return $.addClass(this, 'inlined'); return $.addClass(this, 'inlined');
}, },
@ -1859,7 +1864,7 @@
return $.removeClass(el, 'qphl'); return $.removeClass(el, 'qphl');
}, },
mouseover: function(e) { mouseover: function(e) {
var el, id, qp, quote, replyID, req, threadID, _i, _len, _ref; var el, id, qp, quote, replyID, threadID, _i, _len, _ref;
id = this.hash.slice(1); id = this.hash.slice(1);
qp = $('#qp'); qp = $('#qp');
if (el = d.getElementById(id)) { if (el = d.getElementById(id)) {
@ -1878,15 +1883,9 @@
} else { } else {
qp.innerHTML = "Loading " + id + "..."; qp.innerHTML = "Loading " + id + "...";
threadID = this.pathname.split('/').pop(); threadID = this.pathname.split('/').pop();
if (req = g.requests[threadID]) { $.cache(this.href, (function() {
if (req.readyState === 4) { return quotePreview.parse(this, id, threadID);
quotePreview.parse(req, id, threadID); }));
}
} else {
g.requests[threadID] = $.get(this.href, (function() {
return quotePreview.parse(this, id, threadID);
}));
}
} }
ui.el = qp; ui.el = qp;
ui.winHeight = d.body.clientHeight; ui.winHeight = d.body.clientHeight;
@ -2336,7 +2335,6 @@
}; };
NAMESPACE = 'AEOS.4chan_x.'; NAMESPACE = 'AEOS.4chan_x.';
g = { g = {
requests: {},
callbacks: [] callbacks: []
}; };
main = { main = {

View File

@ -176,6 +176,16 @@ $.extend $,
r.open 'get', url, true r.open 'get', url, true
r.send() r.send()
r r
cache: (url, cb) ->
if req = $.cache.requests[url]
if req.readyState is 4
cb.call req
else
req.callbacks.push cb
else
req = $.get url, (-> cb.call @ for cb in @callbacks)
req.callbacks = [cb]
$.cache.requests[url] = req
cb: cb:
checked: -> checked: ->
$.setValue @name, @checked $.setValue @name, @checked
@ -274,6 +284,8 @@ $.extend $,
#after first sunday #after first sunday
return false return false
$.cache.requests = {}
if GM_deleteValue? if GM_deleteValue?
$.extend $, $.extend $,
deleteValue: (name) -> deleteValue: (name) ->
@ -324,12 +336,8 @@ expandComment =
e.preventDefault() e.preventDefault()
[_, threadID, replyID] = @href.match /(\d+)#(\d+)/ [_, threadID, replyID] = @href.match /(\d+)#(\d+)/
@textContent = "Loading #{replyID}..." @textContent = "Loading #{replyID}..."
if req = g.requests[threadID] a = @
if req.readyState is 4 $.cache @href, (-> expandComment.parse @, a, threadID, replyID)
expandComment.parse req, @, threadID, replyID
else
a = @
g.requests[threadID] = $.get @href, (-> expandComment.parse @, a, threadID, replyID)
parse: (req, a, threadID, replyID) -> parse: (req, a, threadID, replyID) ->
if req.status isnt 200 if req.status isnt 200
a.textContent = "#{req.status} #{req.statusText}" a.textContent = "#{req.status} #{req.statusText}"
@ -370,15 +378,11 @@ expandThread =
switch a.textContent[0] switch a.textContent[0]
when '+' when '+'
a.textContent = a.textContent.replace '+', 'X Loading...' a.textContent = a.textContent.replace '+', 'X Loading...'
if req = g.requests[threadID] $.cache "res/#{threadID}", (-> expandThread.parse @, thread, a)
if req.readyState is 4
expandThread.parse req, thread, a
else
g.requests[threadID] = $.get "res/#{threadID}", (-> expandThread.parse @, thread, a)
when 'X' when 'X'
a.textContent = a.textContent.replace 'X Loading...', '+' a.textContent = a.textContent.replace 'X Loading...', '+'
g.requests[id].abort() $.cache["res/#{threadID}"].abort()
when '-' when '-'
a.textContent = a.textContent.replace '-', '+' a.textContent = a.textContent.replace '-', '+'
@ -1425,12 +1429,7 @@ quoteInline =
$.after @parentNode, inline $.after @parentNode, inline
# or ... is for index page new posts. # or ... is for index page new posts.
threadID = @pathname.split('/').pop() or $.x('ancestor::div[@class="thread"]/div', @).id threadID = @pathname.split('/').pop() or $.x('ancestor::div[@class="thread"]/div', @).id
if req = g.requests[threadID] $.cache @href, (-> quoteInline.parse @, id, threadID, inline)
if req.readyState is 4
quoteInline.parse req, id, threadID, inline
else
#FIXME need an array of callbacks
g.requests[threadID] = $.get @href, (-> quoteInline.parse @, id, threadID, inline)
$.addClass @, 'inlined' $.addClass @, 'inlined'
parse: (req, id, threadID, inline) -> parse: (req, id, threadID, inline) ->
if req.status isnt 200 if req.status isnt 200
@ -1488,11 +1487,7 @@ quotePreview =
else else
qp.innerHTML = "Loading #{id}..." qp.innerHTML = "Loading #{id}..."
threadID = @pathname.split('/').pop() threadID = @pathname.split('/').pop()
if req = g.requests[threadID] $.cache @href, (-> quotePreview.parse @, id, threadID)
if req.readyState is 4
quotePreview.parse req, id, threadID
else
g.requests[threadID] = $.get @href, (-> quotePreview.parse @, id, threadID)
ui.el = qp ui.el = qp
ui.winHeight = d.body.clientHeight ui.winHeight = d.body.clientHeight
$.show qp $.show qp
@ -1784,7 +1779,6 @@ imgExpand =
#main #main
NAMESPACE = 'AEOS.4chan_x.' NAMESPACE = 'AEOS.4chan_x.'
g = g =
requests: {}
callbacks: [] callbacks: []
main = main =