More or less just trying to optimize this.

This commit is contained in:
Zixaphir 2014-01-16 09:56:20 -07:00
parent ef99677f14
commit 69288c7ebe
3 changed files with 122 additions and 82 deletions

View File

@ -2360,7 +2360,7 @@
}); });
}, },
scroll: $.debounce(100, function() { scroll: $.debounce(100, function() {
var nodes, nodesPerPage, pageNum; var nodes, nodesPerPage, offset, pageNum;
if (Index.req || Conf['Index Mode'] !== 'infinite' || (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) || g.VIEW === 'thread') { if (Index.req || Conf['Index Mode'] !== 'infinite' || (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) || g.VIEW === 'thread') {
return; return;
} }
@ -2371,12 +2371,13 @@
if (pageNum >= Index.pagesNum) { if (pageNum >= Index.pagesNum) {
return Index.endNotice(); return Index.endNotice();
} }
nodesPerPage = Index.threadsNumPerPage * 2; nodesPerPage = Index.threadsNumPerPage;
nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); offset = nodesPerPage * pageNum;
nodes = Index.sortedNodes.slice(offset, offset + nodesPerPage);
if (Conf['Show Replies']) { if (Conf['Show Replies']) {
Index.buildReplies(nodes); Index.buildReplies(nodes);
} }
$.add(Index.root, nodes); Index.buildStructure(nodes);
return Index.setPage(pageNum); return Index.setPage(pageNum);
}), }),
endNotice: (function() { endNotice: (function() {
@ -2655,7 +2656,7 @@
thread = new Thread(threadData.no, g.BOARD); thread = new Thread(threadData.no, g.BOARD);
threads.push(thread); threads.push(thread);
} }
Index.nodes.push(threadRoot, $.el('hr')); Index.nodes.push(threadRoot);
if (thread.ID in thread.posts) { if (thread.ID in thread.posts) {
continue; continue;
} }
@ -2682,7 +2683,7 @@
buildReplies: function(threadRoots) { buildReplies: function(threadRoots) {
var data, err, errors, i, lastReplies, node, nodes, post, posts, thread, threadRoot, _i, _j, _len, _len1; var data, err, errors, i, lastReplies, node, nodes, post, posts, thread, threadRoot, _i, _j, _len, _len1;
posts = []; posts = [];
for (_i = 0, _len = threadRoots.length; _i < _len; _i += 2) { for (_i = 0, _len = threadRoots.length; _i < _len; _i++) {
threadRoot = threadRoots[_i]; threadRoot = threadRoots[_i];
thread = Get.threadFromRoot(threadRoot); thread = Get.threadFromRoot(threadRoot);
i = Index.liveThreadIDs.indexOf(thread.ID); i = Index.liveThreadIDs.indexOf(thread.ID);
@ -2718,7 +2719,7 @@
return Main.callbackNodes(Post, posts); return Main.callbackNodes(Post, posts);
}, },
sort: function() { sort: function() {
var cnd, fn, i, item, items, sortOnTop, sortedThreadIDs, threadID, _i, _len; var cnd, fn, i, item, items, nodes, sortedThreadIDs, threadID, _i, _len;
sortedThreadIDs = { sortedThreadIDs = {
lastreply: __slice.call(Index.liveThreadData).sort(function(a, b) { lastreply: __slice.call(Index.liveThreadData).sort(function(a, b) {
if ('last_replies' in a) { if ('last_replies' in a) {
@ -2747,15 +2748,14 @@
}) })
}[Conf['Index Sort']]; }[Conf['Index Sort']];
Index.sortedNodes = []; Index.sortedNodes = [];
nodes = Index.nodes;
for (_i = 0, _len = sortedThreadIDs.length; _i < _len; _i++) { for (_i = 0, _len = sortedThreadIDs.length; _i < _len; _i++) {
threadID = sortedThreadIDs[_i]; threadID = sortedThreadIDs[_i];
i = Index.liveThreadIDs.indexOf(threadID) * 2; Index.sortedNodes.push(nodes[Index.liveThreadIDs.indexOf(threadID)]);
Index.sortedNodes.push(Index.nodes[i], Index.nodes[i + 1]);
} }
if (Index.isSearching) { if (Index.isSearching && (nodes = Index.querySearch(Index.searchInput.value))) {
Index.sortedNodes = Index.querySearch(Index.searchInput.value) || Index.sortedNodes; Index.sortedNodes = nodes;
} }
sortOnTop = Index.sortOnTop;
items = [ items = [
{ {
fn: function(thread) { fn: function(thread) {
@ -2777,28 +2777,30 @@
i = 0; i = 0;
while (item = items[i++]) { while (item = items[i++]) {
fn = item.fn, cnd = item.cnd; fn = item.fn, cnd = item.cnd;
if (fn) { if (cnd) {
sortOnTop(fn); Index.sortOnTop(fn);
} }
} }
}, },
sortOnTop: function(match) { sortOnTop: function(match) {
var i, offset, threadRoot, _i, _len, _ref, _ref1; var i, offset, threadRoot, _i, _len, _ref;
offset = 0; offset = 0;
i = 0;
_ref = Index.sortedNodes; _ref = Index.sortedNodes;
for (i = _i = 0, _len = _ref.length; _i < _len; i = _i += 2) { for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
threadRoot = _ref[i]; threadRoot = _ref[i];
if (match(Get.threadFromRoot(threadRoot))) { if (match(Get.threadFromRoot(threadRoot))) {
(_ref1 = Index.sortedNodes).splice.apply(_ref1, [offset++ * 2, 0].concat(__slice.call(Index.sortedNodes.splice(i, 2)))); Index.sortedNodes.splice(offset++, 0, Index.sortedNodes.splice(i, 1)[0]);
} }
} }
}, },
buildIndex: function() { buildIndex: function() {
var nodes, nodesPerPage, pageNum; var nodes, nodesPerPage, offset, pageNum;
if (Conf['Index Mode'] !== 'all pages') { if (Conf['Index Mode'] !== 'all pages') {
pageNum = Index.getCurrentPage(); pageNum = Index.getCurrentPage();
nodesPerPage = Index.threadsNumPerPage * 2; nodesPerPage = Index.threadsNumPerPage;
nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); offset = nodesPerPage * pageNum;
nodes = Index.sortedNodes.slice(offset, offset + nodesPerPage);
} else { } else {
nodes = Index.sortedNodes; nodes = Index.sortedNodes;
} }
@ -2807,8 +2809,22 @@
if (Conf['Show Replies']) { if (Conf['Show Replies']) {
Index.buildReplies(nodes); Index.buildReplies(nodes);
} }
$.add(Index.root, nodes); return Index.buildStructure(nodes);
return $.event('IndexBuild', nodes); },
buildStructure: function(nodes) {
var hr, i, node, result, _i, _len, _ref;
result = $.frag();
i = 0;
while (node = nodes[i++]) {
$.add(result, [node, $.el('hr')]);
}
$.add(Index.root, result);
_ref = $$('hr + hr', Index.root);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
hr = _ref[_i];
$.rm(hr);
}
return $.event('IndexBuild', result);
}, },
isSearching: false, isSearching: false,
clearSearch: function() { clearSearch: function() {
@ -2851,16 +2867,16 @@
return Index.search(keywords); return Index.search(keywords);
}, },
search: function(keywords) { search: function(keywords) {
var found, i, threadRoot, _i, _len, _ref; var threadRoot, _i, _len, _ref, _results;
found = [];
_ref = Index.sortedNodes; _ref = Index.sortedNodes;
for (i = _i = 0, _len = _ref.length; _i < _len; i = _i += 2) { _results = [];
threadRoot = _ref[i]; for (_i = 0, _len = _ref.length; _i < _len; _i++) {
threadRoot = _ref[_i];
if (Index.searchMatch(Get.threadFromRoot(threadRoot), keywords)) { if (Index.searchMatch(Get.threadFromRoot(threadRoot), keywords)) {
found.push(Index.sortedNodes[i], Index.sortedNodes[i + 1]); _results.push(threadRoot);
} }
} }
return found; return _results;
}, },
searchMatch: function(thread, keywords) { searchMatch: function(thread, keywords) {
var file, info, key, keyword, text, _i, _j, _len, _len1, _ref, _ref1; var file, info, key, keyword, text, _i, _j, _len, _len1, _ref, _ref1;

View File

@ -2370,7 +2370,7 @@
}); });
}, },
scroll: $.debounce(100, function() { scroll: $.debounce(100, function() {
var nodes, nodesPerPage, pageNum; var nodes, nodesPerPage, offset, pageNum;
if (Index.req || Conf['Index Mode'] !== 'infinite' || (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) || g.VIEW === 'thread') { if (Index.req || Conf['Index Mode'] !== 'infinite' || (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) || g.VIEW === 'thread') {
return; return;
} }
@ -2381,12 +2381,13 @@
if (pageNum >= Index.pagesNum) { if (pageNum >= Index.pagesNum) {
return Index.endNotice(); return Index.endNotice();
} }
nodesPerPage = Index.threadsNumPerPage * 2; nodesPerPage = Index.threadsNumPerPage;
nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); offset = nodesPerPage * pageNum;
nodes = Index.sortedNodes.slice(offset, offset + nodesPerPage);
if (Conf['Show Replies']) { if (Conf['Show Replies']) {
Index.buildReplies(nodes); Index.buildReplies(nodes);
} }
$.add(Index.root, nodes); Index.buildStructure(nodes);
return Index.setPage(pageNum); return Index.setPage(pageNum);
}), }),
endNotice: (function() { endNotice: (function() {
@ -2665,7 +2666,7 @@
thread = new Thread(threadData.no, g.BOARD); thread = new Thread(threadData.no, g.BOARD);
threads.push(thread); threads.push(thread);
} }
Index.nodes.push(threadRoot, $.el('hr')); Index.nodes.push(threadRoot);
if (thread.ID in thread.posts) { if (thread.ID in thread.posts) {
continue; continue;
} }
@ -2692,7 +2693,7 @@
buildReplies: function(threadRoots) { buildReplies: function(threadRoots) {
var data, err, errors, i, lastReplies, node, nodes, post, posts, thread, threadRoot, _i, _j, _len, _len1; var data, err, errors, i, lastReplies, node, nodes, post, posts, thread, threadRoot, _i, _j, _len, _len1;
posts = []; posts = [];
for (_i = 0, _len = threadRoots.length; _i < _len; _i += 2) { for (_i = 0, _len = threadRoots.length; _i < _len; _i++) {
threadRoot = threadRoots[_i]; threadRoot = threadRoots[_i];
thread = Get.threadFromRoot(threadRoot); thread = Get.threadFromRoot(threadRoot);
i = Index.liveThreadIDs.indexOf(thread.ID); i = Index.liveThreadIDs.indexOf(thread.ID);
@ -2728,7 +2729,7 @@
return Main.callbackNodes(Post, posts); return Main.callbackNodes(Post, posts);
}, },
sort: function() { sort: function() {
var cnd, fn, i, item, items, sortOnTop, sortedThreadIDs, threadID, _i, _len; var cnd, fn, i, item, items, nodes, sortedThreadIDs, threadID, _i, _len;
sortedThreadIDs = { sortedThreadIDs = {
lastreply: __slice.call(Index.liveThreadData).sort(function(a, b) { lastreply: __slice.call(Index.liveThreadData).sort(function(a, b) {
if ('last_replies' in a) { if ('last_replies' in a) {
@ -2757,15 +2758,14 @@
}) })
}[Conf['Index Sort']]; }[Conf['Index Sort']];
Index.sortedNodes = []; Index.sortedNodes = [];
nodes = Index.nodes;
for (_i = 0, _len = sortedThreadIDs.length; _i < _len; _i++) { for (_i = 0, _len = sortedThreadIDs.length; _i < _len; _i++) {
threadID = sortedThreadIDs[_i]; threadID = sortedThreadIDs[_i];
i = Index.liveThreadIDs.indexOf(threadID) * 2; Index.sortedNodes.push(nodes[Index.liveThreadIDs.indexOf(threadID)]);
Index.sortedNodes.push(Index.nodes[i], Index.nodes[i + 1]);
} }
if (Index.isSearching) { if (Index.isSearching && (nodes = Index.querySearch(Index.searchInput.value))) {
Index.sortedNodes = Index.querySearch(Index.searchInput.value) || Index.sortedNodes; Index.sortedNodes = nodes;
} }
sortOnTop = Index.sortOnTop;
items = [ items = [
{ {
fn: function(thread) { fn: function(thread) {
@ -2787,28 +2787,30 @@
i = 0; i = 0;
while (item = items[i++]) { while (item = items[i++]) {
fn = item.fn, cnd = item.cnd; fn = item.fn, cnd = item.cnd;
if (fn) { if (cnd) {
sortOnTop(fn); Index.sortOnTop(fn);
} }
} }
}, },
sortOnTop: function(match) { sortOnTop: function(match) {
var i, offset, threadRoot, _i, _len, _ref, _ref1; var i, offset, threadRoot, _i, _len, _ref;
offset = 0; offset = 0;
i = 0;
_ref = Index.sortedNodes; _ref = Index.sortedNodes;
for (i = _i = 0, _len = _ref.length; _i < _len; i = _i += 2) { for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
threadRoot = _ref[i]; threadRoot = _ref[i];
if (match(Get.threadFromRoot(threadRoot))) { if (match(Get.threadFromRoot(threadRoot))) {
(_ref1 = Index.sortedNodes).splice.apply(_ref1, [offset++ * 2, 0].concat(__slice.call(Index.sortedNodes.splice(i, 2)))); Index.sortedNodes.splice(offset++, 0, Index.sortedNodes.splice(i, 1)[0]);
} }
} }
}, },
buildIndex: function() { buildIndex: function() {
var nodes, nodesPerPage, pageNum; var nodes, nodesPerPage, offset, pageNum;
if (Conf['Index Mode'] !== 'all pages') { if (Conf['Index Mode'] !== 'all pages') {
pageNum = Index.getCurrentPage(); pageNum = Index.getCurrentPage();
nodesPerPage = Index.threadsNumPerPage * 2; nodesPerPage = Index.threadsNumPerPage;
nodes = Index.sortedNodes.slice(nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)); offset = nodesPerPage * pageNum;
nodes = Index.sortedNodes.slice(offset, offset + nodesPerPage);
} else { } else {
nodes = Index.sortedNodes; nodes = Index.sortedNodes;
} }
@ -2817,8 +2819,22 @@
if (Conf['Show Replies']) { if (Conf['Show Replies']) {
Index.buildReplies(nodes); Index.buildReplies(nodes);
} }
$.add(Index.root, nodes); return Index.buildStructure(nodes);
return $.event('IndexBuild', nodes); },
buildStructure: function(nodes) {
var hr, i, node, result, _i, _len, _ref;
result = $.frag();
i = 0;
while (node = nodes[i++]) {
$.add(result, [node, $.el('hr')]);
}
$.add(Index.root, result);
_ref = $$('hr + hr', Index.root);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
hr = _ref[_i];
$.rm(hr);
}
return $.event('IndexBuild', result);
}, },
isSearching: false, isSearching: false,
clearSearch: function() { clearSearch: function() {
@ -2861,16 +2877,16 @@
return Index.search(keywords); return Index.search(keywords);
}, },
search: function(keywords) { search: function(keywords) {
var found, i, threadRoot, _i, _len, _ref; var threadRoot, _i, _len, _ref, _results;
found = [];
_ref = Index.sortedNodes; _ref = Index.sortedNodes;
for (i = _i = 0, _len = _ref.length; _i < _len; i = _i += 2) { _results = [];
threadRoot = _ref[i]; for (_i = 0, _len = _ref.length; _i < _len; _i++) {
threadRoot = _ref[_i];
if (Index.searchMatch(Get.threadFromRoot(threadRoot), keywords)) { if (Index.searchMatch(Get.threadFromRoot(threadRoot), keywords)) {
found.push(Index.sortedNodes[i], Index.sortedNodes[i + 1]); _results.push(threadRoot);
} }
} }
return found; return _results;
}, },
searchMatch: function(thread, keywords) { searchMatch: function(thread, keywords) {
var file, info, key, keyword, text, _i, _j, _len, _len1, _ref, _ref1; var file, info, key, keyword, text, _i, _j, _len, _len1, _ref, _ref1;

View File

@ -115,12 +115,16 @@ Index =
scroll: $.debounce 100, -> scroll: $.debounce 100, ->
return if Index.req or Conf['Index Mode'] isnt 'infinite' or (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) or g.VIEW is 'thread' return if Index.req or Conf['Index Mode'] isnt 'infinite' or (doc.scrollTop <= doc.scrollHeight - (300 + window.innerHeight)) or g.VIEW is 'thread'
Index.pageNum = Index.getCurrentPage() unless Index.pageNum? # Avoid having to pushState to keep track of the current page Index.pageNum = Index.getCurrentPage() unless Index.pageNum? # Avoid having to pushState to keep track of the current page
pageNum = Index.pageNum++ pageNum = Index.pageNum++
return Index.endNotice() if pageNum >= Index.pagesNum return Index.endNotice() if pageNum >= Index.pagesNum
nodesPerPage = Index.threadsNumPerPage * 2
nodes = Index.sortedNodes[nodesPerPage * pageNum ... nodesPerPage * (pageNum + 1)] nodesPerPage = Index.threadsNumPerPage
offset = nodesPerPage * pageNum
nodes = Index.sortedNodes[offset ... offset + nodesPerPage]
Index.buildReplies nodes if Conf['Show Replies'] Index.buildReplies nodes if Conf['Show Replies']
$.add Index.root, nodes Index.buildStructure nodes
Index.setPage pageNum Index.setPage pageNum
endNotice: do -> endNotice: do ->
@ -331,7 +335,7 @@ Index =
else else
thread = new Thread threadData.no, g.BOARD thread = new Thread threadData.no, g.BOARD
threads.push thread threads.push thread
Index.nodes.push threadRoot, $.el 'hr' Index.nodes.push threadRoot
continue if thread.ID of thread.posts continue if thread.ID of thread.posts
posts.push new Post $('.opContainer', threadRoot), thread, g.BOARD posts.push new Post $('.opContainer', threadRoot), thread, g.BOARD
catch err catch err
@ -350,7 +354,7 @@ Index =
buildReplies: (threadRoots) -> buildReplies: (threadRoots) ->
posts = [] posts = []
for threadRoot in threadRoots by 2 for threadRoot in threadRoots
thread = Get.threadFromRoot threadRoot thread = Get.threadFromRoot threadRoot
i = Index.liveThreadIDs.indexOf thread.ID i = Index.liveThreadIDs.indexOf thread.ID
continue unless lastReplies = Index.liveThreadData[i].last_replies continue unless lastReplies = Index.liveThreadData[i].last_replies
@ -382,19 +386,18 @@ Index =
b.no - a.no b.no - a.no
).map (data) -> data.no ).map (data) -> data.no
bump: Index.liveThreadIDs bump: Index.liveThreadIDs
birth: [Index.liveThreadIDs...].sort (a, b) -> b - a birth: [Index.liveThreadIDs... ].sort (a, b) -> b - a
replycount: [Index.liveThreadData...].sort((a, b) -> b.replies - a.replies).map (data) -> data.no replycount: [Index.liveThreadData...].sort((a, b) -> b.replies - a.replies).map (data) -> data.no
filecount: [Index.liveThreadData...].sort((a, b) -> b.images - a.images).map (data) -> data.no filecount: [Index.liveThreadData...].sort((a, b) -> b.images - a.images ).map (data) -> data.no
}[Conf['Index Sort']] }[Conf['Index Sort']]
Index.sortedNodes = [] Index.sortedNodes = []
{nodes} = Index
for threadID in sortedThreadIDs for threadID in sortedThreadIDs
i = Index.liveThreadIDs.indexOf(threadID) * 2 Index.sortedNodes.push nodes[Index.liveThreadIDs.indexOf(threadID)]
Index.sortedNodes.push Index.nodes[i], Index.nodes[i + 1] if Index.isSearching and nodes = Index.querySearch(Index.searchInput.value)
if Index.isSearching Index.sortedNodes = nodes
Index.sortedNodes = Index.querySearch(Index.searchInput.value) or Index.sortedNodes
# Sticky threads
{sortOnTop} = Index
items = [ items = [
# Sticky threads
fn: (thread) -> thread.isSticky fn: (thread) -> thread.isSticky
cnd: true cnd: true
, # Highlighted threads , # Highlighted threads
@ -407,27 +410,37 @@ Index =
i = 0 i = 0
while item = items[i++] while item = items[i++]
{fn, cnd} = item {fn, cnd} = item
sortOnTop fn if fn Index.sortOnTop fn if cnd
return return
sortOnTop: (match) -> sortOnTop: (match) ->
offset = 0 offset = 0
for threadRoot, i in Index.sortedNodes by 2 when match Get.threadFromRoot threadRoot i = 0
Index.sortedNodes.splice offset++ * 2, 0, Index.sortedNodes.splice(i, 2)... for threadRoot, i in Index.sortedNodes
if match Get.threadFromRoot threadRoot
Index.sortedNodes.splice offset++, 0, Index.sortedNodes.splice(i, 1)[0]
return return
buildIndex: -> buildIndex: ->
if Conf['Index Mode'] isnt 'all pages' if Conf['Index Mode'] isnt 'all pages'
pageNum = Index.getCurrentPage() pageNum = Index.getCurrentPage()
nodesPerPage = Index.threadsNumPerPage * 2 nodesPerPage = Index.threadsNumPerPage
nodes = Index.sortedNodes[nodesPerPage * pageNum ... nodesPerPage * (pageNum + 1)] offset = nodesPerPage * pageNum
nodes = Index.sortedNodes[offset ... offset + nodesPerPage]
else else
nodes = Index.sortedNodes nodes = Index.sortedNodes
$.rmAll Index.root $.rmAll Index.root
$.rmAll Header.hover $.rmAll Header.hover
Index.buildReplies nodes if Conf['Show Replies'] Index.buildReplies nodes if Conf['Show Replies']
$.add Index.root, nodes Index.buildStructure nodes
$.event 'IndexBuild', nodes
buildStructure: (nodes) ->
result = $.frag()
i = 0
$.add result, [node, $.el 'hr'] while node = nodes[i++]
$.add Index.root, result
$.rm hr for hr in $$ 'hr + hr', Index.root # Temp fix until I figure out where I fucked up
$.event 'IndexBuild', result
isSearching: false isSearching: false
@ -467,12 +480,7 @@ Index =
return unless keywords = query.toLowerCase().match /\S+/g return unless keywords = query.toLowerCase().match /\S+/g
Index.search keywords Index.search keywords
search: (keywords) -> search: (keywords) -> threadRoot for threadRoot in Index.sortedNodes when Index.searchMatch Get.threadFromRoot(threadRoot), keywords
found = []
for threadRoot, i in Index.sortedNodes by 2
if Index.searchMatch Get.threadFromRoot(threadRoot), keywords
found.push Index.sortedNodes[i], Index.sortedNodes[i + 1]
found
searchMatch: (thread, keywords) -> searchMatch: (thread, keywords) ->
{info, file} = thread.OP {info, file} = thread.OP