Merge pull request #14683 from Snuffleupagus/sendTest-cleanup
[src/display/api.js] Simplify the `sendTest` function, used with Worker initialization (PR 14291 follow-up)
This commit is contained in:
		
						commit
						5de6af4e64
					
				@ -75,9 +75,8 @@ class WorkerMessageHandler {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
      testMessageProcessed = true;
 | 
					      testMessageProcessed = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Ensure that `TypedArray`s can be sent to the worker,
 | 
					      // Ensure that `TypedArray`s can be sent to the worker.
 | 
				
			||||||
      // and that `postMessage` transfers are supported.
 | 
					      handler.send("test", data instanceof Uint8Array);
 | 
				
			||||||
      handler.send("test", data instanceof Uint8Array && data[0] === 255);
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    handler.on("configure", function wphConfigure(data) {
 | 
					    handler.on("configure", function wphConfigure(data) {
 | 
				
			||||||
 | 
				
			|||||||
@ -533,9 +533,7 @@ async function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
 | 
				
			|||||||
 * after which individual pages can be rendered.
 | 
					 * after which individual pages can be rendered.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class PDFDocumentLoadingTask {
 | 
					class PDFDocumentLoadingTask {
 | 
				
			||||||
  static get idCounters() {
 | 
					  static #docId = 0;
 | 
				
			||||||
    return shadow(this, "idCounters", { doc: 0 });
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor() {
 | 
					  constructor() {
 | 
				
			||||||
    this._capability = createPromiseCapability();
 | 
					    this._capability = createPromiseCapability();
 | 
				
			||||||
@ -546,7 +544,7 @@ class PDFDocumentLoadingTask {
 | 
				
			|||||||
     * Unique identifier for the document loading task.
 | 
					     * Unique identifier for the document loading task.
 | 
				
			||||||
     * @type {string}
 | 
					     * @type {string}
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    this.docId = `d${PDFDocumentLoadingTask.idCounters.doc++}`;
 | 
					    this.docId = `d${PDFDocumentLoadingTask.#docId++}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Whether the loading task is destroyed or not.
 | 
					     * Whether the loading task is destroyed or not.
 | 
				
			||||||
@ -2017,16 +2015,14 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
 | 
				
			|||||||
 * @param {PDFWorkerParameters} params - The worker initialization parameters.
 | 
					 * @param {PDFWorkerParameters} params - The worker initialization parameters.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class PDFWorker {
 | 
					class PDFWorker {
 | 
				
			||||||
  static get _workerPorts() {
 | 
					  static #workerPorts = new WeakMap();
 | 
				
			||||||
    return shadow(this, "_workerPorts", new WeakMap());
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor({
 | 
					  constructor({
 | 
				
			||||||
    name = null,
 | 
					    name = null,
 | 
				
			||||||
    port = null,
 | 
					    port = null,
 | 
				
			||||||
    verbosity = getVerbosityLevel(),
 | 
					    verbosity = getVerbosityLevel(),
 | 
				
			||||||
  } = {}) {
 | 
					  } = {}) {
 | 
				
			||||||
    if (port && PDFWorker._workerPorts.has(port)) {
 | 
					    if (port && PDFWorker.#workerPorts.has(port)) {
 | 
				
			||||||
      throw new Error("Cannot use more than one PDFWorker per port.");
 | 
					      throw new Error("Cannot use more than one PDFWorker per port.");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -2040,7 +2036,7 @@ class PDFWorker {
 | 
				
			|||||||
    this._messageHandler = null;
 | 
					    this._messageHandler = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (port) {
 | 
					    if (port) {
 | 
				
			||||||
      PDFWorker._workerPorts.set(port, this);
 | 
					      PDFWorker.#workerPorts.set(port, this);
 | 
				
			||||||
      this._initializeFromPort(port);
 | 
					      this._initializeFromPort(port);
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -2171,16 +2167,9 @@ class PDFWorker {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const sendTest = () => {
 | 
					        const sendTest = () => {
 | 
				
			||||||
          const testObj = new Uint8Array([255]);
 | 
					          const testObj = new Uint8Array();
 | 
				
			||||||
          // Some versions of Opera throw a DATA_CLONE_ERR on serializing the
 | 
					          // Ensure that we can use `postMessage` transfers.
 | 
				
			||||||
          // typed array. Also, checking if we can use transfers.
 | 
					 | 
				
			||||||
          try {
 | 
					 | 
				
			||||||
          messageHandler.send("test", testObj, [testObj.buffer]);
 | 
					          messageHandler.send("test", testObj, [testObj.buffer]);
 | 
				
			||||||
          } catch (ex) {
 | 
					 | 
				
			||||||
            warn("Cannot use postMessage transfers.");
 | 
					 | 
				
			||||||
            testObj[0] = 0;
 | 
					 | 
				
			||||||
            messageHandler.send("test", testObj);
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // It might take time for the worker to initialize. We will try to send
 | 
					        // It might take time for the worker to initialize. We will try to send
 | 
				
			||||||
@ -2245,7 +2234,7 @@ class PDFWorker {
 | 
				
			|||||||
      this._webWorker.terminate();
 | 
					      this._webWorker.terminate();
 | 
				
			||||||
      this._webWorker = null;
 | 
					      this._webWorker = null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    PDFWorker._workerPorts.delete(this._port);
 | 
					    PDFWorker.#workerPorts.delete(this._port);
 | 
				
			||||||
    this._port = null;
 | 
					    this._port = null;
 | 
				
			||||||
    if (this._messageHandler) {
 | 
					    if (this._messageHandler) {
 | 
				
			||||||
      this._messageHandler.destroy();
 | 
					      this._messageHandler.destroy();
 | 
				
			||||||
@ -2260,8 +2249,8 @@ class PDFWorker {
 | 
				
			|||||||
    if (!params?.port) {
 | 
					    if (!params?.port) {
 | 
				
			||||||
      throw new Error("PDFWorker.fromPort - invalid method signature.");
 | 
					      throw new Error("PDFWorker.fromPort - invalid method signature.");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (this._workerPorts.has(params.port)) {
 | 
					    if (this.#workerPorts.has(params.port)) {
 | 
				
			||||||
      return this._workerPorts.get(params.port);
 | 
					      return this.#workerPorts.get(params.port);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return new PDFWorker(params);
 | 
					    return new PDFWorker(params);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -3196,9 +3185,7 @@ class RenderTask {
 | 
				
			|||||||
 * @ignore
 | 
					 * @ignore
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class InternalRenderTask {
 | 
					class InternalRenderTask {
 | 
				
			||||||
  static get canvasInUse() {
 | 
					  static #canvasInUse = new WeakSet();
 | 
				
			||||||
    return shadow(this, "canvasInUse", new WeakSet());
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor({
 | 
					  constructor({
 | 
				
			||||||
    callback,
 | 
					    callback,
 | 
				
			||||||
@ -3251,14 +3238,14 @@ class InternalRenderTask {
 | 
				
			|||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (this._canvas) {
 | 
					    if (this._canvas) {
 | 
				
			||||||
      if (InternalRenderTask.canvasInUse.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."
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      InternalRenderTask.canvasInUse.add(this._canvas);
 | 
					      InternalRenderTask.#canvasInUse.add(this._canvas);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (this._pdfBug && globalThis.StepperManager?.enabled) {
 | 
					    if (this._pdfBug && globalThis.StepperManager?.enabled) {
 | 
				
			||||||
@ -3298,7 +3285,7 @@ class InternalRenderTask {
 | 
				
			|||||||
      this.gfx.endDrawing();
 | 
					      this.gfx.endDrawing();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (this._canvas) {
 | 
					    if (this._canvas) {
 | 
				
			||||||
      InternalRenderTask.canvasInUse.delete(this._canvas);
 | 
					      InternalRenderTask.#canvasInUse.delete(this._canvas);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    this.callback(
 | 
					    this.callback(
 | 
				
			||||||
      error ||
 | 
					      error ||
 | 
				
			||||||
@ -3364,7 +3351,7 @@ class InternalRenderTask {
 | 
				
			|||||||
      if (this.operatorList.lastChunk) {
 | 
					      if (this.operatorList.lastChunk) {
 | 
				
			||||||
        this.gfx.endDrawing();
 | 
					        this.gfx.endDrawing();
 | 
				
			||||||
        if (this._canvas) {
 | 
					        if (this._canvas) {
 | 
				
			||||||
          InternalRenderTask.canvasInUse.delete(this._canvas);
 | 
					          InternalRenderTask.#canvasInUse.delete(this._canvas);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        this.callback();
 | 
					        this.callback();
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user