|
15 | 15 |
|
16 | 16 | import { createPromiseCapability, getFilenameFromUrl } from "pdfjs-lib";
|
17 | 17 | import { BaseTreeViewer } from "./base_tree_viewer.js";
|
| 18 | +import { waitOnEventOrTimeout } from "./event_utils.js"; |
18 | 19 |
|
19 | 20 | /**
|
20 | 21 | * @typedef {Object} PDFAttachmentViewerOptions
|
@@ -51,36 +52,33 @@ class PDFAttachmentViewer extends BaseTreeViewer {
|
51 | 52 | // replaced is when appending FileAttachment annotations.
|
52 | 53 | this._renderedCapability = createPromiseCapability();
|
53 | 54 | }
|
54 |
| - if (this._pendingDispatchEvent) { |
55 |
| - clearTimeout(this._pendingDispatchEvent); |
56 |
| - } |
57 |
| - this._pendingDispatchEvent = null; |
| 55 | + this._pendingDispatchEvent = false; |
58 | 56 | }
|
59 | 57 |
|
60 | 58 | /**
|
61 | 59 | * @private
|
62 | 60 | */
|
63 |
| - _dispatchEvent(attachmentsCount) { |
| 61 | + async _dispatchEvent(attachmentsCount) { |
64 | 62 | this._renderedCapability.resolve();
|
65 | 63 |
|
66 |
| - if (this._pendingDispatchEvent) { |
67 |
| - clearTimeout(this._pendingDispatchEvent); |
68 |
| - this._pendingDispatchEvent = null; |
69 |
| - } |
70 |
| - if (attachmentsCount === 0) { |
| 64 | + if (attachmentsCount === 0 && !this._pendingDispatchEvent) { |
71 | 65 | // Delay the event when no "regular" attachments exist, to allow time for
|
72 | 66 | // parsing of any FileAttachment annotations that may be present on the
|
73 | 67 | // *initially* rendered page; this reduces the likelihood of temporarily
|
74 | 68 | // 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, |
81 | 75 | });
|
82 |
| - return; |
| 76 | + |
| 77 | + if (!this._pendingDispatchEvent) { |
| 78 | + return; // There was already another `_dispatchEvent`-call`. |
| 79 | + } |
83 | 80 | }
|
| 81 | + this._pendingDispatchEvent = false; |
84 | 82 |
|
85 | 83 | this.eventBus.dispatch("attachmentsloaded", {
|
86 | 84 | source: this,
|
|
0 commit comments