[Scripting] Await manually triggered dispatchEventInSandbox
calls in the viewer
Given that the `dispatchEventInSandbox` method (on the scripting-classes) is asynchronous, there's a very real risk that the events won't be dispatched/handled until *after* their associated functionality has actually run (with the "Will..." events being particularily susceptible to this issue). To reduce the likelihood of that happening, we can simply `await` the `dispatchEventInSandbox` calls as necessary. A couple of methods are now marked as `async` to support these changes, however that shouldn't be a problem as far as I can tell. *Please note:* Given that the browser "beforeprint"/"afterprint" events are *synchronous*, we unfortunately cannot await the `WillPrint`/`DidPrint` event dispatching. To fix this properly the web-platform would need support for asynchronous printing, and we'll thus have to hope that things work correctly anyway.
This commit is contained in:
parent
0daf51c340
commit
a4786c9689
17
web/app.js
17
web/app.js
@ -1013,7 +1013,7 @@ const PDFViewerApplication = {
|
|||||||
.catch(downloadByUrl); // Error occurred, try downloading with the URL.
|
.catch(downloadByUrl); // Error occurred, try downloading with the URL.
|
||||||
},
|
},
|
||||||
|
|
||||||
save({ sourceEventType = "download" } = {}) {
|
async save({ sourceEventType = "download" } = {}) {
|
||||||
if (this._saveInProgress) {
|
if (this._saveInProgress) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1036,12 +1036,13 @@ const PDFViewerApplication = {
|
|||||||
this.download({ sourceEventType });
|
this.download({ sourceEventType });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
this._saveInProgress = true;
|
||||||
|
|
||||||
|
await this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
||||||
id: "doc",
|
id: "doc",
|
||||||
name: "WillSave",
|
name: "WillSave",
|
||||||
});
|
});
|
||||||
|
|
||||||
this._saveInProgress = true;
|
|
||||||
this.pdfDocument
|
this.pdfDocument
|
||||||
.saveDocument(this.pdfDocument.annotationStorage)
|
.saveDocument(this.pdfDocument.annotationStorage)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
@ -1051,8 +1052,8 @@ const PDFViewerApplication = {
|
|||||||
.catch(() => {
|
.catch(() => {
|
||||||
this.download({ sourceEventType });
|
this.download({ sourceEventType });
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(async () => {
|
||||||
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
await this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
||||||
id: "doc",
|
id: "doc",
|
||||||
name: "DidSave",
|
name: "DidSave",
|
||||||
});
|
});
|
||||||
@ -1614,7 +1615,7 @@ const PDFViewerApplication = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
scripting.dispatchEventInSandbox({
|
await scripting.dispatchEventInSandbox({
|
||||||
id: "doc",
|
id: "doc",
|
||||||
name: "Open",
|
name: "Open",
|
||||||
});
|
});
|
||||||
@ -1967,6 +1968,8 @@ const PDFViewerApplication = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
beforePrint() {
|
beforePrint() {
|
||||||
|
// Given that the "beforeprint" browser event is synchronous, we
|
||||||
|
// unfortunately cannot await the scripting event dispatching here.
|
||||||
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
||||||
id: "doc",
|
id: "doc",
|
||||||
name: "WillPrint",
|
name: "WillPrint",
|
||||||
@ -2033,6 +2036,8 @@ const PDFViewerApplication = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
afterPrint() {
|
afterPrint() {
|
||||||
|
// Given that the "afterprint" browser event is synchronous, we
|
||||||
|
// unfortunately cannot await the scripting event dispatching here.
|
||||||
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
||||||
id: "doc",
|
id: "doc",
|
||||||
name: "DidPrint",
|
name: "DidPrint",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user