From 8c81f0a9ff9eea56dfbd018a1e155676dbd03827 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 30 Jun 2016 08:43:38 +0100 Subject: [PATCH 01/14] prerender offscreen pages --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 669cfeced1..36b58f9e54 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -258,7 +258,7 @@ define [ renderVisiblePages = () -> pages = getVisiblePages() - # pages = getExtraPages visiblePages + pages = getExtraPages visiblePages scope.document.renderPages(pages) getVisiblePages = () -> From 83ea771aef7f009971cdd5bd4c6cf329e99b0e1f Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 1 Jul 2016 16:31:17 +0100 Subject: [PATCH 02/14] include start method for spinner --- .../public/coffee/ide/pdfng/directives/pdfSpinner.coffee | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfSpinner.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfSpinner.coffee index da764995ad..c17ebe6b32 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfSpinner.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfSpinner.coffee @@ -8,14 +8,15 @@ define [ constructor: () -> # handler for spinners - add: (element) -> - h = element.height() - w = element.width() + add: (element, options) -> size = 64 - spinner = $('
') + spinner = $('
') spinner.css({'font-size' : size + 'px'}) element.append(spinner) + start: (element) -> + element.find('.fa-spinner').addClass('fa-spin') + stop: (element) -> element.find('.fa-spinner').removeClass('fa-spin') From c657a02db5247a3c3fb15a566d012324b1c3d827 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 1 Jul 2016 16:32:03 +0100 Subject: [PATCH 03/14] render offscreen pages --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 36b58f9e54..6b6aaff2d5 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -257,7 +257,7 @@ define [ # console.log 'layoutReady was resolved' renderVisiblePages = () -> - pages = getVisiblePages() + visiblePages = getVisiblePages() pages = getExtraPages visiblePages scope.document.renderPages(pages) From 0e550ac012ec2d59a507c9dcce3891028034a5d2 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 1 Jul 2016 16:33:54 +0100 Subject: [PATCH 04/14] add spinner for loading, initially static --- .../web/public/coffee/ide/pdfng/directives/pdfPage.coffee | 2 +- .../public/coffee/ide/pdfng/directives/pdfRenderer.coffee | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee index d52ef60256..c55af7b42a 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee @@ -3,7 +3,7 @@ define [ ], (App) -> # App = angular.module 'pdfPage', ['pdfHighlights'] - App.directive 'pdfPage', ['$timeout', 'pdfHighlights', ($timeout, pdfHighlights) -> + App.directive 'pdfPage', ['$timeout', 'pdfHighlights', 'pdfSpinner', ($timeout, pdfHighlights, pdfSpinner) -> { require: '^pdfViewer', template: ''' diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index e634afea3a..f40e804711 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -126,7 +126,10 @@ define [ element.canvas.addClass('pdfng-loading') spinTimer = setTimeout () => - @spinner.add(element.canvas) + @spinner.add(element.canvas, {static:true}) + spinTimer = setTimeout () => + @spinner.start(element.canvas) + , 100 , 100 completeRef = @complete From b8f677ac7cbdfd60dbae4301ff27ce9f85e43c1c Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 1 Jul 2016 16:34:28 +0100 Subject: [PATCH 05/14] replace canvas with rendered page after 1 second --- .../public/coffee/ide/pdfng/directives/pdfRenderer.coffee | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index f40e804711..3c61893749 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -224,7 +224,10 @@ define [ navigateFn: @navigateFn }) - element.canvas.replaceWith(canvas) + renderTimer = setTimeout () -> + renderTimer = null + element.canvas.replaceWith(canvas) + , 1000 # console.log 'staring page render', pagenum @@ -245,6 +248,9 @@ define [ result.then () -> # console.log 'page rendered', pagenum + if renderTimer? + element.canvas.replaceWith(canvas) + clearTimeout renderTimer $timeout.cancel(timer) canvas.removeClass('pdfng-rendering') page.getTextContent().then (textContent) -> From eaca616dd37aa7b8a211ef64a84171fa5ea2c7f9 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 1 Jul 2016 17:00:06 +0100 Subject: [PATCH 06/14] use progress bar for initial loading --- .../web/public/coffee/ide/pdfng/directives/pdfJs.coffee | 6 ++++-- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee index 310a9ba7f8..65e6b3d88c 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee @@ -82,6 +82,7 @@ define [ scope.$watch "pdfSrc", (url) -> if url scope.loading = true + scope.loaded = false # console.log 'pdfSrc =', url initializePosition() flashControls() @@ -95,12 +96,12 @@ define [ # flashControls() scope.$on "loaded", () -> + scope.loaded = true scope.progress = 100 - scope.$apply() $timeout () -> scope.loading = false delete scope.progress - , 250 + , 500 #scope.$watch "highlights", (areas) -> # console.log 'got HIGHLIGHTS in pdfJS', areas @@ -163,6 +164,7 @@ define [ scope.$on 'progress', (event, progress) -> scope.$apply () -> + return if scope.loaded scope.progress = Math.floor(progress.loaded/progress.total*100) scope.progress = 100 if scope.progress > 100 scope.progress = 0 if scope.progress < 0 diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 6b6aaff2d5..926eea880c 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -41,7 +41,6 @@ define [ $scope.navigateTo = ref $scope.$apply() progressCallback: (progress) -> - return if onDemandLoading is true # don't show progress for on-demand page loading $scope.$emit 'progress', progress loadedCallback: () -> $scope.$emit 'loaded' @@ -65,6 +64,7 @@ define [ result.pdfViewport.width ] # console.log 'resolved q.all, page size is', result + $scope.$emit 'loaded' $scope.numPages = result.numPages .catch (error) -> $scope.$emit 'pdf:error', error From a4cd5c90c33f679b37532abc6195e40a70873533 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 5 Jul 2016 12:11:47 +0100 Subject: [PATCH 07/14] only process the render queue after we stop moving --- .../web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index 3c61893749..efba85bb9a 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -81,6 +81,8 @@ define [ @resetState() triggerRenderQueue: (interval = @JOB_QUEUE_INTERVAL) -> + if @queueTimer? + clearTimeout @queueTimer @queueTimer = setTimeout () => @queueTimer = null @processRenderQueue() From 41b0309e6eeb961f520cfc960997ccb15bda4702 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 5 Jul 2016 13:09:50 +0100 Subject: [PATCH 08/14] show progress bar when loading starts --- services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee index 65e6b3d88c..ff27810726 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee @@ -83,6 +83,7 @@ define [ if url scope.loading = true scope.loaded = false + scope.progress = 1 # console.log 'pdfSrc =', url initializePosition() flashControls() From 70a82c10ce17a8de0c396c22534aa09316c51b71 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 5 Jul 2016 13:11:11 +0100 Subject: [PATCH 09/14] cleanup pdfJs --- .../coffee/ide/pdfng/directives/pdfJs.coffee | 54 +------------------ 1 file changed, 1 insertion(+), 53 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee index ff27810726..98361b9ff7 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfJs.coffee @@ -27,15 +27,6 @@ define [ "dblClickCallback": "=" } link: (scope, element, attrs) -> - # pdfListView = new PDFListView element.find(".pdfjs-viewer")[0], - # textLayerBuilder: TextLayerBuilder - # annotationsLayerBuilder: AnnotationsLayerBuilder - # highlightsLayerBuilder: HighlightsLayerBuilder - # ondblclick: (e) -> onDoubleClick(e) - # # logLevel: PDFListView.Logger.DEBUG - # pdfListView.listView.pageWidthOffset = 20 - # pdfListView.listView.pageHeightOffset = 20 - scope.loading = false scope.pleaseJumpTo = null scope.scale = null @@ -56,8 +47,6 @@ define [ "top": +position.offset.top "left": +position.offset.left - #scope.position = pdfListView.getPdfPosition(true) - scope.$on "$destroy", () => localStorage "pdf.scale", scope.scale localStorage "pdf.position.#{attrs.key}", scope.position @@ -84,18 +73,9 @@ define [ scope.loading = true scope.loaded = false scope.progress = 1 - # console.log 'pdfSrc =', url initializePosition() flashControls() - # pdfListView - # .loadPdf(url, onProgress) - # .then () -> - # scope.$apply () -> - # scope.loading = false - # delete scope.progress - # initializePosition() - # flashControls() - + scope.$on "loaded", () -> scope.loaded = true scope.progress = 100 @@ -104,37 +84,6 @@ define [ delete scope.progress , 500 - #scope.$watch "highlights", (areas) -> - # console.log 'got HIGHLIGHTS in pdfJS', areas - - # return if !areas? - # highlights = for area in areas or [] - # { - # page: area.page - # highlight: - # left: area.h - # top: area.v - # height: area.height - # width: area.width - # } - - # if highlights.length > 0 - # first = highlights[0] - # position = { - # page: first.page - # offset: - # left: first.highlight.left - # top: first.highlight.top - 80 - # } - # console.log 'position is', position, 'in highlights' - # scope.pleaseJumpTo = position - # pdfListView.clearHighlights() - # pdfListView.setHighlights(highlights, true) - - # setTimeout () => - # pdfListView.clearHighlights() - # , 1000 - scope.fitToHeight = () -> scale = angular.copy (scope.scale) scale.scaleMode = 'scale_mode_fit_height' @@ -161,7 +110,6 @@ define [ for event in attrs.resizeOn.split(",") scope.$on event, (e) -> #console.log 'got a resize event', event, e - # scope.$on 'progress', (event, progress) -> scope.$apply () -> From ad83784a8110f5f7a8e74cdc3dacc12dee724abe Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 5 Jul 2016 14:31:55 +0100 Subject: [PATCH 10/14] remove the renderTimer only display the page when it is fully rendered --- .../coffee/ide/pdfng/directives/pdfRenderer.coffee | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index efba85bb9a..0b70ca77c0 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -226,11 +226,6 @@ define [ navigateFn: @navigateFn }) - renderTimer = setTimeout () -> - renderTimer = null - element.canvas.replaceWith(canvas) - , 1000 - # console.log 'staring page render', pagenum result = page.render { @@ -250,9 +245,7 @@ define [ result.then () -> # console.log 'page rendered', pagenum - if renderTimer? - element.canvas.replaceWith(canvas) - clearTimeout renderTimer + element.canvas.replaceWith(canvas) $timeout.cancel(timer) canvas.removeClass('pdfng-rendering') page.getTextContent().then (textContent) -> From 379e6d2f904e78f233612554cddacd3195f9507e Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 5 Jul 2016 16:09:33 +0100 Subject: [PATCH 11/14] nullify highlight timeout after clearing it --- services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee index c55af7b42a..7009c87892 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee @@ -82,7 +82,7 @@ define [ if scope.timeoutHandler $timeout.cancel(scope.timeoutHandler) highlightsLayer.clearHighlights() - scope.timeoutHandler + scope.timeoutHandler = null # console.log 'got highlight watch in pdfPage', scope.page pageHighlights = (h for h in highlights when h.page == scope.page.pageNum) From f68ab1e508891112fd3e879d9726225ac20403f5 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 11 Jul 2016 15:01:24 +0100 Subject: [PATCH 12/14] improve loading indicators --- .../ide/pdfng/directives/pdfRenderer.coffee | 95 +++++++++++++++---- 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index 0b70ca77c0..20a3534856 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -9,6 +9,8 @@ define [ JOB_QUEUE_INTERVAL: 25 PAGE_LOAD_TIMEOUT: 60*1000 PAGE_RENDER_TIMEOUT: 60*1000 + INDICATOR_DELAY1: 100 # time to delay before showing the indicator + INDICATOR_DELAY2: 250 # time until the indicator starts animating constructor: (@url, @options) -> # PDFJS.disableFontFace = true # avoids repaints, uses worker more @@ -112,27 +114,82 @@ define [ @renderQueue.push current @processRenderQueue() + getPageDetails: (page) -> + return [page.element.canvas, page.pagenum] + + # handle the loading indicators for each page + + startIndicators: () -> + # make an array of the pages in the queue + @queuedPages = [] + @queuedPages[page.pagenum] = true for page in @renderQueue + # clear any unfinished spinner timers on pages that aren't in the queue any more + for pagenum of @spinTimer when not @queuedPages[pagenum] + clearTimeout @spinTimer[pagenum] + delete @spinTimer[pagenum] + # add indicators for any new pages in the current queue + for page in @renderQueue when not @spinTimer[page.pagenum] and not @spinTimerDone[page.pagenum] + @startIndicator page + + startIndicator: (page) -> + [canvas, pagenum] = @getPageDetails page + canvas.addClass('pdfng-loading') + @spinTimer[pagenum] = setTimeout () => + for queuedPage in @renderQueue + if pagenum == queuedPage.pagenum + @spinner.add(canvas, {static:true}) + @spinTimerDone[pagenum] = true + break + delete @spinTimer[pagenum] + , @INDICATOR_DELAY1 + + updateIndicator: (page) -> + [canvas, pagenum] = @getPageDetails page + # did the spinner insert itself already? + if @spinTimerDone[pagenum] + @spinTimer[pagenum] = setTimeout () => + @spinner.start(canvas) + delete @spinTimer[pagenum] + , @INDICATOR_DELAY2 + else + # stop the existing spin timer + clearTimeout @spinTimer[pagenum] + # start a new one which will also start spinning + @spinTimer[pagenum] = setTimeout () => + @spinner.add(canvas, {static:true}) + @spinTimerDone[pagenum] = true + @spinTimer[pagenum] = setTimeout () => + @spinner.start(canvas) + delete @spinTimer[pagenum] + , @INDICATOR_DELAY2 + , @INDICATOR_DELAY1 + + clearIndicator: (page) -> + [canvas, pagenum] = @getPageDetails page + @spinner.stop(canvas) + clearTimeout @spinTimer[pagenum] + delete @spinTimer[pagenum] + @spinTimerDone[pagenum] = true + + # handle the queue of pages to be rendered + processRenderQueue: () -> return if @shuttingDown + # mark all pages in the queue as loading + @startIndicators() + # bail out if there is already a render job running return if @jobs > 0 - current = @renderQueue.shift() - return unless current? - [element, pagenum] = [current.element, current.pagenum] - # if task is underway or complete, go to the next entry in the - # render queue - # console.log 'processing renderq', pagenum, @renderTask[pagenum], @complete[pagenum] - if @pageLoad[pagenum] or @renderTask[pagenum] or @complete[pagenum] - @processRenderQueue() - return + # take the first page in the queue + page = @renderQueue.shift() + # check if it is in action already + while page? and @pageState[page.pagenum]? + page = @renderQueue.shift() + return unless page? + [element, pagenum] = [page.element, page.pagenum] @jobs = @jobs + 1 - element.canvas.addClass('pdfng-loading') - spinTimer = setTimeout () => - @spinner.add(element.canvas, {static:true}) - spinTimer = setTimeout () => - @spinner.start(element.canvas) - , 100 - , 100 + # update the spinner to make it spinning (signifies loading has begun) + @updateIndicator page completeRef = @complete renderTaskRef = @renderTask @@ -143,8 +200,7 @@ define [ 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 timedOut = true - clearTimeout(spinTimer) - @spinner.stop(element.canvas) + @clearIndicator page # @jobs = @jobs - 1 # @triggerRenderQueue(0) @errorCallback?('timeout') @@ -169,11 +225,12 @@ define [ .catch (error) -> # console.log 'in page load error', pagenum, 'timedOut=', timedOut $timeout.cancel(timer) - clearTimeout(spinTimer) + @clearIndicator page # console.log 'ERROR', error doRender: (element, pagenum, page) -> self = this + # display an error icon scale = @scale if (not scale?) From 4b1ec24bc0f77d6693277376f96b583477d8b26a Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 11 Jul 2016 15:01:58 +0100 Subject: [PATCH 13/14] cancel existing load/render tasks correctly --- .../ide/pdfng/directives/pdfRenderer.coffee | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index 20a3534856..b66d987d7d 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -35,11 +35,19 @@ define [ @errorCallback(exception) resetState: () -> - @complete = [] - @timeout = [] - @pageLoad = [] - @renderTask = [] + #console.log 'called reset state' @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 getNumPages: () -> @@ -90,9 +98,7 @@ define [ @processRenderQueue() , interval - removeCompletedJob: (taskRef, pagenum) -> - # may need to clean up deferred object here - delete taskRef[pagenum] + removeCompletedJob: (pagenum) -> @jobs = @jobs - 1 @triggerRenderQueue(0) @@ -191,12 +197,11 @@ define [ # update the spinner to make it spinning (signifies loading has begun) @updateIndicator page - completeRef = @complete - renderTaskRef = @renderTask # console.log 'started page load', pagenum timedOut = false 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 # console.log 'page load timed out', pagenum timedOut = true @@ -206,22 +211,31 @@ define [ @errorCallback?('timeout') , @PAGE_LOAD_TIMEOUT - @pageLoad[pagenum] = @getPage(pagenum) + loadTask = @getPage(pagenum) - @pageLoad[pagenum].then (pageObject) => - # console.log 'in page load success', pagenum + loadTask.cancel = () -> + @cancelled = true + + @pageState[pagenum] = pageState = { loadTask: loadTask } + + loadTask.then (pageObject) => + #console.log 'in page load success', pagenum $timeout.cancel(timer) - clearTimeout(spinTimer) - @renderTask[pagenum] = @doRender element, pagenum, pageObject - @renderTask[pagenum].then () => - # complete - # console.log 'render task success', pagenum - completeRef[pagenum] = true - @removeCompletedJob renderTaskRef, pagenum + return if loadTask.cancelled # return from cancelled page load + pageState.renderTask = @doRender element, pagenum, pageObject + pageState.renderTask.then () => + #console.log 'render task success', pagenum + @clearIndicator page + pageState.complete = true + delete pageState.renderTask + @removeCompletedJob pagenum , () => + # display an error icon # console.log 'render task failed', pagenum + pageState.complete = false + delete pageState.renderTask # rejected - @removeCompletedJob renderTaskRef, pagenum + @removeCompletedJob pagenum .catch (error) -> # console.log 'in page load error', pagenum, 'timedOut=', timedOut $timeout.cancel(timer) @@ -230,7 +244,6 @@ define [ doRender: (element, pagenum, page) -> self = this - # display an error icon scale = @scale if (not scale?) @@ -325,13 +338,12 @@ define [ return result + stop: () -> + destroy: () -> # console.log 'in pdf renderer destroy', @renderQueue @shuttingDown = true - clearTimeout @queueTimer if @queueTimer? - @renderQueue = [] - for task in @renderTask - task.cancel() if task? + @resetState() @pdfjs.then (document) -> document.cleanup() document.destroy() From b546c5eee9e66e25fa7d75d1093120e1f00efe0c Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 11 Jul 2016 15:06:02 +0100 Subject: [PATCH 14/14] cleanup, remove comments --- .../ide/pdfng/directives/pdfRenderer.coffee | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index b66d987d7d..46e6369957 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -31,11 +31,10 @@ define [ @errorCallback = @options.errorCallback @pageSizeChangeCallback = @options.pageSizeChangeCallback @pdfjs.promise.catch (exception) => - # console.log 'ERROR in get document', exception + # error getting document @errorCallback(exception) resetState: () -> - #console.log 'called reset state' @renderQueue = [] clearTimeout @queueTimer if @queueTimer? # clear any existing timers, render tasks @@ -56,7 +55,6 @@ define [ getPage: (pageNum) -> @document.then (pdfDocument) -> - # console.log 'got pdf document, now getting Page', pageNum pdfDocument.getPage(pageNum) getPdfViewport: (pageNum, scale) -> @@ -197,13 +195,10 @@ define [ # update the spinner to make it spinning (signifies loading has begun) @updateIndicator page - # console.log 'started page load', pagenum - timedOut = false - timer = $timeout () => + timer = $timeout () => # page load timed out 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 - # console.log 'page load timed out', pagenum timedOut = true @clearIndicator page # @jobs = @jobs - 1 @@ -219,35 +214,33 @@ define [ @pageState[pagenum] = pageState = { loadTask: loadTask } loadTask.then (pageObject) => - #console.log 'in page load success', pagenum + # page load success $timeout.cancel(timer) return if loadTask.cancelled # return from cancelled page load pageState.renderTask = @doRender element, pagenum, pageObject pageState.renderTask.then () => - #console.log 'render task success', pagenum + # render task success @clearIndicator page pageState.complete = true delete pageState.renderTask @removeCompletedJob pagenum , () => - # display an error icon - # console.log 'render task failed', pagenum + # render task failed + # could display an error icon pageState.complete = false delete pageState.renderTask - # rejected @removeCompletedJob pagenum .catch (error) -> - # console.log 'in page load error', pagenum, 'timedOut=', timedOut + # page load error $timeout.cancel(timer) @clearIndicator page - # console.log 'ERROR', error doRender: (element, pagenum, page) -> self = this scale = @scale if (not scale?) - # console.log 'scale is undefined, returning' + # scale is undefined, returning return canvas = $('') @@ -296,8 +289,6 @@ define [ navigateFn: @navigateFn }) - # console.log 'staring page render', pagenum - result = page.render { canvasContext: ctx viewport: viewport @@ -307,14 +298,14 @@ define [ timedOut = false timer = $timeout () => + # page render timed out Raven?.captureMessage?('pdfng page render timed out after ' + @PAGE_RENDER_TIMEOUT + 'ms (1% sample)') if Math.random() < 0.01 - # console.log 'page render timed out', pagenum timedOut = true result.cancel() , @PAGE_RENDER_TIMEOUT result.then () -> - # console.log 'page rendered', pagenum + # page render success element.canvas.replaceWith(canvas) $timeout.cancel(timer) canvas.removeClass('pdfng-rendering') @@ -327,21 +318,18 @@ define [ , (error) -> self.errorCallback?(error) .catch (error) -> - # console.log 'page render failed', pagenum, error + # page render failed $timeout.cancel(timer) if timedOut - # console.log 'calling ERROR callback - was timeout' self.errorCallback?('timeout') - else if error != 'cancelled' - # console.log 'calling ERROR callback' + else if error is 'cancelled' + return # do nothing when cancelled + else self.errorCallback?(error) return result - stop: () -> - destroy: () -> - # console.log 'in pdf renderer destroy', @renderQueue @shuttingDown = true @resetState() @pdfjs.then (document) ->