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:
parent
11ce57ac22
commit
ef0331877d
@ -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;
|
||||||
|
|||||||
@ -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,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user