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

View file

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