Skip to content

Commit 753abc3

Browse files
Merge pull request #14888 from Snuffleupagus/attachmentsCount-zero
Try to fix dispatching of "attachmentsloaded"-events, when the document contains no "regular" attachments (PR 8788, 12163 follow-up)
2 parents 7111a17 + dacea52 commit 753abc3

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

web/pdf_attachment_viewer.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import { createPromiseCapability, getFilenameFromUrl } from "pdfjs-lib";
1717
import { BaseTreeViewer } from "./base_tree_viewer.js";
18+
import { waitOnEventOrTimeout } from "./event_utils.js";
1819

1920
/**
2021
* @typedef {Object} PDFAttachmentViewerOptions
@@ -51,36 +52,33 @@ class PDFAttachmentViewer extends BaseTreeViewer {
5152
// replaced is when appending FileAttachment annotations.
5253
this._renderedCapability = createPromiseCapability();
5354
}
54-
if (this._pendingDispatchEvent) {
55-
clearTimeout(this._pendingDispatchEvent);
56-
}
57-
this._pendingDispatchEvent = null;
55+
this._pendingDispatchEvent = false;
5856
}
5957

6058
/**
6159
* @private
6260
*/
63-
_dispatchEvent(attachmentsCount) {
61+
async _dispatchEvent(attachmentsCount) {
6462
this._renderedCapability.resolve();
6563

66-
if (this._pendingDispatchEvent) {
67-
clearTimeout(this._pendingDispatchEvent);
68-
this._pendingDispatchEvent = null;
69-
}
70-
if (attachmentsCount === 0) {
64+
if (attachmentsCount === 0 && !this._pendingDispatchEvent) {
7165
// Delay the event when no "regular" attachments exist, to allow time for
7266
// parsing of any FileAttachment annotations that may be present on the
7367
// *initially* rendered page; this reduces the likelihood of temporarily
7468
// disabling the attachmentsView when the `PDFSidebar` handles the event.
75-
this._pendingDispatchEvent = setTimeout(() => {
76-
this.eventBus.dispatch("attachmentsloaded", {
77-
source: this,
78-
attachmentsCount: 0,
79-
});
80-
this._pendingDispatchEvent = null;
69+
this._pendingDispatchEvent = true;
70+
71+
await waitOnEventOrTimeout({
72+
target: this.eventBus,
73+
name: "annotationlayerrendered",
74+
delay: 1000,
8175
});
82-
return;
76+
77+
if (!this._pendingDispatchEvent) {
78+
return; // There was already another `_dispatchEvent`-call`.
79+
}
8380
}
81+
this._pendingDispatchEvent = false;
8482

8583
this.eventBus.dispatch("attachmentsloaded", {
8684
source: this,

0 commit comments

Comments
 (0)