Remove the closure used with the PDFDocumentLoadingTask class
				
					
				
			This patch utilizes the same approach as used in lots of other parts of the code-base, which thus *slightly* reduces the size of this code. By removing some of the (current) indirection, we can also simplify the JSDocs a little bit. Looking at the `gulp jsdoc` output, this actually seem to *improve* the documentation for this class.
This commit is contained in:
		
							parent
							
								
									4ad5c5d52a
								
							
						
					
					
						commit
						b18620ac0f
					
				| @ -524,111 +524,81 @@ 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. |  | ||||||
|  */ |  */ | ||||||
|  | class PDFDocumentLoadingTask { | ||||||
|  |   static get idCounters() { | ||||||
|  |     return shadow(this, "idCounters", { doc: 0 }); | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
| /** |   constructor() { | ||||||
|  * @type {any} |     this._capability = createPromiseCapability(); | ||||||
|  * @ignore |     this._transport = null; | ||||||
|  */ |     this._worker = null; | ||||||
| const PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() { | 
 | ||||||
|   let nextDocumentId = 0; |     /** | ||||||
|  |      * Unique identifier for the document loading task. | ||||||
|  |      * @type {string} | ||||||
|  |      */ | ||||||
|  |     this.docId = `d${PDFDocumentLoadingTask.idCounters.doc++}`; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Whether the loading task is destroyed or not. | ||||||
|  |      * @type {boolean} | ||||||
|  |      */ | ||||||
|  |     this.destroyed = false; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 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}). | ||||||
|  |      * @type {function} | ||||||
|  |      */ | ||||||
|  |     this.onPassword = null; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 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. | ||||||
|  |      * @type {function} | ||||||
|  |      */ | ||||||
|  |     this.onProgress = null; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Callback for when an unsupported feature is used in the PDF document. | ||||||
|  |      * The callback receives an {@link UNSUPPORTED_FEATURES} argument. | ||||||
|  |      * @type {function} | ||||||
|  |      */ | ||||||
|  |     this.onUnsupportedFeature = null; | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|    * The loading task controls the operations required to load a PDF document |    * Promise for document loading task completion. | ||||||
|    * (such as network requests) and provides a way to listen for completion, |    * @type {Promise<PDFDocumentProxy>} | ||||||
|    * after which individual pages can be rendered. |  | ||||||
|    */ |    */ | ||||||
|   // eslint-disable-next-line no-shadow
 |   get promise() { | ||||||
|   class PDFDocumentLoadingTask { |     return this._capability.promise; | ||||||
|     constructor() { |  | ||||||
|       this._capability = createPromiseCapability(); |  | ||||||
|       this._transport = null; |  | ||||||
|       this._worker = null; |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * Unique identifier for the document loading task. |  | ||||||
|        * @type {string} |  | ||||||
|        */ |  | ||||||
|       this.docId = "d" + nextDocumentId++; |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * Whether the loading task is destroyed or not. |  | ||||||
|        * @type {boolean} |  | ||||||
|        */ |  | ||||||
|       this.destroyed = false; |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * 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}). |  | ||||||
|        * @type {function} |  | ||||||
|        */ |  | ||||||
|       this.onPassword = null; |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * 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. |  | ||||||
|        * @type {function} |  | ||||||
|        */ |  | ||||||
|       this.onProgress = null; |  | ||||||
| 
 |  | ||||||
|       /** |  | ||||||
|        * Callback for when an unsupported feature is used in the PDF document. |  | ||||||
|        * The callback receives an {@link UNSUPPORTED_FEATURES} argument. |  | ||||||
|        * @type {function} |  | ||||||
|        */ |  | ||||||
|       this.onUnsupportedFeature = null; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Promise for document loading task completion. |  | ||||||
|      * @type {Promise<PDFDocumentProxy>} |  | ||||||
|      */ |  | ||||||
|     get promise() { |  | ||||||
|       return this._capability.promise; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * @returns {Promise<void>} A promise that is resolved when destruction is |  | ||||||
|      *   completed. |  | ||||||
|      */ |  | ||||||
|     destroy() { |  | ||||||
|       this.destroyed = true; |  | ||||||
| 
 |  | ||||||
|       const transportDestroyed = !this._transport |  | ||||||
|         ? Promise.resolve() |  | ||||||
|         : this._transport.destroy(); |  | ||||||
|       return transportDestroyed.then(() => { |  | ||||||
|         this._transport = null; |  | ||||||
|         if (this._worker) { |  | ||||||
|           this._worker.destroy(); |  | ||||||
|           this._worker = null; |  | ||||||
|         } |  | ||||||
|       }); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|   return PDFDocumentLoadingTask; | 
 | ||||||
| })(); |   /** | ||||||
|  |    * Abort all network requests and destroy the worker. | ||||||
|  |    * @returns {Promise<void>} A promise that is resolved when destruction is | ||||||
|  |    *   completed. | ||||||
|  |    */ | ||||||
|  |   destroy() { | ||||||
|  |     this.destroyed = true; | ||||||
|  | 
 | ||||||
|  |     const transportDestroyed = !this._transport | ||||||
|  |       ? Promise.resolve() | ||||||
|  |       : this._transport.destroy(); | ||||||
|  |     return transportDestroyed.then(() => { | ||||||
|  |       this._transport = null; | ||||||
|  |       if (this._worker) { | ||||||
|  |         this._worker.destroy(); | ||||||
|  |         this._worker = null; | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Abstract class to support range requests file loading. |  * Abstract class to support range requests file loading. | ||||||
|  | |||||||
| @ -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 () { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user