[api-minor] Add a basic AbortSignal.any polyfill in PDF.js legacy builds

This is an admittedly very basic polyfill, to allow us to remove a bunch of inline feature testing, that I've thrown together based on reading https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static and related MDN articles.

Compared to PR 19218 it's obviously much more "primitive", however the implementation is simple and it doesn't suffer from any licensing issues (since I wrote the code myself).
This commit is contained in:
Jonas Jenwald 2025-03-18 10:54:49 +01:00
parent 00e3a4d87a
commit 028e4f7ea8
3 changed files with 42 additions and 30 deletions

View File

@ -1172,6 +1172,37 @@ if (
}; };
} }
if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("SKIP_BABEL") &&
typeof AbortSignal.any !== "function"
) {
AbortSignal.any = function (iterable) {
const ac = new AbortController();
const { signal } = ac;
// Return immediately if any of the signals are already aborted.
for (const s of iterable) {
if (s.aborted) {
ac.abort(s.reason);
return signal;
}
}
// Register "abort" listeners for all signals.
for (const s of iterable) {
s.addEventListener(
"abort",
() => {
ac.abort(s.reason);
},
{ signal } // Automatically remove the listener.
);
}
return signal;
};
}
export { export {
_isValidExplicitDest, _isValidExplicitDest,
AbortException, AbortException,

View File

@ -559,11 +559,7 @@ const PDFViewerApplication = {
} }
if (appConfig.annotationEditorParams) { if (appConfig.annotationEditorParams) {
if ( if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
((typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function") &&
annotationEditorMode !== AnnotationEditorType.DISABLE
) {
const editorSignatureButton = appConfig.toolbar?.editorSignatureButton; const editorSignatureButton = appConfig.toolbar?.editorSignatureButton;
if (editorSignatureButton && AppOptions.get("enableSignatureEditor")) { if (editorSignatureButton && AppOptions.get("enableSignatureEditor")) {
editorSignatureButton.parentElement.hidden = false; editorSignatureButton.parentElement.hidden = false;
@ -2075,10 +2071,6 @@ const PDFViewerApplication = {
_windowAbortController: { signal }, _windowAbortController: { signal },
} = this; } = this;
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function"
) {
this._touchManager = new TouchManager({ this._touchManager = new TouchManager({
container: window, container: window,
isPinchingDisabled: () => pdfViewer.isInPresentationMode, isPinchingDisabled: () => pdfViewer.isInPresentationMode,
@ -2087,7 +2079,6 @@ const PDFViewerApplication = {
onPinchEnd: this.touchPinchEndCallback.bind(this), onPinchEnd: this.touchPinchEndCallback.bind(this),
signal, signal,
}); });
}
function addWindowResolutionChange(evt = null) { function addWindowResolutionChange(evt = null) {
if (evt) { if (evt) {

View File

@ -711,13 +711,7 @@ class PDFViewer {
hiddenCapability.resolve(); hiddenCapability.resolve();
} }
}, },
{ { signal: AbortSignal.any([signal, ac.signal]) }
signal:
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function"
? AbortSignal.any([signal, ac.signal])
: signal,
}
); );
await Promise.race([ await Promise.race([
@ -914,11 +908,7 @@ class PDFViewer {
viewer.before(element); viewer.before(element);
} }
if ( if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
((typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function") &&
annotationEditorMode !== AnnotationEditorType.DISABLE
) {
const mode = annotationEditorMode; const mode = annotationEditorMode;
if (pdfDocument.isPureXfa) { if (pdfDocument.isPureXfa) {