Index optimization: only build/load last replies of visible threads.

This commit is contained in:
Mayhem 2013-11-03 18:55:58 +01:00
parent 11371f1f40
commit 94322e3dd8
3 changed files with 49 additions and 29 deletions

View File

@ -16,8 +16,12 @@ ThreadHiding =
$.prepend @OP.nodes.root, ThreadHiding.makeButton @, 'hide' $.prepend @OP.nodes.root, ThreadHiding.makeButton @, 'hide'
onrefresh: -> onrefresh: ->
for threadID, thread of g.BOARD.threads when thread.isHidden and thread.stub for threadID, thread of g.BOARD.threads when thread.isHidden
$.prepend thread.OP.nodes.root.parentNode, thread.stub root = thread.OP.nodes.root.parentNode
if thread.stub
$.prepend root, thread.stub
else
threadRoot.nextElementSibling.hidden = true
return return
syncCatalog: -> syncCatalog: ->

View File

@ -271,26 +271,16 @@ Build =
thread: (board, data) -> thread: (board, data) ->
Build.spoilerRange[board] = data.custom_spoiler Build.spoilerRange[board] = data.custom_spoiler
if (OP = board.posts[data.no]) and parent = OP.nodes.root.parentNode if (OP = board.posts[data.no]) and root = OP.nodes.root.parentNode
root = parent
hr = parent.nextElementSibling
$.rmAll root $.rmAll root
else else
root = $.el 'div', root = $.el 'div',
className: 'thread' className: 'thread'
id: "t#{data.no}" id: "t#{data.no}"
hr = $.el 'hr'
nodes = [] nodes = [if OP then OP.nodes.root else Build.postFromObject data, board.ID]
for obj in [data].concat data.last_replies or []
nodes.push if post = board.posts[obj.no]
post.nodes.root
else
Build.postFromObject obj, board.ID
# build if necessary
if data.omitted_posts if data.omitted_posts
nodes.splice 1, 0, Build.summary board.ID, data.no, data.omitted_posts, data.omitted_images nodes.push Build.summary board.ID, data.no, data.omitted_posts, data.omitted_images
$.add root, nodes $.add root, nodes
[root, hr] root

View File

@ -158,6 +158,7 @@ Index =
try try
Index.parse JSON.parse req.response if req.status is 200 Index.parse JSON.parse req.response if req.status is 200
catch err catch err
c.error 'Index failure:', err.stack
# network error or non-JSON content for example. # network error or non-JSON content for example.
notice.setType 'error' notice.setType 'error'
notice.el.lastElementChild.textContent = 'Index refresh failed.' notice.el.lastElementChild.textContent = 'Index refresh failed.'
@ -171,7 +172,7 @@ Index =
Index.scrollToIndex() Index.scrollToIndex()
parse: (pages) -> parse: (pages) ->
Index.parseThreadList pages Index.parseThreadList pages
Index.buildAll() Index.buildThreads()
Index.sort() Index.sort()
Index.buildIndex() Index.buildIndex()
Index.buildPagelist() Index.buildPagelist()
@ -183,35 +184,59 @@ Index =
for threadID, thread of g.BOARD.threads when thread.ID not in Index.liveThreadIDs for threadID, thread of g.BOARD.threads when thread.ID not in Index.liveThreadIDs
thread.collect() thread.collect()
return return
buildAll: -> buildThreads: ->
Index.nodes = [] Index.nodes = []
threads = [] threads = []
posts = [] posts = []
for threadData in Index.liveThreadData for threadData in Index.liveThreadData
[threadRoot, hr] = Build.thread g.BOARD, threadData threadRoot = Build.thread g.BOARD, threadData
Index.nodes.push threadRoot, hr Index.nodes.push threadRoot, $.el 'hr'
if thread = g.BOARD.threads[threadData.no] if thread = g.BOARD.threads[threadData.no]
thread.setStatus 'Sticky', !!threadData.sticky thread.setStatus 'Sticky', !!threadData.sticky
thread.setStatus 'Closed', !!threadData.closed thread.setStatus 'Closed', !!threadData.closed
else else
thread = new Thread threadData.no, g.BOARD thread = new Thread threadData.no, g.BOARD
threads.push thread threads.push thread
postRoots = $$ '.thread > .postContainer', threadRoot # postRoots = $$ '.thread > .postContainer', threadRoot
for postRoot in postRoots when postRoot.id.match(/\d+/)[0] not of thread.posts # for postRoot in postRoots when postRoot.id.match(/\d+/)[0] not of thread.posts
OPRoot = $ '.opContainer', threadRoot
continue if OPRoot.id.match(/\d+/)[0] of thread.posts
try
posts.push new Post OPRoot, thread, g.BOARD
catch err
# Skip posts that we failed to parse.
Main.handleErrors
message: "Parsing of Post No.#{postRoot.id.match /\d+/} failed. Post will be skipped."
error: err
# Add the threads and <hr>s in a container to make sure all features work.
$.nodes Index.nodes
Main.callbackNodes Thread, threads
Main.callbackNodes Post, posts
buildReplies: (threadRoots) ->
posts = []
for threadRoot in threadRoots by 2
thread = Get.threadFromRoot threadRoot
i = Index.liveThreadIDs.indexOf thread.ID
continue unless lastReplies = Index.liveThreadData[i].last_replies
nodes = []
for data in lastReplies
if post = thread.posts[data.no]
nodes.push post.nodes.root
continue
nodes.push node = Build.postFromObject data, thread.board.ID
try try
posts.push new Post postRoot, thread, g.BOARD posts.push new Post node, thread, thread.board
catch err catch err
# Skip posts that we failed to parse. # Skip posts that we failed to parse.
errors = [] unless errors errors = [] unless errors
errors.push errors.push
message: "Parsing of Post No.#{postRoot.id.match /\d+/} failed. Post will be skipped." message: "Parsing of Post No.#{postRoot.id.match /\d+/} failed. Post will be skipped."
error: err error: err
Main.handleErrors errors if errors $.add threadRoot, nodes
# Add the threads and <hr>s in a container to make sure all features work. Main.handleErrors errors if errors
$.nodes Index.nodes Main.callbackNodes Post, posts
Main.callbackNodes Thread, threads
Main.callbackNodes Post, posts
sort: -> sort: ->
switch Conf['Index Sort'] switch Conf['Index Sort']
when 'bump' when 'bump'
@ -232,7 +257,7 @@ Index =
for threadID in sortedThreadIDs for threadID in sortedThreadIDs
i = Index.liveThreadIDs.indexOf(threadID) * 2 i = Index.liveThreadIDs.indexOf(threadID) * 2
Index.sortedNodes.push Index.nodes[i], Index.nodes[i + 1] Index.sortedNodes.push Index.nodes[i], Index.nodes[i + 1]
# Put the sticky threads on top of the index.g # Put the sticky threads on top of the index.
offset = 0 offset = 0
for threadRoot, i in Index.sortedNodes by 2 when Get.threadFromRoot(threadRoot).isSticky for threadRoot, i in Index.sortedNodes by 2 when Get.threadFromRoot(threadRoot).isSticky
Index.sortedNodes.splice offset++ * 2, 0, Index.sortedNodes.splice(i, 2)... Index.sortedNodes.splice offset++ * 2, 0, Index.sortedNodes.splice(i, 2)...
@ -251,5 +276,6 @@ Index =
else else
nodes = Index.sortedNodes nodes = Index.sortedNodes
$.rmAll Index.root $.rmAll Index.root
Index.buildReplies nodes
$.event 'IndexRefresh' $.event 'IndexRefresh'
$.add Index.root, nodes $.add Index.root, nodes