From c0ba3246ce780117ef75131e973a5df2172cc801 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sun, 31 Mar 2013 00:10:05 +0100 Subject: [PATCH] Sync unread status between tabs/devices. --- src/features.coffee | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/features.coffee b/src/features.coffee index 33075054a..cdbb9a19d 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -3615,15 +3615,18 @@ Unread = Unread.hr = $.el 'hr', id: 'unread-line' Misc.clearThreads "lastReadPosts.#{g.BOARD}" + $.sync "lastReadPosts.#{g.BOARD}", @sync + + Unread.posts = [] + Unread.postsQuotingYou = [] + Thread::callbacks.push name: 'Unread' cb: @node node: -> - Unread.thread = @ - Unread.posts = [] - Unread.postsQuotingYou = [] - Unread.title = d.title + Unread.thread = @ + Unread.title = d.title posts = [] for ID, post of @posts posts.push post if post.isReply @@ -3640,6 +3643,14 @@ Unread = $.on d, 'scroll visibilitychange', Unread.read $.on d, 'visibilitychange', Unread.setLine if Conf['Unread Line'] + sync: (lastReadPosts) -> + return unless (lastReadPost = lastReadPosts?.threads?[Unread.thread]) and Unread.lastReadPost < lastReadPost + Unread.lastReadPost = lastReadPost + Unread.readArray Unread.posts + Unread.readArray Unread.postsQuotingYou + Unread.setLine() + Unread.update() + addPosts: (newPosts) -> if Conf['Quick Reply'] {yourPosts} = QR @@ -3671,6 +3682,11 @@ Unread = else Unread.addPosts e.detail.newPosts + readArray: (arr) -> + for post, i in arr + break if post.ID > Unread.lastReadPost + arr.splice 0, i + read: (e) -> return if d.hidden or !Unread.posts.length height = doc.clientHeight @@ -3681,13 +3697,11 @@ Unread = Unread.lastReadPost = Unread.posts[i - 1].ID Unread.saveLastReadPost() - Unread.posts = Unread.posts[i..] - for post, i in Unread.postsQuotingYou - break if post.ID > Unread.lastReadPost - Unread.postsQuotingYou = Unread.postsQuotingYou[i..] + Unread.posts.splice 0, i + Unread.readArray Unread.postsQuotingYou Unread.update() if e - saveLastReadPost: $.debounce $.SECOND, -> + saveLastReadPost: $.debounce 2 * $.SECOND, -> $.get "lastReadPosts.#{Unread.thread.board}", threads: {}, (item) -> lastReadPosts = item["lastReadPosts.#{Unread.thread.board}"] lastReadPosts.threads[Unread.thread] = Unread.lastReadPost