Merge pull request #19161 from Snuffleupagus/Promise-try

Introduce `Promise.try()` usage in the code-base
This commit is contained in:
Jonas Jenwald 2024-12-05 10:48:29 +01:00 committed by GitHub
commit 2ad8f6155c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 13 deletions

View File

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

View File

@ -1124,6 +1124,19 @@ function fromBase64Util(str) {
return stringToBytes(atob(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 { export {
AbortException, AbortException,
AnnotationActionEventType, AnnotationActionEventType,