1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 08:38:06 +02:00

Introduce Promise.try() usage in the code-base

This simplifies the creation of Promises in some cases; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/try
This commit is contained in:
Jonas Jenwald 2024-11-13 11:07:55 +01:00
parent 11ce57ac22
commit ef0331877d
2 changed files with 21 additions and 13 deletions

View file

@ -41,6 +41,8 @@ const StreamKind = {
START_COMPLETE: 8,
};
function onFn() {}
function wrapReason(reason) {
if (
!(
@ -121,9 +123,7 @@ class MessageHandler {
targetName = data.sourceName,
comObj = this.comObj;
new Promise(function (resolve) {
resolve(action(data.data));
}).then(
Promise.try(action, data.data).then(
function (result) {
comObj.postMessage({
sourceName,
@ -365,9 +365,7 @@ class MessageHandler {
streamSink.ready = streamSink.sinkCapability.promise;
this.streamSinks[streamId] = streamSink;
new Promise(function (resolve) {
resolve(action(data.data, streamSink));
}).then(
Promise.try(action, data.data, streamSink).then(
function () {
comObj.postMessage({
sourceName,
@ -432,9 +430,7 @@ class MessageHandler {
// Reset desiredSize property of sink on every pull.
streamSink.desiredSize = data.desiredSize;
new Promise(function (resolve) {
resolve(streamSink.onPull?.());
}).then(
Promise.try(streamSink.onPull || onFn).then(
function () {
comObj.postMessage({
sourceName,
@ -488,10 +484,9 @@ class MessageHandler {
if (!streamSink) {
break;
}
const dataReason = wrapReason(data.reason);
new Promise(function (resolve) {
resolve(streamSink.onCancel?.(wrapReason(data.reason)));
}).then(
Promise.try(streamSink.onCancel || onFn, dataReason).then(
function () {
comObj.postMessage({
sourceName,
@ -511,7 +506,7 @@ class MessageHandler {
});
}
);
streamSink.sinkCapability.reject(wrapReason(data.reason));
streamSink.sinkCapability.reject(dataReason);
streamSink.isCancelled = true;
delete this.streamSinks[streamId];
break;

View file

@ -1124,6 +1124,19 @@ function fromBase64Util(str) {
return stringToBytes(atob(str));
}
// TODO: Remove this once https://bugzilla.mozilla.org/show_bug.cgi?id=1928493
// is fixed.
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("SKIP_BABEL")) &&
typeof Promise.try !== "function"
) {
Promise.try = function (fn, ...args) {
return new Promise(resolve => {
resolve(fn(...args));
});
};
}
export {
AbortException,
AnnotationActionEventType,