Skip to content

Commit

Permalink
Dispatch a 'pagecancelled' event, in PDFPageView.cancelRendering, w…
Browse files Browse the repository at this point in the history
…hen rendering is cancelled

Also, the patch updates `TextLayerBuilder` to use the new 'pagecancelled' event for (future) event removal purposes.
  • Loading branch information
Snuffleupagus committed Sep 23, 2018
1 parent 54d6c24 commit 1eaa3b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ class PDFPageView {
}

cancelRendering(keepAnnotations = false) {
const renderingState = this.renderingState;

if (this.paintTask) {
this.paintTask.cancel();
this.paintTask = null;
Expand All @@ -275,6 +277,14 @@ class PDFPageView {
this.annotationLayer.cancel();
this.annotationLayer = null;
}

if (renderingState !== RenderingStates.INITIAL) {
this.eventBus.dispatch('pagecancelled', {
source: this,
pageNumber: this.id,
renderingState,
});
}
}

cssTransform(target, redrawAnnotations = false) {
Expand Down
30 changes: 30 additions & 0 deletions web/text_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class TextLayerBuilder {
this.textLayerRenderTask = null;
this.enhanceTextSelection = enhanceTextSelection;

this._boundEvents = Object.create(null);
this._bindEvents();

this._bindMouse();
}

Expand Down Expand Up @@ -331,6 +334,33 @@ class TextLayerBuilder {
this.renderMatches(this.matches);
}

/**
* @private
*/
_bindEvents() {
const { eventBus, _boundEvents, } = this;

_boundEvents.pageCancelled = (evt) => {
if (evt.pageNumber !== this.pageNumber) {
return;
}
if (this.textLayerRenderTask) {
console.error('TextLayerBuilder._bindEvents: `this.cancel()` should ' +
'have been called when the page was reset, or rendering cancelled.');
return;
}
// Ensure that all event listeners are cleaned up when the page is reset,
// since re-rendering will create new `TextLayerBuilder` instances and the
// number of (stale) event listeners would otherwise grow without bound.
for (const name in _boundEvents) {
eventBus.off(name.toLowerCase(), _boundEvents[name]);
delete _boundEvents[name];
}
};

eventBus.on('pagecancelled', _boundEvents.pageCancelled);
}

/**
* Improves text selection by adding an additional div where the mouse was
* clicked. This reduces flickering of the content if the mouse is slowly
Expand Down

0 comments on commit 1eaa3b8

Please sign in to comment.