Add a settled property, tracking the fulfilled/rejected stated of the Promise, to createPromiseCapability
				
					
				
			This allows cleaning-up code which is currently manually tracking the state of the Promise of a `createPromiseCapability` instance.
This commit is contained in:
		
							parent
							
								
									291e62b41e
								
							
						
					
					
						commit
						22468817e1
					
				| @ -972,23 +972,36 @@ function isSpace(ch) { | |||||||
|  * Promise Capability object. |  * Promise Capability object. | ||||||
|  * |  * | ||||||
|  * @typedef {Object} PromiseCapability |  * @typedef {Object} PromiseCapability | ||||||
|  * @property {Promise} promise - A promise object. |  * @property {Promise} promise - A Promise object. | ||||||
|  * @property {function} resolve - Fulfills the promise. |  * @property {boolean} settled - If the Promise has been fulfilled/rejected. | ||||||
|  * @property {function} reject - Rejects the promise. |  * @property {function} resolve - Fulfills the Promise. | ||||||
|  |  * @property {function} reject - Rejects the Promise. | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Creates a promise capability object. |  * Creates a promise capability object. | ||||||
|  * @alias createPromiseCapability |  * @alias createPromiseCapability | ||||||
|  * |  * | ||||||
|  * @return {PromiseCapability} A capability object contains: |  * @return {PromiseCapability} | ||||||
|  * - a Promise, resolve and reject methods. |  | ||||||
|  */ |  */ | ||||||
| function createPromiseCapability() { | function createPromiseCapability() { | ||||||
|   var capability = {}; |   const capability = Object.create(null); | ||||||
|   capability.promise = new Promise(function (resolve, reject) { |   let isSettled = false; | ||||||
|     capability.resolve = resolve; | 
 | ||||||
|     capability.reject = reject; |   Object.defineProperty(capability, 'settled', { | ||||||
|  |     get() { | ||||||
|  |       return isSettled; | ||||||
|  |     }, | ||||||
|  |   }); | ||||||
|  |   capability.promise = new Promise(function(resolve, reject) { | ||||||
|  |     capability.resolve = function(data) { | ||||||
|  |       isSettled = true; | ||||||
|  |       resolve(data); | ||||||
|  |     }; | ||||||
|  |     capability.reject = function(reason) { | ||||||
|  |       isSettled = true; | ||||||
|  |       reject(reason); | ||||||
|  |     }; | ||||||
|   }); |   }); | ||||||
|   return capability; |   return capability; | ||||||
| } | } | ||||||
|  | |||||||
| @ -64,14 +64,11 @@ describe('api', function() { | |||||||
|     it('creates pdf doc from URL', function(done) { |     it('creates pdf doc from URL', function(done) { | ||||||
|       var loadingTask = getDocument(basicApiGetDocumentParams); |       var loadingTask = getDocument(basicApiGetDocumentParams); | ||||||
| 
 | 
 | ||||||
|       var isProgressReportedResolved = false; |  | ||||||
|       var progressReportedCapability = createPromiseCapability(); |       var progressReportedCapability = createPromiseCapability(); | ||||||
| 
 |  | ||||||
|       // Attach the callback that is used to report loading progress;
 |       // Attach the callback that is used to report loading progress;
 | ||||||
|       // similarly to how viewer.js works.
 |       // similarly to how viewer.js works.
 | ||||||
|       loadingTask.onProgress = function (progressData) { |       loadingTask.onProgress = function (progressData) { | ||||||
|         if (!isProgressReportedResolved) { |         if (!progressReportedCapability.settled) { | ||||||
|           isProgressReportedResolved = true; |  | ||||||
|           progressReportedCapability.resolve(progressData); |           progressReportedCapability.resolve(progressData); | ||||||
|         } |         } | ||||||
|       }; |       }; | ||||||
| @ -183,25 +180,20 @@ describe('api', function() { | |||||||
|         function (done) { |         function (done) { | ||||||
|       var loadingTask = getDocument(buildGetDocumentParams('pr6531_1.pdf')); |       var loadingTask = getDocument(buildGetDocumentParams('pr6531_1.pdf')); | ||||||
| 
 | 
 | ||||||
|       var isPasswordNeededResolved = false; |  | ||||||
|       var passwordNeededCapability = createPromiseCapability(); |       var passwordNeededCapability = createPromiseCapability(); | ||||||
|       var isPasswordIncorrectResolved = false; |  | ||||||
|       var passwordIncorrectCapability = createPromiseCapability(); |       var passwordIncorrectCapability = createPromiseCapability(); | ||||||
| 
 |  | ||||||
|       // Attach the callback that is used to request a password;
 |       // Attach the callback that is used to request a password;
 | ||||||
|       // similarly to how viewer.js handles passwords.
 |       // similarly to how viewer.js handles passwords.
 | ||||||
|       loadingTask.onPassword = function (updatePassword, reason) { |       loadingTask.onPassword = function (updatePassword, reason) { | ||||||
|         if (reason === PasswordResponses.NEED_PASSWORD && |         if (reason === PasswordResponses.NEED_PASSWORD && | ||||||
|             !isPasswordNeededResolved) { |             !passwordNeededCapability.settled) { | ||||||
|           isPasswordNeededResolved = true; |  | ||||||
|           passwordNeededCapability.resolve(); |           passwordNeededCapability.resolve(); | ||||||
| 
 | 
 | ||||||
|           updatePassword('qwerty'); // Provide an incorrect password.
 |           updatePassword('qwerty'); // Provide an incorrect password.
 | ||||||
|           return; |           return; | ||||||
|         } |         } | ||||||
|         if (reason === PasswordResponses.INCORRECT_PASSWORD && |         if (reason === PasswordResponses.INCORRECT_PASSWORD && | ||||||
|             !isPasswordIncorrectResolved) { |             !passwordIncorrectCapability.settled) { | ||||||
|           isPasswordIncorrectResolved = true; |  | ||||||
|           passwordIncorrectCapability.resolve(); |           passwordIncorrectCapability.resolve(); | ||||||
| 
 | 
 | ||||||
|           updatePassword('asdfasdf'); // Provide the correct password.
 |           updatePassword('asdfasdf'); // Provide the correct password.
 | ||||||
|  | |||||||
| @ -14,9 +14,10 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| import { | import { | ||||||
|   bytesToString, createValidAbsoluteUrl, getInheritableProperty, isArrayBuffer, |   bytesToString, createPromiseCapability, createValidAbsoluteUrl, | ||||||
|   isBool, isEmptyObj, isNum, isSameOrigin, isSpace, isString, log2, |   getInheritableProperty, isArrayBuffer, isBool, isEmptyObj, isNum, | ||||||
|   ReadableStream, removeNullCharacters, stringToBytes, stringToPDFString, URL |   isSameOrigin, isSpace, isString, log2, ReadableStream, removeNullCharacters, | ||||||
|  |   stringToBytes, stringToPDFString, URL | ||||||
| } from '../../src/shared/util'; | } from '../../src/shared/util'; | ||||||
| import { Dict, Ref } from '../../src/core/primitives'; | import { Dict, Ref } from '../../src/core/primitives'; | ||||||
| import { XRefMock } from './test_utils'; | import { XRefMock } from './test_utils'; | ||||||
| @ -384,4 +385,35 @@ describe('util', function() { | |||||||
|         .toEqual(null); |         .toEqual(null); | ||||||
|     }); |     }); | ||||||
|   }); |   }); | ||||||
|  | 
 | ||||||
|  |   describe('createPromiseCapability', function() { | ||||||
|  |     it('should resolve with correct data', function(done) { | ||||||
|  |       const promiseCapability = createPromiseCapability(); | ||||||
|  |       expect(promiseCapability.settled).toEqual(false); | ||||||
|  | 
 | ||||||
|  |       promiseCapability.resolve({ test: 'abc', }); | ||||||
|  | 
 | ||||||
|  |       promiseCapability.promise.then(function(data) { | ||||||
|  |         expect(promiseCapability.settled).toEqual(true); | ||||||
|  | 
 | ||||||
|  |         expect(data).toEqual({ test: 'abc', }); | ||||||
|  |         done(); | ||||||
|  |       }, done.fail); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     it('should reject with correct reason', function(done) { | ||||||
|  |       const promiseCapability = createPromiseCapability(); | ||||||
|  |       expect(promiseCapability.settled).toEqual(false); | ||||||
|  | 
 | ||||||
|  |       promiseCapability.reject(new Error('reason')); | ||||||
|  | 
 | ||||||
|  |       promiseCapability.promise.then(done.fail, function(reason) { | ||||||
|  |         expect(promiseCapability.settled).toEqual(true); | ||||||
|  | 
 | ||||||
|  |         expect(reason instanceof Error).toEqual(true); | ||||||
|  |         expect(reason.message).toEqual('reason'); | ||||||
|  |         done(); | ||||||
|  |       }); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
| }); | }); | ||||||
|  | |||||||
| @ -395,8 +395,7 @@ class BaseViewer { | |||||||
|       }); |       }); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     let isOnePageRenderedResolved = false; |     const onePageRenderedCapability = createPromiseCapability(); | ||||||
|     let onePageRenderedCapability = createPromiseCapability(); |  | ||||||
|     this.onePageRendered = onePageRenderedCapability.promise; |     this.onePageRendered = onePageRenderedCapability.promise; | ||||||
| 
 | 
 | ||||||
|     let bindOnAfterAndBeforeDraw = (pageView) => { |     let bindOnAfterAndBeforeDraw = (pageView) => { | ||||||
| @ -407,8 +406,7 @@ class BaseViewer { | |||||||
|         this._buffer.push(pageView); |         this._buffer.push(pageView); | ||||||
|       }; |       }; | ||||||
|       pageView.onAfterDraw = () => { |       pageView.onAfterDraw = () => { | ||||||
|         if (!isOnePageRenderedResolved) { |         if (!onePageRenderedCapability.settled) { | ||||||
|           isOnePageRenderedResolved = true; |  | ||||||
|           onePageRenderedCapability.resolve(); |           onePageRenderedCapability.resolve(); | ||||||
|         } |         } | ||||||
|       }; |       }; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user