Use arrow functions with some Promise.then calls

A lot of this is fairly old code, which we can shorten slightly by using arrow functions instead of "regular" functions.
This commit is contained in:
Jonas Jenwald 2025-03-02 18:58:52 +01:00
parent 3cd1b10433
commit 7b5cd9cddd
5 changed files with 63 additions and 80 deletions

View File

@ -1164,9 +1164,7 @@ class Annotation {
} }
const objectLoader = new ObjectLoader(resources, keys, resources.xref); const objectLoader = new ObjectLoader(resources, keys, resources.xref);
return objectLoader.load().then(function () { return objectLoader.load().then(() => resources);
return resources;
});
}); });
} }

View File

@ -1475,9 +1475,7 @@ class Catalog {
if (!found) { if (!found) {
throw new FormatError("Kid reference not found in parent's kids."); throw new FormatError("Kid reference not found in parent's kids.");
} }
return Promise.all(kidPromises).then(function () { return Promise.all(kidPromises).then(() => [total, parentRef]);
return [total, parentRef];
});
}); });
} }

View File

@ -546,9 +546,7 @@ class Page {
resources: this.resources, resources: this.resources,
operatorList: opList, operatorList: opList,
}) })
.then(function () { .then(() => opList);
return opList;
});
}); });
// Fetch the page's annotations and add their operator lists to the // Fetch the page's annotations and add their operator lists to the

View File

@ -450,9 +450,9 @@ class WorkerMessageHandler {
}); });
handler.on("GetPageJSActions", function ({ pageIndex }) { handler.on("GetPageJSActions", function ({ pageIndex }) {
return pdfManager.getPage(pageIndex).then(function (page) { return pdfManager
return pdfManager.ensure(page, "jsActions"); .getPage(pageIndex)
}); .then(page => pdfManager.ensure(page, "jsActions"));
}); });
handler.on("GetOutline", function (data) { handler.on("GetOutline", function (data) {
@ -479,9 +479,7 @@ class WorkerMessageHandler {
}); });
handler.on("GetData", function (data) { handler.on("GetData", function (data) {
return pdfManager.requestLoadedStream().then(function (stream) { return pdfManager.requestLoadedStream().then(stream => stream.bytes);
return stream.bytes;
});
}); });
handler.on("GetAnnotations", function ({ pageIndex, intent }) { handler.on("GetAnnotations", function ({ pageIndex, intent }) {
@ -812,9 +810,9 @@ class WorkerMessageHandler {
}); });
handler.on("GetStructTree", function (data) { handler.on("GetStructTree", function (data) {
return pdfManager.getPage(data.pageIndex).then(function (page) { return pdfManager
return pdfManager.ensure(page, "getStructTree"); .getPage(data.pageIndex)
}); .then(page => pdfManager.ensure(page, "getStructTree"));
}); });
handler.on("FontFallback", function (data) { handler.on("FontFallback", function (data) {
@ -872,9 +870,9 @@ class WorkerMessageHandler {
return pdfManager.ensureDoc("startXRef"); return pdfManager.ensureDoc("startXRef");
}); });
handler.on("GetAnnotArray", function (data) { handler.on("GetAnnotArray", function (data) {
return pdfManager.getPage(data.pageIndex).then(function (page) { return pdfManager
return page.annotations.map(a => a.toString()); .getPage(data.pageIndex)
}); .then(page => page.annotations.map(a => a.toString()));
}); });
} }

View File

@ -192,9 +192,9 @@ describe("api", function () {
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true); expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
// This can be somewhat random -- we cannot guarantee perfect // This can be somewhat random -- we cannot guarantee perfect
// 'Terminate' message to the worker before/after setting up pdfManager. // 'Terminate' message to the worker before/after setting up pdfManager.
const destroyed = loadingTask._worker.promise.then(function () { const destroyed = loadingTask._worker.promise.then(() =>
return loadingTask.destroy(); loadingTask.destroy()
}); );
await destroyed; await destroyed;
expect(true).toEqual(true); expect(true).toEqual(true);
@ -861,9 +861,9 @@ describe("api", function () {
expect(!!worker).toEqual(true); expect(!!worker).toEqual(true);
}); });
const destroyPromise = loadingTask.promise.then(function () { const destroyPromise = loadingTask.promise.then(() =>
return loadingTask.destroy(); loadingTask.destroy()
}); );
await destroyPromise; await destroyPromise;
const destroyedWorker = loadingTask._worker; const destroyedWorker = loadingTask._worker;
@ -890,9 +890,9 @@ describe("api", function () {
expect(messageHandlerPort === worker.port).toEqual(true); expect(messageHandlerPort === worker.port).toEqual(true);
}); });
const destroyPromise = loadingTask.promise.then(function () { const destroyPromise = loadingTask.promise.then(() =>
return loadingTask.destroy(); loadingTask.destroy()
}); );
await destroyPromise; await destroyPromise;
expect(worker.destroyed).toEqual(false); expect(worker.destroyed).toEqual(false);
@ -1141,8 +1141,8 @@ describe("api", function () {
buildGetDocumentParams("Pages-tree-refs.pdf") buildGetDocumentParams("Pages-tree-refs.pdf")
); );
const page1 = loadingTask.promise.then(function (pdfDoc) { const page1 = loadingTask.promise.then(pdfDoc =>
return pdfDoc.getPage(1).then( pdfDoc.getPage(1).then(
function (pdfPage) { function (pdfPage) {
expect(pdfPage instanceof PDFPageProxy).toEqual(true); expect(pdfPage instanceof PDFPageProxy).toEqual(true);
expect(pdfPage.ref).toEqual({ num: 6, gen: 0 }); expect(pdfPage.ref).toEqual({ num: 6, gen: 0 });
@ -1150,11 +1150,11 @@ describe("api", function () {
function (reason) { function (reason) {
throw new Error("shall not fail for valid page"); throw new Error("shall not fail for valid page");
} }
)
); );
});
const page2 = loadingTask.promise.then(function (pdfDoc) { const page2 = loadingTask.promise.then(pdfDoc =>
return pdfDoc.getPage(2).then( pdfDoc.getPage(2).then(
function (pdfPage) { function (pdfPage) {
throw new Error("shall fail for invalid page"); throw new Error("shall fail for invalid page");
}, },
@ -1164,8 +1164,8 @@ describe("api", function () {
"Pages tree contains circular reference." "Pages tree contains circular reference."
); );
} }
)
); );
});
await Promise.all([page1, page2]); await Promise.all([page1, page2]);
await loadingTask.destroy(); await loadingTask.destroy();
@ -1396,29 +1396,29 @@ describe("api", function () {
it("gets page labels", async function () { it("gets page labels", async function () {
// PageLabels with Roman/Arabic numerals. // PageLabels with Roman/Arabic numerals.
const loadingTask0 = getDocument(buildGetDocumentParams("bug793632.pdf")); const loadingTask0 = getDocument(buildGetDocumentParams("bug793632.pdf"));
const promise0 = loadingTask0.promise.then(function (pdfDoc) { const promise0 = loadingTask0.promise.then(pdfDoc =>
return pdfDoc.getPageLabels(); pdfDoc.getPageLabels()
}); );
// PageLabels with only a label prefix. // PageLabels with only a label prefix.
const loadingTask1 = getDocument(buildGetDocumentParams("issue1453.pdf")); const loadingTask1 = getDocument(buildGetDocumentParams("issue1453.pdf"));
const promise1 = loadingTask1.promise.then(function (pdfDoc) { const promise1 = loadingTask1.promise.then(pdfDoc =>
return pdfDoc.getPageLabels(); pdfDoc.getPageLabels()
}); );
// PageLabels identical to standard page numbering. // PageLabels identical to standard page numbering.
const loadingTask2 = getDocument(buildGetDocumentParams("rotation.pdf")); const loadingTask2 = getDocument(buildGetDocumentParams("rotation.pdf"));
const promise2 = loadingTask2.promise.then(function (pdfDoc) { const promise2 = loadingTask2.promise.then(pdfDoc =>
return pdfDoc.getPageLabels(); pdfDoc.getPageLabels()
}); );
// PageLabels with bad "Prefix" entries. // PageLabels with bad "Prefix" entries.
const loadingTask3 = getDocument( const loadingTask3 = getDocument(
buildGetDocumentParams("bad-PageLabels.pdf") buildGetDocumentParams("bad-PageLabels.pdf")
); );
const promise3 = loadingTask3.promise.then(function (pdfDoc) { const promise3 = loadingTask3.promise.then(pdfDoc =>
return pdfDoc.getPageLabels(); pdfDoc.getPageLabels()
}); );
const pageLabels = await Promise.all([ const pageLabels = await Promise.all([
promise0, promise0,
@ -1512,9 +1512,7 @@ describe("api", function () {
); );
const promise1 = loadingTask1.promise const promise1 = loadingTask1.promise
.then(function (pdfDoc) { .then(pdfDoc => pdfDoc.getOpenAction())
return pdfDoc.getOpenAction();
})
.then(function (openAction) { .then(function (openAction) {
expect(openAction.dest).toBeUndefined(); expect(openAction.dest).toBeUndefined();
expect(openAction.action).toEqual("Print"); expect(openAction.action).toEqual("Print");
@ -1522,9 +1520,7 @@ describe("api", function () {
return loadingTask1.destroy(); return loadingTask1.destroy();
}); });
const promise2 = loadingTask2.promise const promise2 = loadingTask2.promise
.then(function (pdfDoc) { .then(pdfDoc => pdfDoc.getOpenAction())
return pdfDoc.getOpenAction();
})
.then(function (openAction) { .then(function (openAction) {
expect(openAction.dest).toBeUndefined(); expect(openAction.dest).toBeUndefined();
expect(openAction.action).toEqual("Print"); expect(openAction.action).toEqual("Print");
@ -2064,25 +2060,25 @@ describe("api", function () {
const loadingTask0 = getDocument( const loadingTask0 = getDocument(
buildGetDocumentParams("issue9972-1.pdf") buildGetDocumentParams("issue9972-1.pdf")
); );
const promise0 = loadingTask0.promise.then(function (pdfDoc) { const promise0 = loadingTask0.promise.then(pdfDoc =>
return pdfDoc.getPermissions(); pdfDoc.getPermissions()
}); );
// Printing not allowed. // Printing not allowed.
const loadingTask1 = getDocument( const loadingTask1 = getDocument(
buildGetDocumentParams("issue9972-2.pdf") buildGetDocumentParams("issue9972-2.pdf")
); );
const promise1 = loadingTask1.promise.then(function (pdfDoc) { const promise1 = loadingTask1.promise.then(pdfDoc =>
return pdfDoc.getPermissions(); pdfDoc.getPermissions()
}); );
// Copying not allowed. // Copying not allowed.
const loadingTask2 = getDocument( const loadingTask2 = getDocument(
buildGetDocumentParams("issue9972-3.pdf") buildGetDocumentParams("issue9972-3.pdf")
); );
const promise2 = loadingTask2.promise.then(function (pdfDoc) { const promise2 = loadingTask2.promise.then(pdfDoc =>
return pdfDoc.getPermissions(); pdfDoc.getPermissions()
}); );
const totalPermissionCount = Object.keys(PermissionFlag).length; const totalPermissionCount = Object.keys(PermissionFlag).length;
const permissions = await Promise.all([promise0, promise1, promise2]); const permissions = await Promise.all([promise0, promise1, promise2]);
@ -3050,9 +3046,7 @@ describe("api", function () {
params.url = url.href; params.url = url.href;
loadingTask = getDocument(params); loadingTask = getDocument(params);
return loadingTask.promise return loadingTask.promise
.then(function (pdf) { .then(pdf => pdf.destroy())
return pdf.destroy();
})
.then( .then(
function () { function () {
expect(expectSuccess).toEqual(true); expect(expectSuccess).toEqual(true);
@ -3293,11 +3287,10 @@ describe("api", function () {
const filename = "bug766086.pdf"; const filename = "bug766086.pdf";
const defaultLoadingTask = getDocument(buildGetDocumentParams(filename)); const defaultLoadingTask = getDocument(buildGetDocumentParams(filename));
const defaultPromise = defaultLoadingTask.promise.then(function (pdfDoc) { const defaultPromise = defaultLoadingTask.promise.then(async pdfDoc => {
return pdfDoc.getPage(1).then(function (pdfPage) { const pdfPage = await pdfDoc.getPage(1);
return pdfPage.getAnnotations(); return pdfPage.getAnnotations();
}); });
});
const docBaseUrlLoadingTask = getDocument( const docBaseUrlLoadingTask = getDocument(
buildGetDocumentParams(filename, { buildGetDocumentParams(filename, {
@ -3305,10 +3298,9 @@ describe("api", function () {
}) })
); );
const docBaseUrlPromise = docBaseUrlLoadingTask.promise.then( const docBaseUrlPromise = docBaseUrlLoadingTask.promise.then(
function (pdfDoc) { async pdfDoc => {
return pdfDoc.getPage(1).then(function (pdfPage) { const pdfPage = await pdfDoc.getPage(1);
return pdfPage.getAnnotations(); return pdfPage.getAnnotations();
});
} }
); );
@ -3318,11 +3310,10 @@ describe("api", function () {
}) })
); );
const invalidDocBaseUrlPromise = const invalidDocBaseUrlPromise =
invalidDocBaseUrlLoadingTask.promise.then(function (pdfDoc) { invalidDocBaseUrlLoadingTask.promise.then(async pdfDoc => {
return pdfDoc.getPage(1).then(function (pdfPage) { const pdfPage = await pdfDoc.getPage(1);
return pdfPage.getAnnotations(); return pdfPage.getAnnotations();
}); });
});
const [ const [
defaultAnnotations, defaultAnnotations,