Browse Source

optimization of postboard showPosts() and preventing of post duplication

master
Simon Grim 10 years ago
parent
commit
ef210732fe
  1. 56
      js/twister_timeline.js

56
js/twister_timeline.js

@ -188,6 +188,7 @@ function showPosts(req, posts)
//console.log(req); //console.log(req);
//console.log(posts); //console.log(posts);
var streamItemsParent = $.MAL.getStreamPostsParent(); var streamItemsParent = $.MAL.getStreamPostsParent();
var streamItems = streamItemsParent.children();
for( var i = 0; i < posts.length; i++ ) { for( var i = 0; i < posts.length; i++ ) {
var post = posts[i]; var post = posts[i];
@ -200,31 +201,44 @@ function showPosts(req, posts)
var streamPostAppended = false; var streamPostAppended = false;
// insert the post in timeline ordered by (you guessed) time // insert the post in timeline ordered by (you guessed) time
// FIXME: lame! searching everything everytime. please optimize! if (streamItems.length) {
var streamItems = streamItemsParent.children(); // check for duplicate twists
if( streamItems.length == 0) { var streamItemsSameTime = streamItemsParent.children('[data-time='+timePost+']');
// timeline is empty if (streamItemsSameTime.length) {
streamItemsParent.append( streamPost ); var streamPostInnerHTML = streamPost[0].innerHTML;
streamPostAppended = true; for (var j = 0; j < streamItemsSameTime.length; j++) {
} else { var streamItem = streamItemsSameTime.eq(j);
var j = 0; if (streamItem[0].innerHTML === streamPostInnerHTML) {
for( j = 0; j < streamItems.length; j++) { streamPostAppended = true;
var streamItem = streamItems.eq(j); console.log('appending of duplicate twist prevented');
var timeItem = streamItem.attr("data-time"); break;
if( timeItem == undefined || }
timePost > parseInt(timeItem) ) { }
// this post in stream is older, so post must be inserted above }
streamItem.before(streamPost); if (!streamPostAppended) {
streamPostAppended = true; for (var j = 0; j < streamItems.length; j++) {
break; var streamItem = streamItems.eq(j);
var timeItem = streamItem.attr("data-time");
if( timeItem == undefined ||
timePost > parseInt(timeItem) ) {
// this post in stream is older, so post must be inserted above
streamItem.before(streamPost);
streamItems[streamItems.length] = streamPost[0];
streamItems.length += 1;
streamPostAppended = true;
streamPost.show();
break;
}
} }
} }
} }
if (!streamPostAppended) if (!streamPostAppended) {
streamItemsParent.append( streamPost ); streamItemsParent.append( streamPost );
streamItems[streamItems.length] = streamPost[0];
streamPostAppended = true; streamItems.length += 1;
streamPost.show(); streamPostAppended = true;
streamPost.show();
}
req.reportProcessedPost(post["userpost"]["n"],post["userpost"]["k"], streamPostAppended); req.reportProcessedPost(post["userpost"]["n"],post["userpost"]["k"], streamPostAppended);
} }
} }

Loading…
Cancel
Save