diff --git a/src/Miscellaneous/RelativeDates.coffee b/src/Miscellaneous/RelativeDates.coffee index 982d88d32..42b0cfa61 100644 --- a/src/Miscellaneous/RelativeDates.coffee +++ b/src/Miscellaneous/RelativeDates.coffee @@ -7,7 +7,7 @@ RelativeDates = Index.enabled ) @flush() - $.on d, 'visibilitychange ThreadUpdate', @flush + $.on d, 'visibilitychange PostsInserted', @flush if Conf['Relative Post Dates'] Callbacks.Post.push diff --git a/src/Monitoring/Unread.coffee b/src/Monitoring/Unread.coffee index 82125b197..4714deff8 100644 --- a/src/Monitoring/Unread.coffee +++ b/src/Monitoring/Unread.coffee @@ -60,7 +60,7 @@ Unread = Unread.readCount = 0 Unread.readCount++ for ID in @posts.keys when +ID <= Unread.lastReadPost $.one d, '4chanXInitFinished', Unread.ready - $.on d, 'ThreadUpdate', Unread.onUpdate + $.on d, 'PostsInserted', Unread.onUpdate ready: -> Unread.scroll() if Conf['Remember Last Read Post'] and Conf['Scroll to Last Read Post'] @@ -137,11 +137,11 @@ Unread = notif.close() , 7 * $.SECOND - onUpdate: (e) -> - if !e.detail[404] + onUpdate: -> + $.queueTask -> # ThreadUpdater may scroll immediately after inserting posts Unread.setLine() Unread.read() - Unread.update() + Unread.update() readSinglePost: (post) -> {ID} = post diff --git a/src/main/Main.coffee b/src/main/Main.coffee index 47eda1404..8ee5f176c 100644 --- a/src/main/Main.coffee +++ b/src/main/Main.coffee @@ -326,6 +326,9 @@ Main = if (board = $ s.board) threads = [] posts = [] + errors = [] + + Main.addPostsObserver = new MutationObserver Main.addPosts for threadRoot in $$(s.thread, board) boardObj = if (boardID = threadRoot.dataset.board) @@ -338,17 +341,9 @@ Main = threads.push thread postRoots = $$ s.postContainer, threadRoot postRoots.unshift threadRoot if Site.isOPContainerThread - for postRoot in postRoots when $(s.comment, postRoot) - try - posts.push new Post postRoot, thread, thread.board - catch err - # Skip posts that we failed to parse. - unless errors - errors = [] - errors.push - message: "Parsing of Post No.#{postRoot.id.match(/\d+/)} failed. Post will be skipped." - error: err - Main.handleErrors errors if errors + Main.parsePosts postRoots, thread, posts, errors + Main.addPostsObserver.observe threadRoot, {childList: true} + Main.handleErrors errors if errors.length if g.VIEW is 'thread' Site.parseThreadMetadata?(threads[0]) @@ -363,6 +358,33 @@ Main = Main.expectInitFinished = true $.event '4chanXInitFinished' + parsePosts: (postRoots, thread, posts, errors) -> + for postRoot in postRoots when !postRoot.dataset.fullID and $(Site.selectors.comment, postRoot) + try + posts.push new Post postRoot, thread, thread.board + catch err + # Skip posts that we failed to parse. + errors.push + message: "Parsing of Post No.#{postRoot.id.match(/\d+/)} failed. Post will be skipped." + error: err + return + + addPosts: (records) -> + threads = [] + posts = [] + errors = [] + for record in records + thread = Get.threadFromRoot record.target + n = posts.length + Main.parsePosts record.addedNodes, thread, posts, errors + if posts.length > n and thread not in threads + threads.push thread + Main.handleErrors errors if errors.length + Main.callbackNodesDB 'Post', posts, -> + for thread in threads + $.event 'PostsInserted', null, thread.nodes.root + return + callbackNodes: (klass, nodes) -> i = 0 cb = Callbacks[klass]