Merge pull request #13813 from Snuffleupagus/rm-closure-API

Remove a couple of closures in the `src/display/api.js` file
This commit is contained in:
Tim van der Meij 2021-07-30 21:55:45 +02:00 committed by GitHub
commit 99b14a9da0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 258 additions and 274 deletions

View File

@ -524,41 +524,12 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
* The loading task controls the operations required to load a PDF document * The loading task controls the operations required to load a PDF document
* (such as network requests) and provides a way to listen for completion, * (such as network requests) and provides a way to listen for completion,
* after which individual pages can be rendered. * after which individual pages can be rendered.
*
* @typedef {Object} PDFDocumentLoadingTask
* @property {string} docId - Unique identifier for the document loading task.
* @property {boolean} destroyed - Whether the loading task is destroyed or not.
* @property {function} [onPassword] - Callback to request a password if a wrong
* or no password was provided. The callback receives two parameters: a
* function that should be called with the new password, and a reason (see
* {@link PasswordResponses}).
* @property {function} [onProgress] - Callback to be able to monitor the
* loading progress of the PDF file (necessary to implement e.g. a loading
* bar). The callback receives an {@link OnProgressParameters} argument.
* @property {function} [onUnsupportedFeature] - Callback for when an
* unsupported feature is used in the PDF document. The callback receives an
* {@link UNSUPPORTED_FEATURES} argument.
* @property {Promise<PDFDocumentProxy>} promise - Promise for document loading
* task completion.
* @property {function} destroy - Abort all network requests and destroy
* the worker. Returns a promise that is resolved when destruction is
* completed.
*/ */
/**
* @type {any}
* @ignore
*/
const PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
let nextDocumentId = 0;
/**
* The loading task controls the operations required to load a PDF document
* (such as network requests) and provides a way to listen for completion,
* after which individual pages can be rendered.
*/
// eslint-disable-next-line no-shadow
class PDFDocumentLoadingTask { class PDFDocumentLoadingTask {
static get idCounters() {
return shadow(this, "idCounters", { doc: 0 });
}
constructor() { constructor() {
this._capability = createPromiseCapability(); this._capability = createPromiseCapability();
this._transport = null; this._transport = null;
@ -568,7 +539,7 @@ const PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
* Unique identifier for the document loading task. * Unique identifier for the document loading task.
* @type {string} * @type {string}
*/ */
this.docId = "d" + nextDocumentId++; this.docId = `d${PDFDocumentLoadingTask.idCounters.doc++}`;
/** /**
* Whether the loading task is destroyed or not. * Whether the loading task is destroyed or not.
@ -609,6 +580,7 @@ const PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
} }
/** /**
* Abort all network requests and destroy the worker.
* @returns {Promise<void>} A promise that is resolved when destruction is * @returns {Promise<void>} A promise that is resolved when destruction is
* completed. * completed.
*/ */
@ -627,8 +599,6 @@ const PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
}); });
} }
} }
return PDFDocumentLoadingTask;
})();
/** /**
* Abstract class to support range requests file loading. * Abstract class to support range requests file loading.
@ -1935,8 +1905,8 @@ class LoopbackPort {
* @typedef {Object} PDFWorkerParameters * @typedef {Object} PDFWorkerParameters
* @property {string} [name] - The name of the worker. * @property {string} [name] - The name of the worker.
* @property {Object} [port] - The `workerPort` object. * @property {Object} [port] - The `workerPort` object.
* @property {number} [verbosity] - Controls the logging level; the * @property {number} [verbosity] - Controls the logging level;
* constants from {@link VerbosityLevel} should be used. * the constants from {@link VerbosityLevel} should be used.
*/ */
/** @type {any} */ /** @type {any} */
@ -1953,13 +1923,11 @@ const PDFWorker = (function PDFWorkerClosure() {
// Workers aren't supported in Node.js, force-disabling them there. // Workers aren't supported in Node.js, force-disabling them there.
isWorkerDisabled = true; isWorkerDisabled = true;
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB")) { fallbackWorkerSrc = PDFJSDev.test("LIB")
fallbackWorkerSrc = "../pdf.worker.js"; ? "../pdf.worker.js"
} else { : "./pdf.worker.js";
fallbackWorkerSrc = "./pdf.worker.js"; } else if (typeof document === "object") {
} const pdfjsFilePath = document?.currentScript?.src;
} else if (typeof document === "object" && "currentScript" in document) {
const pdfjsFilePath = document.currentScript?.src;
if (pdfjsFilePath) { if (pdfjsFilePath) {
fallbackWorkerSrc = pdfjsFilePath.replace( fallbackWorkerSrc = pdfjsFilePath.replace(
/(\.(?:min\.)?js)(\?.*)?$/i, /(\.(?:min\.)?js)(\?.*)?$/i,
@ -1983,16 +1951,14 @@ const PDFWorker = (function PDFWorkerClosure() {
} }
function getMainThreadWorkerMessageHandler() { function getMainThreadWorkerMessageHandler() {
let mainWorkerMessageHandler;
try { try {
mainWorkerMessageHandler = globalThis.pdfjsWorker?.WorkerMessageHandler; return globalThis.pdfjsWorker?.WorkerMessageHandler || null;
} catch (ex) { } catch (ex) {
/* Ignore errors. */ return null;
} }
return mainWorkerMessageHandler || null;
} }
// Loads worker code into main thread. // Loads worker code into main-thread.
function setupFakeWorkerGlobal() { function setupFakeWorkerGlobal() {
if (fakeWorkerCapability) { if (fakeWorkerCapability) {
return fakeWorkerCapability.promise; return fakeWorkerCapability.promise;
@ -3093,11 +3059,11 @@ class RenderTask {
* For internal use only. * For internal use only.
* @ignore * @ignore
*/ */
const InternalRenderTask = (function InternalRenderTaskClosure() {
const canvasInRendering = new WeakSet();
// eslint-disable-next-line no-shadow
class InternalRenderTask { class InternalRenderTask {
static get canvasInUse() {
return shadow(this, "canvasInUse", new WeakSet());
}
constructor({ constructor({
callback, callback,
params, params,
@ -3147,14 +3113,14 @@ const InternalRenderTask = (function InternalRenderTaskClosure() {
return; return;
} }
if (this._canvas) { if (this._canvas) {
if (canvasInRendering.has(this._canvas)) { if (InternalRenderTask.canvasInUse.has(this._canvas)) {
throw new Error( throw new Error(
"Cannot use the same canvas during multiple render() operations. " + "Cannot use the same canvas during multiple render() operations. " +
"Use different canvas or ensure previous operations were " + "Use different canvas or ensure previous operations were " +
"cancelled or completed." "cancelled or completed."
); );
} }
canvasInRendering.add(this._canvas); InternalRenderTask.canvasInUse.add(this._canvas);
} }
if (this._pdfBug && globalThis.StepperManager?.enabled) { if (this._pdfBug && globalThis.StepperManager?.enabled) {
@ -3193,7 +3159,7 @@ const InternalRenderTask = (function InternalRenderTaskClosure() {
this.gfx.endDrawing(); this.gfx.endDrawing();
} }
if (this._canvas) { if (this._canvas) {
canvasInRendering.delete(this._canvas); InternalRenderTask.canvasInUse.delete(this._canvas);
} }
this.callback( this.callback(
error || error ||
@ -3259,15 +3225,13 @@ const InternalRenderTask = (function InternalRenderTaskClosure() {
if (this.operatorList.lastChunk) { if (this.operatorList.lastChunk) {
this.gfx.endDrawing(); this.gfx.endDrawing();
if (this._canvas) { if (this._canvas) {
canvasInRendering.delete(this._canvas); InternalRenderTask.canvasInUse.delete(this._canvas);
} }
this.callback(); this.callback();
} }
} }
} }
} }
return InternalRenderTask;
})();
/** @type {string} */ /** @type {string} */
const version = const version =

View File

@ -385,6 +385,26 @@ describe("api", function () {
await loadingTask.destroy(); await loadingTask.destroy();
}); });
it("checks that `docId`s are unique and increasing", async function () {
const loadingTask1 = getDocument(basicApiGetDocumentParams);
await loadingTask1.promise;
const docId1 = loadingTask1.docId;
const loadingTask2 = getDocument(basicApiGetDocumentParams);
await loadingTask2.promise;
const docId2 = loadingTask2.docId;
expect(docId1).not.toEqual(docId2);
const docIdRegExp = /^d(\d)+$/,
docNum1 = docIdRegExp.exec(docId1)?.[1],
docNum2 = docIdRegExp.exec(docId2)?.[1];
expect(+docNum1).toBeLessThan(+docNum2);
await Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]);
});
}); });
describe("PDFWorker", function () { describe("PDFWorker", function () {