Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/shared/message_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const StreamKind = {
START_COMPLETE: 8,
};

function onFn() {}

function wrapReason(reason) {
if (
!(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -511,7 +506,7 @@ class MessageHandler {
});
}
);
streamSink.sinkCapability.reject(wrapReason(data.reason));
streamSink.sinkCapability.reject(dataReason);
streamSink.isCancelled = true;
delete this.streamSinks[streamId];
break;
Expand Down
13 changes: 13 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading