Clicking on thread watcher refresh button while loading threads aborts it.

This commit is contained in:
ccd0 2015-05-24 19:35:08 -07:00
parent 1afec2e1cc
commit 502ca4fb46

View File

@ -20,7 +20,7 @@ ThreadWatcher =
$.on d, 'QRPostSuccessful', @cb.post $.on d, 'QRPostSuccessful', @cb.post
$.on sc, 'click', @toggleWatcher $.on sc, 'click', @toggleWatcher
$.on @refreshButton, 'click', @fetchAllStatus $.on @refreshButton, 'click', @buttonFetchAll
$.on @closeButton, 'click', @toggleWatcher $.on @closeButton, 'click', @toggleWatcher
$.on d, '4chanXInitFinished', @ready $.on d, '4chanXInitFinished', @ready
@ -148,9 +148,19 @@ ThreadWatcher =
# Update dead status. # Update dead status.
ThreadWatcher.add thread ThreadWatcher.add thread
fetchCount: requests: []
fetched: 0 fetched: 0
fetching: 0
clearRequests: ->
ThreadWatcher.requests = []
ThreadWatcher.fetched = 0
ThreadWatcher.status.textContent = ''
$.rmClass ThreadWatcher.refreshButton, 'fa-spin'
abort: ->
for req in ThreadWatcher.requests when req.readyState isnt 4 # DONE
req.abort()
ThreadWatcher.clearRequests()
fetchAuto: -> fetchAuto: ->
clearTimeout ThreadWatcher.timeout clearTimeout ThreadWatcher.timeout
@ -164,6 +174,12 @@ ThreadWatcher =
db.save() db.save()
ThreadWatcher.timeout = setTimeout ThreadWatcher.fetchAuto, interval ThreadWatcher.timeout = setTimeout ThreadWatcher.fetchAuto, interval
buttonFetchAll: ->
if ThreadWatcher.requests.length
ThreadWatcher.abort()
else
ThreadWatcher.fetchAllStatus()
fetchAllStatus: -> fetchAllStatus: ->
ThreadWatcher.db.forceSync() ThreadWatcher.db.forceSync()
ThreadWatcher.unreaddb.forceSync() ThreadWatcher.unreaddb.forceSync()
@ -176,26 +192,20 @@ ThreadWatcher =
fetchStatus: (thread, force) -> fetchStatus: (thread, force) ->
{boardID, threadID, data} = thread {boardID, threadID, data} = thread
return if data.isDead and not force return if data.isDead and not force
{fetchCount} = ThreadWatcher if ThreadWatcher.requests.length is 0
if fetchCount.fetching is 0
ThreadWatcher.status.textContent = '...' ThreadWatcher.status.textContent = '...'
$.addClass ThreadWatcher.refreshButton, 'fa-spin' $.addClass ThreadWatcher.refreshButton, 'fa-spin'
fetchCount.fetching++ req = $.ajax "//a.4cdn.org/#{boardID}/thread/#{threadID}.json",
$.ajax "//a.4cdn.org/#{boardID}/thread/#{threadID}.json",
onloadend: -> onloadend: ->
ThreadWatcher.parseStatus.call @, thread ThreadWatcher.parseStatus.call @, thread
ThreadWatcher.requests.push req
parseStatus: ({boardID, threadID, data}) -> parseStatus: ({boardID, threadID, data}) ->
{fetchCount} = ThreadWatcher ThreadWatcher.fetched++
fetchCount.fetched++ if ThreadWatcher.fetched is ThreadWatcher.requests.length
if fetchCount.fetched is fetchCount.fetching ThreadWatcher.clearRequests()
fetchCount.fetched = 0
fetchCount.fetching = 0
status = ''
$.rmClass ThreadWatcher.refreshButton, 'fa-spin'
else else
status = "#{Math.round fetchCount.fetched / fetchCount.fetching * 100}%" ThreadWatcher.status.textContent = "#{Math.round(ThreadWatcher.fetched / ThreadWatcher.requests.length * 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