Better Unread code.

Less indexOf.
This commit is contained in:
Nicolas Stepien 2013-02-22 03:43:27 +01:00
parent 938ff38293
commit 5851a1cb29
2 changed files with 39 additions and 41 deletions

View File

@ -3361,36 +3361,36 @@
}; };
}, },
allQuotelinksLinkingTo: function(post) { allQuotelinksLinkingTo: function(post) {
var ID, quote, quotedPost, quotelinks, quoterPost, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; var ID, quote, quotedPost, quotelinks, quoterPost, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _ref4;
quotelinks = []; quotelinks = [];
_ref = g.posts; _ref = g.posts;
for (ID in _ref) { for (ID in _ref) {
quoterPost = _ref[ID]; quoterPost = _ref[ID];
if (-1 !== quoterPost.quotes.indexOf(post.fullID)) { if (_ref1 = post.fullID, __indexOf.call(quoterPost.quotes, _ref1) >= 0) {
_ref1 = [quoterPost].concat(quoterPost.clones); _ref2 = [quoterPost].concat(quoterPost.clones);
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
quoterPost = _ref1[_i]; quoterPost = _ref2[_i];
quotelinks.push.apply(quotelinks, quoterPost.nodes.quotelinks); quotelinks.push.apply(quotelinks, quoterPost.nodes.quotelinks);
} }
} }
} }
if (Conf['Quote Backlinks']) { if (Conf['Quote Backlinks']) {
_ref2 = post.quotes; _ref3 = post.quotes;
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
quote = _ref2[_j]; quote = _ref3[_j];
if (!(quotedPost = g.posts[quote])) { if (!(quotedPost = g.posts[quote])) {
continue; continue;
} }
_ref3 = [quotedPost].concat(quotedPost.clones); _ref4 = [quotedPost].concat(quotedPost.clones);
for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
quotedPost = _ref3[_k]; quotedPost = _ref4[_k];
quotelinks.push.apply(quotelinks, Array.prototype.slice.call(quotedPost.nodes.backlinks)); quotelinks.push.apply(quotelinks, Array.prototype.slice.call(quotedPost.nodes.backlinks));
} }
} }
} }
return quotelinks.filter(function(quotelink) { return quotelinks.filter(function(quotelink) {
var board, postID, _ref4; var board, postID, _ref5;
_ref4 = Get.postDataFromLink(quotelink), board = _ref4.board, postID = _ref4.postID; _ref5 = Get.postDataFromLink(quotelink), board = _ref5.board, postID = _ref5.postID;
return board === post.board.ID && postID === post.ID; return board === post.board.ID && postID === post.ID;
}); });
}, },
@ -3911,7 +3911,7 @@
}); });
}, },
node: function() { node: function() {
var board, op, postID, quote, quotelinks, quotes, _i, _j, _len, _len1, _ref; var board, op, postID, quote, quotelinks, quotes, _i, _j, _len, _len1, _ref, _ref1;
if (this.isClone && this.thread === this.context.thread) { if (this.isClone && this.thread === this.context.thread) {
return; return;
} }
@ -3919,19 +3919,19 @@
return; return;
} }
quotelinks = this.nodes.quotelinks; quotelinks = this.nodes.quotelinks;
if (this.isClone && -1 < quotes.indexOf(this.fullID)) { if (this.isClone && (_ref = this.thread.fullID, __indexOf.call(quotes, _ref) >= 0)) {
for (_i = 0, _len = quotelinks.length; _i < _len; _i++) { for (_i = 0, _len = quotelinks.length; _i < _len; _i++) {
quote = quotelinks[_i]; quote = quotelinks[_i];
quote.textContent = quote.textContent.replace(QuoteOP.text, ''); quote.textContent = quote.textContent.replace(QuoteOP.text, '');
} }
} }
op = (this.isClone ? this.context : this).thread.fullID; op = (this.isClone ? this.context : this).thread.fullID;
if (!(-1 < quotes.indexOf(op))) { if (__indexOf.call(quotes, op) < 0) {
return; return;
} }
for (_j = 0, _len1 = quotelinks.length; _j < _len1; _j++) { for (_j = 0, _len1 = quotelinks.length; _j < _len1; _j++) {
quote = quotelinks[_j]; quote = quotelinks[_j];
_ref = Get.postDataFromLink(quote), board = _ref.board, postID = _ref.postID; _ref1 = Get.postDataFromLink(quote), board = _ref1.board, postID = _ref1.postID;
if (("" + board + "." + postID) === op) { if (("" + board + "." + postID) === op) {
$.add(quote, $.tn(QuoteOP.text)); $.add(quote, $.tn(QuoteOP.text));
} }
@ -4942,18 +4942,14 @@
return $.on(d, 'scroll visibilitychange', Unread.read); return $.on(d, 'scroll visibilitychange', Unread.read);
}, },
addPosts: function(newPosts) { addPosts: function(newPosts) {
var height, index, post, _i, _len; var post, _i, _len, _ref;
if (!d.hidden) {
height = doc.clientHeight;
}
for (_i = 0, _len = newPosts.length; _i < _len; _i++) { for (_i = 0, _len = newPosts.length; _i < _len; _i++) {
post = newPosts[_i]; post = newPosts[_i];
if ((index = Unread.yourPosts.indexOf(post.ID)) !== -1) { if (!((_ref = post.ID, __indexOf.call(Unread.yourPosts, _ref) >= 0) || post.isHidden)) {
Unread.yourPosts.splice(index, 1);
} else if (!post.isHidden && (d.hidden || post.nodes.root.getBoundingClientRect().bottom > height)) {
Unread.posts.push(post); Unread.posts.push(post);
} }
} }
return Unread.read();
}, },
onUpdate: function(e) { onUpdate: function(e) {
if (!e.detail[404]) { if (!e.detail[404]) {
@ -4964,8 +4960,11 @@
post: function(e) { post: function(e) {
return Unread.yourPosts.push(+e.detail.postID); return Unread.yourPosts.push(+e.detail.postID);
}, },
read: function() { read: function(e) {
var bottom, height, i, post, _i, _len, _ref; var bottom, height, i, post, _i, _len, _ref;
if (d.hidden || !Unread.posts.length) {
return;
}
height = doc.clientHeight; height = doc.clientHeight;
_ref = Unread.posts; _ref = Unread.posts;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
@ -4979,7 +4978,9 @@
return; return;
} }
Unread.posts = Unread.posts.slice(i); Unread.posts = Unread.posts.slice(i);
return Unread.update(); if (e) {
return Unread.update();
}
}, },
update: function() { update: function() {
var count; var count;
@ -5329,10 +5330,10 @@
continue; continue;
} }
ID = +ID; ID = +ID;
if (-1 === index.indexOf(ID)) { if (__indexOf.call(index, ID) < 0) {
post.kill(); post.kill();
deletedPosts.push(post); deletedPosts.push(post);
} else if (post.file && !post.file.isDead && -1 === files.indexOf(ID)) { } else if (post.file && !post.file.isDead && __indexOf.call(files, ID) < 0) {
post.kill(true); post.kill(true);
deletedFiles.push(post); deletedFiles.push(post);
} }

View File

@ -2097,7 +2097,7 @@ Get =
# if it did quote this post, # if it did quote this post,
# get all their backlinks. # get all their backlinks.
for ID, quoterPost of g.posts for ID, quoterPost of g.posts
if -1 isnt quoterPost.quotes.indexOf post.fullID if post.fullID in quoterPost.quotes
for quoterPost in [quoterPost].concat quoterPost.clones for quoterPost in [quoterPost].concat quoterPost.clones
quotelinks.push.apply quotelinks, quoterPost.nodes.quotelinks quotelinks.push.apply quotelinks, quoterPost.nodes.quotelinks
# Second: # Second:
@ -2571,13 +2571,13 @@ QuoteOP =
quotelinks = @nodes.quotelinks quotelinks = @nodes.quotelinks
# rm (OP) from cross-thread quotes. # rm (OP) from cross-thread quotes.
if @isClone and -1 < quotes.indexOf @fullID if @isClone and @thread.fullID in quotes
for quote in quotelinks for quote in quotelinks
quote.textContent = quote.textContent.replace QuoteOP.text, '' quote.textContent = quote.textContent.replace QuoteOP.text, ''
op = (if @isClone then @context else @).thread.fullID op = (if @isClone then @context else @).thread.fullID
# add (OP) to quotes quoting this context's OP. # add (OP) to quotes quoting this context's OP.
return unless -1 < quotes.indexOf op return unless op in quotes
for quote in quotelinks for quote in quotelinks
{board, postID} = Get.postDataFromLink quote {board, postID} = Get.postDataFromLink quote
if "#{board}.#{postID}" is op if "#{board}.#{postID}" is op
@ -3304,14 +3304,10 @@ Unread =
$.on d, 'scroll visibilitychange', Unread.read $.on d, 'scroll visibilitychange', Unread.read
addPosts: (newPosts) -> addPosts: (newPosts) ->
unless d.hidden
height = doc.clientHeight
for post in newPosts for post in newPosts
if (index = Unread.yourPosts.indexOf post.ID) isnt -1 unless post.ID in Unread.yourPosts or post.isHidden
Unread.yourPosts.splice index, 1
else if !post.isHidden and (d.hidden or post.nodes.root.getBoundingClientRect().bottom > height)
Unread.posts.push post Unread.posts.push post
return Unread.read()
onUpdate: (e) -> onUpdate: (e) ->
unless e.detail[404] unless e.detail[404]
@ -3321,7 +3317,8 @@ Unread =
post: (e) -> post: (e) ->
Unread.yourPosts.push +e.detail.postID Unread.yourPosts.push +e.detail.postID
read: -> read: (e) ->
return if d.hidden or !Unread.posts.length
height = doc.clientHeight height = doc.clientHeight
for post, i in Unread.posts for post, i in Unread.posts
{bottom} = post.nodes.root.getBoundingClientRect() {bottom} = post.nodes.root.getBoundingClientRect()
@ -3329,7 +3326,7 @@ Unread =
return unless i return unless i
Unread.posts = Unread.posts[i..] Unread.posts = Unread.posts[i..]
Unread.update() Unread.update() if e
update: -> update: ->
count = Unread.posts.length count = Unread.posts.length
@ -3627,10 +3624,10 @@ ThreadUpdater =
for ID, post of ThreadUpdater.thread.posts for ID, post of ThreadUpdater.thread.posts
continue if post.isDead continue if post.isDead
ID = +ID ID = +ID
if -1 is index.indexOf ID unless ID in index
post.kill() post.kill()
deletedPosts.push post deletedPosts.push post
else if post.file and !post.file.isDead and -1 is files.indexOf ID else if post.file and !post.file.isDead and ID not in files
post.kill true post.kill true
deletedFiles.push post deletedFiles.push post