Clean up index notification code and handle aborting previous request better.

This commit is contained in:
ccd0 2019-03-24 02:41:56 -07:00
parent d8e59f724d
commit e5ab4ffb5b

View File

@ -573,21 +573,18 @@ Index =
"#{hiddenCount} hidden threads" "#{hiddenCount} hidden threads"
update: (firstTime) -> update: (firstTime) ->
Index.req?.aborted = true if (oldReq = Index.req)
Index.req?.abort() delete Index.req
Index.notice?.close() oldReq.abort()
if Conf['Index Refresh Notifications'] and d.readyState isnt 'loading' if Conf['Index Refresh Notifications']
# Optional notification for manual refreshes # Optional notification for manual refreshes
Index.notice = new Notice 'info', 'Refreshing index...' Index.notice or= new Notice 'info', 'Refreshing index...'
else else
# Also display notice if Index Refresh is taking too long # Also display notice if Index Refresh is taking too long
now = Date.now() Index.nTimeout or= setTimeout ->
$.ready -> Index.notice or= new Notice 'info', 'Refreshing index...'
Index.nTimeout = setTimeout (-> , 3 * $.SECOND
if Index.req and !Index.notice
Index.notice = new Notice 'info', 'Refreshing index...'
), 3 * $.SECOND - (Date.now() - now)
# Hard refresh in case of incomplete page load. # Hard refresh in case of incomplete page load.
if not firstTime and d.readyState isnt 'loading' and not $('.board + *') if not firstTime and d.readyState isnt 'loading' and not $('.board + *')
@ -602,19 +599,17 @@ Index =
$.addClass Index.button, 'fa-spin' $.addClass Index.button, 'fa-spin'
load: -> load: ->
return if @ isnt Index.req # aborted
$.rmClass Index.button, 'fa-spin' $.rmClass Index.button, 'fa-spin'
{req, notice, nTimeout} = Index {notice, nTimeout} = Index
clearTimeout nTimeout if nTimeout clearTimeout nTimeout if nTimeout
delete Index.nTimeout delete Index.nTimeout
delete Index.req delete Index.req
delete Index.notice delete Index.notice
if req.aborted if @status not in [200, 304]
notice?.close() err = "Index refresh failed. #{if @status then "Error #{@statusText} (#{@status})" else 'Connection Error'}"
return
if req.status not in [200, 304]
err = "Index refresh failed. #{if req.status then "Error #{req.statusText} (#{req.status})" else 'Connection Error'}"
if notice if notice
notice.setType 'warning' notice.setType 'warning'
notice.el.lastElementChild.textContent = err notice.el.lastElementChild.textContent = err
@ -624,13 +619,12 @@ Index =
return return
try try
if req.status is 200 if @status is 200
Index.parse req.response Index.parse @response
else if req.status is 304 else if @status is 304
Index.pageLoad() Index.pageLoad()
catch err catch err
c.error "Index failure: #{err.message}", err.stack c.error "Index failure: #{err.message}", err.stack
# network error or non-JSON content for example.
if notice if notice
notice.setType 'error' notice.setType 'error'
notice.el.lastElementChild.textContent = 'Index refresh failed.' notice.el.lastElementChild.textContent = 'Index refresh failed.'
@ -648,7 +642,7 @@ Index =
notice.close() notice.close()
timeEl = $ '#index-last-refresh time', Index.navLinks timeEl = $ '#index-last-refresh time', Index.navLinks
timeEl.dataset.utc = Date.parse req.getResponseHeader 'Last-Modified' timeEl.dataset.utc = Date.parse @getResponseHeader 'Last-Modified'
RelativeDates.update timeEl RelativeDates.update timeEl
parse: (pages) -> parse: (pages) ->