From 345966effd08da3607d7993274399df813c96444 Mon Sep 17 00:00:00 2001 From: Simon Grim Date: Sun, 10 May 2015 00:32:54 +0500 Subject: [PATCH] more optimization of showPosts(), renaming of it to attachPostsToStream() and applying of it in displayHashtagPending() --- js/twister_actions.js | 28 +---------- js/twister_timeline.js | 104 ++++++++++++++++++++--------------------- 2 files changed, 53 insertions(+), 79 deletions(-) diff --git a/js/twister_actions.js b/js/twister_actions.js index 818b4bd..fc63fee 100644 --- a/js/twister_actions.js +++ b/js/twister_actions.js @@ -379,33 +379,7 @@ function processHashtag(postboard, hashtag, data) { } function displayHashtagPending(postboard) { - for( var i = 0; i < _hashtagPendingPosts.length; i++ ) { - var post = _hashtagPendingPosts[i]; - //console.log(post); - var streamPost = postToElem(post, "original"); - var timePost = post["userpost"]["time"]; - streamPost.attr("data-time",timePost); - - var streamItems = postboard.children(); - if( streamItems.length == 0) { - postboard.prepend( streamPost ); - } else { - var j = 0; - for( j = 0; j < streamItems.length; j++) { - 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); - break; - } - } - if( j == streamItems.length ) { - postboard.append( streamPost ); - } - } - } + attachPostsToStream(postboard, _hashtagPendingPosts, false); $.MAL.postboardLoaded(); _hashtagPendingPosts = []; } diff --git a/js/twister_timeline.js b/js/twister_timeline.js index ab7c127..74a7635 100644 --- a/js/twister_timeline.js +++ b/js/twister_timeline.js @@ -160,7 +160,7 @@ function processReceivedPosts(req, posts) i--; } } - showPosts(req, posts); + updateTimeline(req, posts); req.doneReportProcessing(posts.length); //if the count of recieved posts less or equals to requested then... @@ -182,67 +182,67 @@ function processReceivedPosts(req, posts) } } -function showPosts(req, posts) -{ - //console.log('showPosts:'); - //console.log(req); +function updateTimeline(req, posts) { + attachPostsToStream($.MAL.getStreamPostsParent(), posts, req.getspam); + for (var i = 0; i < posts.length; i++) { + req.reportProcessedPost(posts[i]['userpost']['n'], posts[i]['userpost']['k'], true); + } +} + +function attachPostsToStream(stream, posts, isPromoted) { + //console.log('attachPostsToStream:'); //console.log(posts); - var streamItemsParent = $.MAL.getStreamPostsParent(); - var streamItems = streamItemsParent.children(); + function byTimeInDescendingOrder(a, b) { + return (a.time > b.time) ? -1 : 1; + } - for( var i = 0; i < posts.length; i++ ) { - var post = posts[i]; - //console.log(post); - var streamPost = postToElem(post, "original", req.getspam); - var timePost = post["userpost"]["time"]; - streamPost.attr("data-time",timePost); - - // post will only be shown if appended to the stream list - var streamPostAppended = false; - - // insert the post in timeline ordered by (you guessed) time - if (streamItems.length) { - // check for duplicate twists - var streamItemsSameTime = streamItemsParent.children('[data-time='+timePost+']'); - if (streamItemsSameTime.length) { - var streamPostInnerHTML = streamPost[0].innerHTML; - for (var j = 0; j < streamItemsSameTime.length; j++) { - var streamItem = streamItemsSameTime.eq(j); - if (streamItem[0].innerHTML === streamPostInnerHTML) { - streamPostAppended = true; + var streamItems = stream.children(); + var streamPosts = []; + + for (var i = 0; i < streamItems.length; i++) { + var streamItem = streamItems.eq(i); + streamPosts.push({item: streamItem, time: parseInt(streamItem.attr('data-time'))}); + } + //streamPosts.sort(byTimeInDescendingOrder); // currently there is no reason to sort it yet + + for (var i = 0; i < posts.length; i++) { + //console.log(posts[i]); + var isAttached = false; + var intrantPost = {item: postToElem(posts[i], 'original', isPromoted), time: posts[i]['userpost']['time']}; + intrantPost.item.attr('data-time', intrantPost.time); + + if (streamPosts.length) { + // check to avoid twist duplication + var streamItems = stream.children('[data-time='+intrantPost.time+']'); + if (streamItems.length) { + for (var j = 0; j < streamItems.length; j++) { + var streamItem = streamItems.eq(j); + if (streamItem[0].innerHTML === intrantPost.item[0].innerHTML) { + isAttached = true; console.log('appending of duplicate twist prevented'); break; } } } - if (!streamPostAppended) { - var timeClosest = 0; - for (var j = 0; j < streamItems.length; j++) { - var streamItem = streamItems.eq(j); - var timeItem = parseInt(streamItem.attr("data-time")); - if (timePost > timeItem && timeItem > timeClosest) { - timeClosest = timeItem; - var streamItemClosest = streamItem; + // insert the post in timeline ordered by (you guessed) time + if (!isAttached) { + for (var j = 0; j < streamPosts.length; j++) { + if (intrantPost.time > streamPosts[j].time) { + // this post in stream is older, so post must be inserted above + intrantPost.item.insertBefore(streamPosts[j].item).show(); + streamPosts.push(intrantPost); + streamPosts.sort(byTimeInDescendingOrder); + isAttached = true; + break; } } - if (timeClosest) { - // this post in stream is older, so post must be inserted above - streamItemClosest.before(streamPost); - streamItems[streamItems.length] = streamPost[0]; - streamItems.length += 1; - streamPostAppended = true; - streamPost.show(); - } } } - if (!streamPostAppended) { - streamItemsParent.append( streamPost ); - streamItems[streamItems.length] = streamPost[0]; - streamItems.length += 1; - streamPostAppended = true; - streamPost.show(); + if (!isAttached) { + intrantPost.item.appendTo(stream).show(); + streamPosts.push(intrantPost); + streamPosts.sort(byTimeInDescendingOrder); } - req.reportProcessedPost(post["userpost"]["n"],post["userpost"]["k"], streamPostAppended); } } @@ -258,7 +258,7 @@ function requestTimelineUpdate(mode, count, timelineUsers, getspam) var req = new requestObj(timelineUsers, mode, count, getspam); if (mode === 'pending') { req.mode = 'latest'; - showPosts(req, _newPostsPending); + updateTimeline(req, _newPostsPending); _newPostsPending = []; $.MAL.reportNewPosts(_newPostsPending.length); $.MAL.postboardLoaded(); @@ -340,7 +340,7 @@ function processNewPostsConfirmation(expected, posts) } if ( sendedPostsPending.length > 0 ) { var req = new requestObj([defaultScreenName],'latest',sendedPostsPending.length,promotedPostsOnly); - showPosts(req, sendedPostsPending); + updateTimeline(req, sendedPostsPending); } if( posts.length < expected ) {