add an error callback to the pdf renderer

use page load and render timeouts of 30 seconds to detect hanging pages
This commit is contained in:
Brian Gough 2015-01-12 16:45:24 +00:00
parent dd5562f9b6
commit 7f750ae692
2 changed files with 64 additions and 8 deletions

View file

@ -19,9 +19,11 @@ define [
@navigateFn = @options.navigateFn
@spinner = new pdfSpinner
@resetState()
@errorCallback = @options.errorCallback
@pdfjs.catch (exception) =>
# console.log 'ERROR in get document', exception
@errorCallback(exception)
resetState: () ->
@page = []
@complete = []
@timeout = []
@pageLoad = []
@ -34,9 +36,8 @@ define [
pdfDocument.numPages
getPage: (pageNum) ->
# with promise caching
return @page[pageNum] if @page[pageNum]?
@page[pageNum] = @document.then (pdfDocument) ->
@document.then (pdfDocument) ->
# console.log 'got pdf document, now getting Page', pageNum
pdfDocument.getPage(pageNum)
getPdfViewport: (pageNum, scale) ->
@ -44,6 +45,8 @@ define [
@document.then (pdfDocument) ->
pdfDocument.getPage(pageNum).then (page) ->
viewport = page.getViewport scale
, (error) ->
console.log 'ERROR', error
getDestinations: () ->
@document.then (pdfDocument) ->
@ -57,15 +60,20 @@ define [
pdfDocument.getDestinations()
return @destinations.then (all) ->
all[dest]
, (error) ->
console.log 'ERROR', error
@document.then (pdfDocument) ->
pdfDocument.getDestination(dest)
# @document.then (pdfDocument) ->
# pdfDocument.getDestination(dest)
# , (error) ->
# console.log 'ERROR', error
getPageIndex: (ref) ->
@document.then (pdfDocument) ->
pdfDocument.getPageIndex(ref).then (idx) ->
idx
, (error) ->
console.log 'ERROR', error
getScale: () ->
@scale
@ -101,6 +109,7 @@ define [
@triggerRenderQueue()
processRenderQueue: () ->
return if @shuttingDown
return if @jobs > 0
current = @renderQueue.shift()
return unless current?
@ -118,17 +127,37 @@ define [
completeRef = @complete
renderTaskRef = @renderTask
# console.log 'started page load', pagenum
timedOut = false
timer = $timeout () =>
# console.log 'page load timed out', pagenum
timedOut = true
@spinner.stop(element.canvas)
# @jobs = @jobs - 1
# @triggerRenderQueue(0)
this.errorCallback?('timeout')
, 30*1000
@pageLoad[pagenum] = @getPage(pagenum)
@pageLoad[pagenum].then (pageObject) =>
# console.log 'in page load success', pagenum
$timeout.cancel(timer)
@renderTask[pagenum] = @doRender element, pagenum, pageObject
@renderTask[pagenum].then () =>
# complete
# console.log 'render task success', pagenum
completeRef[pagenum] = true
@removeCompletedJob renderTaskRef, pagenum
, () =>
# console.log 'render task failed', pagenum
# rejected
@removeCompletedJob renderTaskRef, pagenum
.catch (error) ->
# console.log 'in page load error', pagenum, 'timedOut=', timedOut
$timeout.cancel(timer)
# console.log 'ERROR', error
doRender: (element, pagenum, page) ->
self = this
@ -183,17 +212,42 @@ define [
element.canvas.replaceWith(canvas)
# console.log 'staring page render', pagenum
result = page.render {
canvasContext: ctx
viewport: viewport
}
timedOut = false
timer = $timeout () ->
# console.log 'page render timed out', pagenum
timedOut = true
result.cancel()
, 30*1000
result.then () ->
# console.log 'page rendered', pagenum
$timeout.cancel(timer)
canvas.removeClass('pdfng-rendering')
page.getTextContent().then (textContent) ->
textLayer.setTextContent textContent
, (error) ->
console.log 'ERROR', error
page.getAnnotations().then (annotations) ->
annotationsLayer.setAnnotations annotations
, (error) ->
console.log 'ERROR', error
.catch (error) ->
# console.log 'page render failed', pagenum, error
$timeout.cancel(timer)
if timedOut
# console.log 'calling ERROR callback - was timeout'
self.errorCallback?('timeout')
else if error != 'cancelled'
# console.log 'calling ERROR callback'
self.errorCallback?(error)
return result

View file

@ -35,6 +35,8 @@ define [
$scope.$apply()
progressCallback: (progress) ->
$scope.$emit 'progress', progress
errorCallback: (error) ->
$scope.$emit 'pdf:error', error
})
# we will have all the main information needed to start display