handle shutdown of pdf viewer/rendering with destroy method

This commit is contained in:
Brian Gough 2014-12-09 13:46:44 +00:00
parent b157323eb7
commit 843fa6f847
2 changed files with 41 additions and 17 deletions

View file

@ -14,7 +14,8 @@ define [
# PDFJS.disableStream # PDFJS.disableStream
# PDFJS.disableRange # PDFJS.disableRange
@scale = @options.scale || 1 @scale = @options.scale || 1
@document = $q.when(PDFJS.getDocument @url, null, null, @options.progressCallback) @pdfjs = PDFJS.getDocument @url, null, null, @options.progressCallback
@document = $q.when(@pdfjs)
@navigateFn = @options.navigateFn @navigateFn = @options.navigateFn
@spinner = new pdfSpinner @spinner = new pdfSpinner
@resetState() @resetState()
@ -23,6 +24,7 @@ define [
@page = [] @page = []
@complete = [] @complete = []
@timeout = [] @timeout = []
@pageLoad = []
@renderTask = [] @renderTask = []
@renderQueue = [] @renderQueue = []
@jobs = 0 @jobs = 0
@ -73,6 +75,7 @@ define [
pause: (element, pagenum) -> pause: (element, pagenum) ->
return if @complete[pagenum] return if @complete[pagenum]
return if @shuttingDown
@renderQueue = @renderQueue.filter (q) -> @renderQueue = @renderQueue.filter (q) ->
q.pagenum != pagenum q.pagenum != pagenum
@spinner.stop(element.canvas) @spinner.stop(element.canvas)
@ -82,13 +85,14 @@ define [
@processRenderQueue() @processRenderQueue()
, interval , interval
removeCompletedJob: (pagenum) -> removeCompletedJob: (taskRef, pagenum) ->
# may need to clean up deferred object here # may need to clean up deferred object here
delete @renderTask[pagenum] delete taskRef[pagenum]
@jobs = @jobs - 1 @jobs = @jobs - 1
@triggerRenderQueue(0) @triggerRenderQueue(0)
renderPage: (element, pagenum) -> renderPage: (element, pagenum) ->
return if @shuttingDown
current = { current = {
'element': element 'element': element
'pagenum': pagenum 'pagenum': pagenum
@ -103,7 +107,8 @@ define [
[element, pagenum] = [current.element, current.pagenum] [element, pagenum] = [current.element, current.pagenum]
# if task is underway or complete, go to the next entry in the # if task is underway or complete, go to the next entry in the
# render queue # render queue
if @renderTask[pagenum] or @complete[pagenum] # console.log 'processing renderq', pagenum, @renderTask[pagenum], @complete[pagenum]
if @pageLoad[pagenum] or @renderTask[pagenum] or @complete[pagenum]
@processRenderQueue() @processRenderQueue()
return return
@jobs = @jobs + 1 @jobs = @jobs + 1
@ -111,18 +116,19 @@ define [
element.canvas.addClass('pdfng-loading') element.canvas.addClass('pdfng-loading')
@spinner.add(element.canvas) @spinner.add(element.canvas)
pageLoad = @getPage(pagenum) completeRef = @complete
renderTaskRef = @renderTask
@renderTask[pagenum] = pageLoad.then (pageObject) =>
@doRender element, pagenum, pageObject
@pageLoad[pagenum] = @getPage(pagenum)
@pageLoad[pagenum].then (pageObject) =>
@renderTask[pagenum] = @doRender element, pagenum, pageObject
@renderTask[pagenum].then () => @renderTask[pagenum].then () =>
# complete # complete
@complete[pagenum] = true completeRef[pagenum] = true
@removeCompletedJob pagenum @removeCompletedJob renderTaskRef, pagenum
, () => , () =>
# rejected # rejected
@removeCompletedJob pagenum @removeCompletedJob renderTaskRef, pagenum
doRender: (element, pagenum, page) -> doRender: (element, pagenum, page) ->
self = this self = this
@ -177,15 +183,28 @@ define [
element.canvas.replaceWith(canvas) element.canvas.replaceWith(canvas)
return @renderTask = page.render { result = page.render {
canvasContext: ctx canvasContext: ctx
viewport: viewport viewport: viewport
} }
.then () ->
result.then () ->
canvas.removeClass('pdfng-rendering') canvas.removeClass('pdfng-rendering')
page.getTextContent().then (textContent) -> page.getTextContent().then (textContent) ->
textLayer.setTextContent textContent textLayer.setTextContent textContent
page.getAnnotations().then (annotations) -> page.getAnnotations().then (annotations) ->
annotationsLayer.setAnnotations annotations annotationsLayer.setAnnotations annotations
return result
destroy: () ->
console.log 'in pdf renderer destroy', @renderQueue
@shuttingDown = true
@renderQueue = []
for task in @renderTask
task.cancel() if task?
@pdfjs.then (document) ->
document.cleanup()
document.destroy()
] ]

View file

@ -24,6 +24,8 @@ define [
@load = () -> @load = () ->
# $scope.pages = [] # $scope.pages = []
$scope.document.destroy() if $scope.document?
$scope.document = new PDFRenderer($scope.pdfSrc, { $scope.document = new PDFRenderer($scope.pdfSrc, {
scale: 1, scale: 1,
navigateFn: (ref) -> navigateFn: (ref) ->
@ -409,6 +411,9 @@ define [
} }
ctrl.setPdfPosition(scope.pages[first.page], position) ctrl.setPdfPosition(scope.pages[first.page], position)
scope.$watch '$destroy', () ->
console.log 'handle pdfng directive destroy'
} }
] ]