Skip to content

Commit

Permalink
Merge pull request #14888 from Snuffleupagus/attachmentsCount-zero
Browse files Browse the repository at this point in the history
Try to fix dispatching of "attachmentsloaded"-events, when the document contains no "regular" attachments (PR 8788, 12163 follow-up)
  • Loading branch information
timvandermeij authored May 8, 2022
2 parents 7111a17 + dacea52 commit 753abc3
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions web/pdf_attachment_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import { createPromiseCapability, getFilenameFromUrl } from "pdfjs-lib";
import { BaseTreeViewer } from "./base_tree_viewer.js";
import { waitOnEventOrTimeout } from "./event_utils.js";

/**
* @typedef {Object} PDFAttachmentViewerOptions
Expand Down Expand Up @@ -51,36 +52,33 @@ class PDFAttachmentViewer extends BaseTreeViewer {
// replaced is when appending FileAttachment annotations.
this._renderedCapability = createPromiseCapability();
}
if (this._pendingDispatchEvent) {
clearTimeout(this._pendingDispatchEvent);
}
this._pendingDispatchEvent = null;
this._pendingDispatchEvent = false;
}

/**
* @private
*/
_dispatchEvent(attachmentsCount) {
async _dispatchEvent(attachmentsCount) {
this._renderedCapability.resolve();

if (this._pendingDispatchEvent) {
clearTimeout(this._pendingDispatchEvent);
this._pendingDispatchEvent = null;
}
if (attachmentsCount === 0) {
if (attachmentsCount === 0 && !this._pendingDispatchEvent) {
// Delay the event when no "regular" attachments exist, to allow time for
// parsing of any FileAttachment annotations that may be present on the
// *initially* rendered page; this reduces the likelihood of temporarily
// disabling the attachmentsView when the `PDFSidebar` handles the event.
this._pendingDispatchEvent = setTimeout(() => {
this.eventBus.dispatch("attachmentsloaded", {
source: this,
attachmentsCount: 0,
});
this._pendingDispatchEvent = null;
this._pendingDispatchEvent = true;

await waitOnEventOrTimeout({
target: this.eventBus,
name: "annotationlayerrendered",
delay: 1000,
});
return;

if (!this._pendingDispatchEvent) {
return; // There was already another `_dispatchEvent`-call`.
}
}
this._pendingDispatchEvent = false;

this.eventBus.dispatch("attachmentsloaded", {
source: this,
Expand Down

0 comments on commit 753abc3

Please sign in to comment.