From b69dad7fea0efa75679e9a2d7d8cc1eca06af080 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sat, 17 Sep 2016 16:15:41 -0700 Subject: [PATCH] Remove needless wather refreshing / index sorting, make watcher load without waiting for whole page. #685 --- src/Monitoring/ThreadWatcher.coffee | 46 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Monitoring/ThreadWatcher.coffee b/src/Monitoring/ThreadWatcher.coffee index 651b50e76..d6a3c7c52 100644 --- a/src/Monitoring/ThreadWatcher.coffee +++ b/src/Monitoring/ThreadWatcher.coffee @@ -24,6 +24,7 @@ ThreadWatcher = $.on @refreshButton, 'click', @buttonFetchAll $.on @closeButton, 'click', @toggleWatcher + $.onExists doc, 'body', @addDialog $.on d, '4chanXInitFinished', @ready switch g.VIEW @@ -69,7 +70,11 @@ ThreadWatcher = cb: @catalogNode isWatched: (thread) -> - ThreadWatcher.db?.get {boardID: thread.board.ID, threadID: thread.ID} + !!ThreadWatcher.db?.get {boardID: thread.board.ID, threadID: thread.ID} + + setToggler: (toggler, isWatched) -> + toggler.classList.toggle 'watched', isWatched + toggler.title = "#{if isWatched then 'Unwatch' else 'Watch'} Thread" node: -> return if @isReply @@ -80,6 +85,7 @@ ThreadWatcher = href: 'javascript:;' className: 'watch-thread-link' $.before $('input', @nodes.post), toggler + ThreadWatcher.setToggler toggler, ThreadWatcher.isWatched(@thread) $.on toggler, 'click', ThreadWatcher.cb.toggle catalogNode: -> @@ -92,13 +98,14 @@ ThreadWatcher = # Prevent highlighting thumbnail in Firefox. e.preventDefault() if e.button is 0 and e.altKey + addDialog: -> + return unless Main.isThisPageLegit() + ThreadWatcher.build() + $.add Header.hover, ThreadWatcher.dialog + ready: -> $.off d, '4chanXInitFinished', ThreadWatcher.ready - return unless Main.isThisPageLegit() - ThreadWatcher.refresh() - $.add d.body, ThreadWatcher.dialog - - return unless Conf['Auto Watch'] + return unless Main.isThisPageLegit() and Conf['Auto Watch'] $.get 'AutoWatch', 0, ({AutoWatch}) -> return unless thread = g.BOARD.threads[AutoWatch] ThreadWatcher.add thread @@ -140,7 +147,9 @@ ThreadWatcher = {db} = ThreadWatcher boardID = g.BOARD.ID db.forceSync() + nKilled = 0 for threadID, data of db.data.boards[boardID] when not data?.isDead and threadID not of g.BOARD.threads + nKilled++ if Conf['Auto Prune'] or not (data and typeof data is 'object') # corrupt data db.delete {boardID, threadID} else @@ -148,7 +157,7 @@ ThreadWatcher = ThreadWatcher.fetchStatus {boardID, threadID, data} data.isDead = true db.set {boardID, threadID, val: data} - ThreadWatcher.refresh() + ThreadWatcher.refresh() if nKilled onThreadRefresh: (e) -> thread = g.threads[e.detail.threadID] return unless e.detail[404] and ThreadWatcher.db.get {boardID: thread.board.ID, threadID: thread.ID} @@ -313,28 +322,29 @@ ThreadWatcher = $.add div, [x, $.tn(' '), link] div - refresh: -> + build: -> nodes = [] for {boardID, threadID, data} in ThreadWatcher.getAll() nodes.push ThreadWatcher.makeLine boardID, threadID, data - {list} = ThreadWatcher $.rmAll list $.add list, nodes + ThreadWatcher.refreshIcon() + for refresher in ThreadWatcher.menu.refreshers + refresher() + return + + refresh: -> + ThreadWatcher.build() + g.threads.forEach (thread) -> - helper = if ThreadWatcher.isWatched thread then ['addClass', 'Unwatch'] else ['rmClass', 'Watch'] + isWatched = ThreadWatcher.isWatched thread if thread.OP for post in [thread.OP, thread.OP.clones...] toggler = $ '.watch-thread-link', post.nodes.post - $[helper[0]] toggler, 'watched' - toggler.title = "#{helper[1]} Thread" - $[helper[0]] thread.catalogView.nodes.root, 'watched' if thread.catalogView - - ThreadWatcher.refreshIcon() - - for refresher in ThreadWatcher.menu.refreshers - refresher() + ThreadWatcher.setToggler toggler, isWatched + thread.catalogView.nodes.root.classList.toggle 'watched', isWatched if thread.catalogView if Conf['Pin Watched Threads'] $.event 'SortIndex', {deferred: Conf['Index Mode'] isnt 'catalog'}