cancel existing load/render tasks correctly

This commit is contained in:
Brian Gough 2016-07-11 15:01:58 +01:00
parent f68ab1e508
commit 4b1ec24bc0

View file

@ -35,11 +35,19 @@ define [
@errorCallback(exception) @errorCallback(exception)
resetState: () -> resetState: () ->
@complete = [] #console.log 'called reset state'
@timeout = []
@pageLoad = []
@renderTask = []
@renderQueue = [] @renderQueue = []
clearTimeout @queueTimer if @queueTimer?
# clear any existing timers, render tasks
for timer in @spinTimer or []
clearTimeout timer
for page in @pageState or []
page?.loadTask?.cancel()
page?.renderTask?.cancel()
# initialise/reset the state
@pageState = []
@spinTimer = [] # timers for starting the spinners (to avoid jitter)
@spinTimerDone = [] # array of pages where the spinner has activated
@jobs = 0 @jobs = 0
getNumPages: () -> getNumPages: () ->
@ -90,9 +98,7 @@ define [
@processRenderQueue() @processRenderQueue()
, interval , interval
removeCompletedJob: (taskRef, pagenum) -> removeCompletedJob: (pagenum) ->
# may need to clean up deferred object here
delete taskRef[pagenum]
@jobs = @jobs - 1 @jobs = @jobs - 1
@triggerRenderQueue(0) @triggerRenderQueue(0)
@ -191,12 +197,11 @@ define [
# update the spinner to make it spinning (signifies loading has begun) # update the spinner to make it spinning (signifies loading has begun)
@updateIndicator page @updateIndicator page
completeRef = @complete
renderTaskRef = @renderTask
# console.log 'started page load', pagenum # console.log 'started page load', pagenum
timedOut = false timedOut = false
timer = $timeout () => timer = $timeout () =>
return if loadTask.cancelled # return from cancelled page load
Raven?.captureMessage?('pdfng page load timed out after ' + @PAGE_LOAD_TIMEOUT + 'ms (1% sample)') if Math.random() < 0.01 Raven?.captureMessage?('pdfng page load timed out after ' + @PAGE_LOAD_TIMEOUT + 'ms (1% sample)') if Math.random() < 0.01
# console.log 'page load timed out', pagenum # console.log 'page load timed out', pagenum
timedOut = true timedOut = true
@ -206,22 +211,31 @@ define [
@errorCallback?('timeout') @errorCallback?('timeout')
, @PAGE_LOAD_TIMEOUT , @PAGE_LOAD_TIMEOUT
@pageLoad[pagenum] = @getPage(pagenum) loadTask = @getPage(pagenum)
@pageLoad[pagenum].then (pageObject) => loadTask.cancel = () ->
# console.log 'in page load success', pagenum @cancelled = true
@pageState[pagenum] = pageState = { loadTask: loadTask }
loadTask.then (pageObject) =>
#console.log 'in page load success', pagenum
$timeout.cancel(timer) $timeout.cancel(timer)
clearTimeout(spinTimer) return if loadTask.cancelled # return from cancelled page load
@renderTask[pagenum] = @doRender element, pagenum, pageObject pageState.renderTask = @doRender element, pagenum, pageObject
@renderTask[pagenum].then () => pageState.renderTask.then () =>
# complete #console.log 'render task success', pagenum
# console.log 'render task success', pagenum @clearIndicator page
completeRef[pagenum] = true pageState.complete = true
@removeCompletedJob renderTaskRef, pagenum delete pageState.renderTask
@removeCompletedJob pagenum
, () => , () =>
# display an error icon
# console.log 'render task failed', pagenum # console.log 'render task failed', pagenum
pageState.complete = false
delete pageState.renderTask
# rejected # rejected
@removeCompletedJob renderTaskRef, pagenum @removeCompletedJob pagenum
.catch (error) -> .catch (error) ->
# console.log 'in page load error', pagenum, 'timedOut=', timedOut # console.log 'in page load error', pagenum, 'timedOut=', timedOut
$timeout.cancel(timer) $timeout.cancel(timer)
@ -230,7 +244,6 @@ define [
doRender: (element, pagenum, page) -> doRender: (element, pagenum, page) ->
self = this self = this
# display an error icon
scale = @scale scale = @scale
if (not scale?) if (not scale?)
@ -325,13 +338,12 @@ define [
return result return result
stop: () ->
destroy: () -> destroy: () ->
# console.log 'in pdf renderer destroy', @renderQueue # console.log 'in pdf renderer destroy', @renderQueue
@shuttingDown = true @shuttingDown = true
clearTimeout @queueTimer if @queueTimer? @resetState()
@renderQueue = []
for task in @renderTask
task.cancel() if task?
@pdfjs.then (document) -> @pdfjs.then (document) ->
document.cleanup() document.cleanup()
document.destroy() document.destroy()