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

Polyfill AbortSignal.any in PDF.js legacy builds #19218

Closed
Closed
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
64 changes: 64 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,70 @@ if (
};
}

if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("SKIP_BABEL") &&
typeof AbortSignal.any !== "function"
) {
/**
* This polyfill is based on https://gist.github.com/CNSeniorious000/9fc1a72e45358dd7c9e2f16e5d26df5c
* which has no licensing information available.
*
* The following changes have been made:
* - Removal of commented out code.
* - Removal of test-only `count` variable and related code/logging.
* - Renaming of variable in `clear` function, to avoid variable shadowing.
*/
const registry = new FinalizationRegistry(callback => void callback());

AbortSignal.any = function polyfillAbortSignalAny(signals) {
const controller = new AbortController();
for (const signal of signals) {
if (signal.aborted) {
controller.abort(signal.reason);
return controller.signal;
}
}
const controllerRef = new WeakRef(controller);
/** @type {[WeakRef<AbortSignal>, (() => void)][]} */
const eventListenerPairs = [];
let followingCount = signals.length;

signals.forEach(signal => {
const signalRef = new WeakRef(signal);
function abort() {
controllerRef.deref()?.abort(signalRef.deref()?.reason);
}
signal.addEventListener("abort", abort);
eventListenerPairs.push([signalRef, abort]);
registry.register(signal, () => !--followingCount && clear(), signal);
});
function clear() {
eventListenerPairs.forEach(([signalRef, abort]) => {
const signal = signalRef.deref();
if (signal) {
signal.removeEventListener("abort", abort);
registry.unregister(signal);
}
const cntlr = controllerRef.deref();
if (cntlr) {
registry.unregister(cntlr.signal);
delete cntlr.signal.__controller;
}
});
}

const { signal } = controller;

registry.register(signal, clear, signal);
signal.addEventListener("abort", clear);

signal.__controller = controller;

return signal;
};
}

export {
AbortException,
AnnotationActionEventType,
Expand Down
8 changes: 2 additions & 6 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,7 @@ const PDFViewerApplication = {
}

if (appConfig.annotationEditorParams) {
if (
((typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function") &&
annotationEditorMode !== AnnotationEditorType.DISABLE
) {
if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
this.annotationEditorParams = new AnnotationEditorParams(
appConfig.annotationEditorParams,
eventBus
Expand Down Expand Up @@ -2046,7 +2042,7 @@ const PDFViewerApplication = {

this._touchManager = new TouchManager({
container: window,
isPinchingDisabled: () => this.pdfViewer.isInPresentationMode,
isPinchingDisabled: () => pdfViewer.isInPresentationMode,
isPinchingStopped: () => this.overlayManager?.active,
onPinching: this.touchPinchCallback.bind(this),
onPinchEnd: this.touchPinchEndCallback.bind(this),
Expand Down
14 changes: 2 additions & 12 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,7 @@ class PDFViewer {
hiddenCapability.resolve();
}
},
{
signal:
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function"
? AbortSignal.any([signal, ac.signal])
: signal,
}
{ signal: AbortSignal.any([signal, ac.signal]) }
);

await Promise.race([
Expand Down Expand Up @@ -889,11 +883,7 @@ class PDFViewer {
viewer.before(element);
}

if (
((typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function") &&
annotationEditorMode !== AnnotationEditorType.DISABLE
) {
if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
const mode = annotationEditorMode;

if (pdfDocument.isPureXfa) {
Expand Down
Loading