Browse Source

more optimization of showPosts(), renaming of it to attachPostsToStream() and applying of it in displayHashtagPending()

master
Simon Grim 10 years ago
parent
commit
345966effd
  1. 28
      js/twister_actions.js
  2. 104
      js/twister_timeline.js

28
js/twister_actions.js

@ -379,33 +379,7 @@ function processHashtag(postboard, hashtag, data) {
} }
function displayHashtagPending(postboard) { function displayHashtagPending(postboard) {
for( var i = 0; i < _hashtagPendingPosts.length; i++ ) { attachPostsToStream(postboard, _hashtagPendingPosts, false);
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 );
}
}
}
$.MAL.postboardLoaded(); $.MAL.postboardLoaded();
_hashtagPendingPosts = []; _hashtagPendingPosts = [];
} }

104
js/twister_timeline.js

@ -160,7 +160,7 @@ function processReceivedPosts(req, posts)
i--; i--;
} }
} }
showPosts(req, posts); updateTimeline(req, posts);
req.doneReportProcessing(posts.length); req.doneReportProcessing(posts.length);
//if the count of recieved posts less or equals to requested then... //if the count of recieved posts less or equals to requested then...
@ -182,67 +182,67 @@ function processReceivedPosts(req, posts)
} }
} }
function showPosts(req, posts) function updateTimeline(req, posts) {
{ attachPostsToStream($.MAL.getStreamPostsParent(), posts, req.getspam);
//console.log('showPosts:'); for (var i = 0; i < posts.length; i++) {
//console.log(req); req.reportProcessedPost(posts[i]['userpost']['n'], posts[i]['userpost']['k'], true);
}
}
function attachPostsToStream(stream, posts, isPromoted) {
//console.log('attachPostsToStream:');
//console.log(posts); //console.log(posts);
var streamItemsParent = $.MAL.getStreamPostsParent(); function byTimeInDescendingOrder(a, b) {
var streamItems = streamItemsParent.children(); return (a.time > b.time) ? -1 : 1;
}
for( var i = 0; i < posts.length; i++ ) { var streamItems = stream.children();
var post = posts[i]; var streamPosts = [];
//console.log(post);
var streamPost = postToElem(post, "original", req.getspam); for (var i = 0; i < streamItems.length; i++) {
var timePost = post["userpost"]["time"]; var streamItem = streamItems.eq(i);
streamPost.attr("data-time",timePost); streamPosts.push({item: streamItem, time: parseInt(streamItem.attr('data-time'))});
}
// post will only be shown if appended to the stream list //streamPosts.sort(byTimeInDescendingOrder); // currently there is no reason to sort it yet
var streamPostAppended = false;
for (var i = 0; i < posts.length; i++) {
// insert the post in timeline ordered by (you guessed) time //console.log(posts[i]);
if (streamItems.length) { var isAttached = false;
// check for duplicate twists var intrantPost = {item: postToElem(posts[i], 'original', isPromoted), time: posts[i]['userpost']['time']};
var streamItemsSameTime = streamItemsParent.children('[data-time='+timePost+']'); intrantPost.item.attr('data-time', intrantPost.time);
if (streamItemsSameTime.length) {
var streamPostInnerHTML = streamPost[0].innerHTML; if (streamPosts.length) {
for (var j = 0; j < streamItemsSameTime.length; j++) { // check to avoid twist duplication
var streamItem = streamItemsSameTime.eq(j); var streamItems = stream.children('[data-time='+intrantPost.time+']');
if (streamItem[0].innerHTML === streamPostInnerHTML) { if (streamItems.length) {
streamPostAppended = true; 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'); console.log('appending of duplicate twist prevented');
break; break;
} }
} }
} }
if (!streamPostAppended) { // insert the post in timeline ordered by (you guessed) time
var timeClosest = 0; if (!isAttached) {
for (var j = 0; j < streamItems.length; j++) { for (var j = 0; j < streamPosts.length; j++) {
var streamItem = streamItems.eq(j); if (intrantPost.time > streamPosts[j].time) {
var timeItem = parseInt(streamItem.attr("data-time")); // this post in stream is older, so post must be inserted above
if (timePost > timeItem && timeItem > timeClosest) { intrantPost.item.insertBefore(streamPosts[j].item).show();
timeClosest = timeItem; streamPosts.push(intrantPost);
var streamItemClosest = streamItem; 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) { if (!isAttached) {
streamItemsParent.append( streamPost ); intrantPost.item.appendTo(stream).show();
streamItems[streamItems.length] = streamPost[0]; streamPosts.push(intrantPost);
streamItems.length += 1; streamPosts.sort(byTimeInDescendingOrder);
streamPostAppended = true;
streamPost.show();
} }
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); var req = new requestObj(timelineUsers, mode, count, getspam);
if (mode === 'pending') { if (mode === 'pending') {
req.mode = 'latest'; req.mode = 'latest';
showPosts(req, _newPostsPending); updateTimeline(req, _newPostsPending);
_newPostsPending = []; _newPostsPending = [];
$.MAL.reportNewPosts(_newPostsPending.length); $.MAL.reportNewPosts(_newPostsPending.length);
$.MAL.postboardLoaded(); $.MAL.postboardLoaded();
@ -340,7 +340,7 @@ function processNewPostsConfirmation(expected, posts)
} }
if ( sendedPostsPending.length > 0 ) { if ( sendedPostsPending.length > 0 ) {
var req = new requestObj([defaultScreenName],'latest',sendedPostsPending.length,promotedPostsOnly); var req = new requestObj([defaultScreenName],'latest',sendedPostsPending.length,promotedPostsOnly);
showPosts(req, sendedPostsPending); updateTimeline(req, sendedPostsPending);
} }
if( posts.length < expected ) { if( posts.length < expected ) {

Loading…
Cancel
Save