ThreadWatcher: Move parsing code out of anonymous function.

This commit is contained in:
ccd0 2015-01-10 22:24:26 -08:00
parent c6fcbf5de1
commit f4ee994bf7

View File

@ -159,7 +159,8 @@ ThreadWatcher =
for thread in threads for thread in threads
ThreadWatcher.fetchStatus thread ThreadWatcher.fetchStatus thread
return return
fetchStatus: ({boardID, threadID, data}) -> fetchStatus: (thread) ->
{boardID, threadID, data} = thread
return if data.isDead and !Conf['Show Unread Count'] return if data.isDead and !Conf['Show Unread Count']
{fetchCount} = ThreadWatcher {fetchCount} = ThreadWatcher
if fetchCount.fetching is 0 if fetchCount.fetching is 0
@ -168,61 +169,64 @@ ThreadWatcher =
fetchCount.fetching++ fetchCount.fetching++
$.ajax "//a.4cdn.org/#{boardID}/thread/#{threadID}.json", $.ajax "//a.4cdn.org/#{boardID}/thread/#{threadID}.json",
onloadend: -> onloadend: ->
fetchCount.fetched++ ThreadWatcher.parseStatus.call @, thread
if fetchCount.fetched is fetchCount.fetching parseStatus: ({boardID, threadID, data}) ->
fetchCount.fetched = 0 {fetchCount} = ThreadWatcher
fetchCount.fetching = 0 fetchCount.fetched++
status = '' if fetchCount.fetched is fetchCount.fetching
$.rmClass ThreadWatcher.refreshButton, 'fa-spin' fetchCount.fetched = 0
else fetchCount.fetching = 0
status = "#{Math.round fetchCount.fetched / fetchCount.fetching * 100}%" status = ''
ThreadWatcher.status.textContent = status $.rmClass ThreadWatcher.refreshButton, 'fa-spin'
else
status = "#{Math.round fetchCount.fetched / fetchCount.fetching * 100}%"
ThreadWatcher.status.textContent = status
if @status is 200 and @response if @status is 200 and @response
isDead = !!@response.posts[0].archived isDead = !!@response.posts[0].archived
if isDead and Conf['Auto Prune'] if isDead and Conf['Auto Prune']
ThreadWatcher.db.delete {boardID, threadID} ThreadWatcher.db.delete {boardID, threadID}
ThreadWatcher.refresh() ThreadWatcher.refresh()
return return
lastReadPost = ThreadWatcher.unreaddb.get lastReadPost = ThreadWatcher.unreaddb.get
boardID: boardID boardID: boardID
threadID: threadID threadID: threadID
defaultValue: 0 defaultValue: 0
unread = quotingYou = 0 unread = quotingYou = 0
for postObj in @response.posts for postObj in @response.posts
continue unless postObj.no > lastReadPost continue unless postObj.no > lastReadPost
continue if QR.db?.get {boardID, threadID, postID: postObj.no} continue if QR.db?.get {boardID, threadID, postID: postObj.no}
unread++ unread++
continue unless QR.db and postObj.com continue unless QR.db and postObj.com
regexp = /<a [^>]*\bhref="(?:\/([^\/]+)\/thread\/(\d+))?(?:#p(\d+))?"/g regexp = /<a [^>]*\bhref="(?:\/([^\/]+)\/thread\/(\d+))?(?:#p(\d+))?"/g
while match = regexp.exec postObj.com while match = regexp.exec postObj.com
if QR.db.get { if QR.db.get {
boardID: match[1] or boardID boardID: match[1] or boardID
threadID: match[2] or threadID threadID: match[2] or threadID
postID: match[3] or match[2] or threadID postID: match[3] or match[2] or threadID
} }
quotingYou++ quotingYou++
continue continue
if isDead isnt data.isDead or unread isnt data.unread or quotingYou isnt data.quotingYou if isDead isnt data.isDead or unread isnt data.unread or quotingYou isnt data.quotingYou
data.isDead = isDead data.isDead = isDead
data.unread = unread data.unread = unread
data.quotingYou = quotingYou data.quotingYou = quotingYou
ThreadWatcher.db.set {boardID, threadID, val: data} ThreadWatcher.db.set {boardID, threadID, val: data}
ThreadWatcher.refresh() ThreadWatcher.refresh()
else if @status is 404 else if @status is 404
if Conf['Auto Prune'] if Conf['Auto Prune']
ThreadWatcher.db.delete {boardID, threadID} ThreadWatcher.db.delete {boardID, threadID}
else else
data.isDead = true data.isDead = true
delete data.unread delete data.unread
delete data.quotingYou delete data.quotingYou
ThreadWatcher.db.set {boardID, threadID, val: data} ThreadWatcher.db.set {boardID, threadID, val: data}
ThreadWatcher.refresh() ThreadWatcher.refresh()
getAll: -> getAll: ->
all = [] all = []