mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
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
4 changed files with 62 additions and 27 deletions
|
@ -64,14 +64,11 @@ describe('api', function() {
|
|||
it('creates pdf doc from URL', function(done) {
|
||||
var loadingTask = getDocument(basicApiGetDocumentParams);
|
||||
|
||||
var isProgressReportedResolved = false;
|
||||
var progressReportedCapability = createPromiseCapability();
|
||||
|
||||
// Attach the callback that is used to report loading progress;
|
||||
// similarly to how viewer.js works.
|
||||
loadingTask.onProgress = function (progressData) {
|
||||
if (!isProgressReportedResolved) {
|
||||
isProgressReportedResolved = true;
|
||||
if (!progressReportedCapability.settled) {
|
||||
progressReportedCapability.resolve(progressData);
|
||||
}
|
||||
};
|
||||
|
@ -183,25 +180,20 @@ describe('api', function() {
|
|||
function (done) {
|
||||
var loadingTask = getDocument(buildGetDocumentParams('pr6531_1.pdf'));
|
||||
|
||||
var isPasswordNeededResolved = false;
|
||||
var passwordNeededCapability = createPromiseCapability();
|
||||
var isPasswordIncorrectResolved = false;
|
||||
var passwordIncorrectCapability = createPromiseCapability();
|
||||
|
||||
// Attach the callback that is used to request a password;
|
||||
// similarly to how viewer.js handles passwords.
|
||||
loadingTask.onPassword = function (updatePassword, reason) {
|
||||
if (reason === PasswordResponses.NEED_PASSWORD &&
|
||||
!isPasswordNeededResolved) {
|
||||
isPasswordNeededResolved = true;
|
||||
!passwordNeededCapability.settled) {
|
||||
passwordNeededCapability.resolve();
|
||||
|
||||
updatePassword('qwerty'); // Provide an incorrect password.
|
||||
return;
|
||||
}
|
||||
if (reason === PasswordResponses.INCORRECT_PASSWORD &&
|
||||
!isPasswordIncorrectResolved) {
|
||||
isPasswordIncorrectResolved = true;
|
||||
!passwordIncorrectCapability.settled) {
|
||||
passwordIncorrectCapability.resolve();
|
||||
|
||||
updatePassword('asdfasdf'); // Provide the correct password.
|
||||
|
|
|
@ -14,9 +14,10 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
bytesToString, createValidAbsoluteUrl, getInheritableProperty, isArrayBuffer,
|
||||
isBool, isEmptyObj, isNum, isSameOrigin, isSpace, isString, log2,
|
||||
ReadableStream, removeNullCharacters, stringToBytes, stringToPDFString, URL
|
||||
bytesToString, createPromiseCapability, createValidAbsoluteUrl,
|
||||
getInheritableProperty, isArrayBuffer, isBool, isEmptyObj, isNum,
|
||||
isSameOrigin, isSpace, isString, log2, ReadableStream, removeNullCharacters,
|
||||
stringToBytes, stringToPDFString, URL
|
||||
} from '../../src/shared/util';
|
||||
import { Dict, Ref } from '../../src/core/primitives';
|
||||
import { XRefMock } from './test_utils';
|
||||
|
@ -384,4 +385,35 @@ describe('util', function() {
|
|||
.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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue