mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Change the createPromiseCapability
helper function into a PromiseCapability
class
This is not only slightly more compact, but it also simplifies the handling of the `settled` getter.
This commit is contained in:
parent
f9c2a8d437
commit
317abd6d07
24 changed files with 117 additions and 122 deletions
|
@ -14,7 +14,7 @@
|
|||
*/
|
||||
|
||||
import { arrayBuffersToBytes, MissingDataException } from "./core_utils.js";
|
||||
import { assert, createPromiseCapability } from "../shared/util.js";
|
||||
import { assert, PromiseCapability } from "../shared/util.js";
|
||||
import { Stream } from "./stream.js";
|
||||
|
||||
class ChunkedStream extends Stream {
|
||||
|
@ -275,7 +275,7 @@ class ChunkedStreamManager {
|
|||
this.progressiveDataLength = 0;
|
||||
this.aborted = false;
|
||||
|
||||
this._loadedStreamCapability = createPromiseCapability();
|
||||
this._loadedStreamCapability = new PromiseCapability();
|
||||
}
|
||||
|
||||
sendRequest(begin, end) {
|
||||
|
@ -349,7 +349,7 @@ class ChunkedStreamManager {
|
|||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const capability = createPromiseCapability();
|
||||
const capability = new PromiseCapability();
|
||||
this._promisesByRequest.set(requestId, capability);
|
||||
|
||||
const chunksToRequest = [];
|
||||
|
|
|
@ -18,7 +18,6 @@ import {
|
|||
AbortException,
|
||||
assert,
|
||||
CMapCompressionType,
|
||||
createPromiseCapability,
|
||||
FONT_IDENTITY_MATRIX,
|
||||
FormatError,
|
||||
IDENTITY_MATRIX,
|
||||
|
@ -26,6 +25,7 @@ import {
|
|||
isArrayEqual,
|
||||
normalizeUnicode,
|
||||
OPS,
|
||||
PromiseCapability,
|
||||
shadow,
|
||||
stringToPDFString,
|
||||
TextRenderingMode,
|
||||
|
@ -1253,7 +1253,7 @@ class PartialEvaluator {
|
|||
return this.fontCache.get(font.cacheKey);
|
||||
}
|
||||
|
||||
const fontCapability = createPromiseCapability();
|
||||
const fontCapability = new PromiseCapability();
|
||||
|
||||
let preEvaluatedFont;
|
||||
try {
|
||||
|
|
|
@ -16,12 +16,12 @@
|
|||
import {
|
||||
AbortException,
|
||||
assert,
|
||||
createPromiseCapability,
|
||||
getVerbosityLevel,
|
||||
info,
|
||||
InvalidPDFException,
|
||||
MissingPDFException,
|
||||
PasswordException,
|
||||
PromiseCapability,
|
||||
setVerbosityLevel,
|
||||
stringToPDFString,
|
||||
UnexpectedResponseException,
|
||||
|
@ -46,7 +46,7 @@ class WorkerTask {
|
|||
constructor(name) {
|
||||
this.name = name;
|
||||
this.terminated = false;
|
||||
this._capability = createPromiseCapability();
|
||||
this._capability = new PromiseCapability();
|
||||
}
|
||||
|
||||
get finished() {
|
||||
|
@ -228,7 +228,7 @@ class WorkerMessageHandler {
|
|||
password,
|
||||
rangeChunkSize,
|
||||
};
|
||||
const pdfManagerCapability = createPromiseCapability();
|
||||
const pdfManagerCapability = new PromiseCapability();
|
||||
let newPdfManager;
|
||||
|
||||
if (data) {
|
||||
|
|
|
@ -21,7 +21,6 @@ import {
|
|||
AbortException,
|
||||
AnnotationMode,
|
||||
assert,
|
||||
createPromiseCapability,
|
||||
getVerbosityLevel,
|
||||
info,
|
||||
InvalidPDFException,
|
||||
|
@ -29,6 +28,7 @@ import {
|
|||
MAX_IMAGE_SIZE_TO_CACHE,
|
||||
MissingPDFException,
|
||||
PasswordException,
|
||||
PromiseCapability,
|
||||
RenderingIntentFlag,
|
||||
setVerbosityLevel,
|
||||
shadow,
|
||||
|
@ -588,7 +588,7 @@ class PDFDocumentLoadingTask {
|
|||
static #docId = 0;
|
||||
|
||||
constructor() {
|
||||
this._capability = createPromiseCapability();
|
||||
this._capability = new PromiseCapability();
|
||||
this._transport = null;
|
||||
this._worker = null;
|
||||
|
||||
|
@ -675,7 +675,7 @@ class PDFDataRangeTransport {
|
|||
this._progressListeners = [];
|
||||
this._progressiveReadListeners = [];
|
||||
this._progressiveDoneListeners = [];
|
||||
this._readyCapability = createPromiseCapability();
|
||||
this._readyCapability = new PromiseCapability();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1460,7 +1460,7 @@ class PDFPageProxy {
|
|||
// If there's no displayReadyCapability yet, then the operatorList
|
||||
// was never requested before. Make the request and create the promise.
|
||||
if (!intentState.displayReadyCapability) {
|
||||
intentState.displayReadyCapability = createPromiseCapability();
|
||||
intentState.displayReadyCapability = new PromiseCapability();
|
||||
intentState.operatorList = {
|
||||
fnArray: [],
|
||||
argsArray: [],
|
||||
|
@ -1578,7 +1578,7 @@ class PDFPageProxy {
|
|||
if (!intentState.opListReadCapability) {
|
||||
opListTask = Object.create(null);
|
||||
opListTask.operatorListChanged = operatorListChanged;
|
||||
intentState.opListReadCapability = createPromiseCapability();
|
||||
intentState.opListReadCapability = new PromiseCapability();
|
||||
(intentState.renderTasks ||= new Set()).add(opListTask);
|
||||
intentState.operatorList = {
|
||||
fnArray: [],
|
||||
|
@ -2054,7 +2054,7 @@ class PDFWorker {
|
|||
this.destroyed = false;
|
||||
this.verbosity = verbosity;
|
||||
|
||||
this._readyCapability = createPromiseCapability();
|
||||
this._readyCapability = new PromiseCapability();
|
||||
this._port = null;
|
||||
this._webWorker = null;
|
||||
this._messageHandler = null;
|
||||
|
@ -2388,7 +2388,7 @@ class WorkerTransport {
|
|||
this._networkStream = networkStream;
|
||||
this._fullReader = null;
|
||||
this._lastProgress = null;
|
||||
this.downloadInfoCapability = createPromiseCapability();
|
||||
this.downloadInfoCapability = new PromiseCapability();
|
||||
|
||||
this.setupMessageHandler();
|
||||
|
||||
|
@ -2487,7 +2487,7 @@ class WorkerTransport {
|
|||
}
|
||||
|
||||
this.destroyed = true;
|
||||
this.destroyCapability = createPromiseCapability();
|
||||
this.destroyCapability = new PromiseCapability();
|
||||
|
||||
if (this._passwordCapability) {
|
||||
this._passwordCapability.reject(
|
||||
|
@ -2581,7 +2581,7 @@ class WorkerTransport {
|
|||
});
|
||||
|
||||
messageHandler.on("ReaderHeadersReady", data => {
|
||||
const headersCapability = createPromiseCapability();
|
||||
const headersCapability = new PromiseCapability();
|
||||
const fullReader = this._fullReader;
|
||||
fullReader.headersReady.then(() => {
|
||||
// If stream or range are disabled, it's our only way to report
|
||||
|
@ -2696,7 +2696,7 @@ class WorkerTransport {
|
|||
});
|
||||
|
||||
messageHandler.on("PasswordRequest", exception => {
|
||||
this._passwordCapability = createPromiseCapability();
|
||||
this._passwordCapability = new PromiseCapability();
|
||||
|
||||
if (loadingTask.onPassword) {
|
||||
const updatePassword = password => {
|
||||
|
@ -3117,7 +3117,7 @@ class PDFObjects {
|
|||
return obj;
|
||||
}
|
||||
return (this.#objs[objId] = {
|
||||
capability: createPromiseCapability(),
|
||||
capability: new PromiseCapability(),
|
||||
data: null,
|
||||
});
|
||||
}
|
||||
|
@ -3277,7 +3277,7 @@ class InternalRenderTask {
|
|||
this._useRequestAnimationFrame =
|
||||
useRequestAnimationFrame === true && typeof window !== "undefined";
|
||||
this.cancelled = false;
|
||||
this.capability = createPromiseCapability();
|
||||
this.capability = new PromiseCapability();
|
||||
this.task = new RenderTask(this);
|
||||
// caching this-bound methods
|
||||
this._cancelBound = this.cancel.bind(this);
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
import {
|
||||
AbortException,
|
||||
assert,
|
||||
createPromiseCapability,
|
||||
PromiseCapability,
|
||||
warn,
|
||||
} from "../shared/util.js";
|
||||
import {
|
||||
|
@ -118,7 +118,7 @@ class PDFFetchStreamReader {
|
|||
const source = stream.source;
|
||||
this._withCredentials = source.withCredentials || false;
|
||||
this._contentLength = source.length;
|
||||
this._headersCapability = createPromiseCapability();
|
||||
this._headersCapability = new PromiseCapability();
|
||||
this._disableRange = source.disableRange || false;
|
||||
this._rangeChunkSize = source.rangeChunkSize;
|
||||
if (!this._rangeChunkSize && !this._disableRange) {
|
||||
|
@ -224,7 +224,7 @@ class PDFFetchStreamRangeReader {
|
|||
this._loaded = 0;
|
||||
const source = stream.source;
|
||||
this._withCredentials = source.withCredentials || false;
|
||||
this._readCapability = createPromiseCapability();
|
||||
this._readCapability = new PromiseCapability();
|
||||
this._isStreamingSupported = !source.disableStream;
|
||||
|
||||
this._abortController = new AbortController();
|
||||
|
|
|
@ -13,11 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
assert,
|
||||
createPromiseCapability,
|
||||
stringToBytes,
|
||||
} from "../shared/util.js";
|
||||
import { assert, PromiseCapability, stringToBytes } from "../shared/util.js";
|
||||
import {
|
||||
createResponseStatusError,
|
||||
extractFilenameFromHeader,
|
||||
|
@ -259,7 +255,7 @@ class PDFNetworkStreamFullRequestReader {
|
|||
};
|
||||
this._url = source.url;
|
||||
this._fullRequestId = manager.requestFull(args);
|
||||
this._headersReceivedCapability = createPromiseCapability();
|
||||
this._headersReceivedCapability = new PromiseCapability();
|
||||
this._disableRange = source.disableRange || false;
|
||||
this._contentLength = source.length; // Optional
|
||||
this._rangeChunkSize = source.rangeChunkSize;
|
||||
|
@ -380,7 +376,7 @@ class PDFNetworkStreamFullRequestReader {
|
|||
if (this._done) {
|
||||
return { value: undefined, done: true };
|
||||
}
|
||||
const requestCapability = createPromiseCapability();
|
||||
const requestCapability = new PromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
return requestCapability.promise;
|
||||
}
|
||||
|
@ -471,7 +467,7 @@ class PDFNetworkStreamRangeRequestReader {
|
|||
if (this._done) {
|
||||
return { value: undefined, done: true };
|
||||
}
|
||||
const requestCapability = createPromiseCapability();
|
||||
const requestCapability = new PromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
return requestCapability.promise;
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
import {
|
||||
AbortException,
|
||||
assert,
|
||||
createPromiseCapability,
|
||||
MissingPDFException,
|
||||
PromiseCapability,
|
||||
} from "../shared/util.js";
|
||||
import {
|
||||
extractFilenameFromHeader,
|
||||
|
@ -124,8 +124,8 @@ class BaseFullReader {
|
|||
this._isRangeSupported = !source.disableRange;
|
||||
|
||||
this._readableStream = null;
|
||||
this._readCapability = createPromiseCapability();
|
||||
this._headersCapability = createPromiseCapability();
|
||||
this._readCapability = new PromiseCapability();
|
||||
this._headersCapability = new PromiseCapability();
|
||||
}
|
||||
|
||||
get headersReady() {
|
||||
|
@ -159,7 +159,7 @@ class BaseFullReader {
|
|||
|
||||
const chunk = this._readableStream.read();
|
||||
if (chunk === null) {
|
||||
this._readCapability = createPromiseCapability();
|
||||
this._readCapability = new PromiseCapability();
|
||||
return this.read();
|
||||
}
|
||||
this._loaded += chunk.length;
|
||||
|
@ -226,7 +226,7 @@ class BaseRangeReader {
|
|||
this.onProgress = null;
|
||||
this._loaded = 0;
|
||||
this._readableStream = null;
|
||||
this._readCapability = createPromiseCapability();
|
||||
this._readCapability = new PromiseCapability();
|
||||
const source = stream.source;
|
||||
this._isStreamingSupported = !source.disableStream;
|
||||
}
|
||||
|
@ -246,7 +246,7 @@ class BaseRangeReader {
|
|||
|
||||
const chunk = this._readableStream.read();
|
||||
if (chunk === null) {
|
||||
this._readCapability = createPromiseCapability();
|
||||
this._readCapability = new PromiseCapability();
|
||||
return this.read();
|
||||
}
|
||||
this._loaded += chunk.length;
|
||||
|
|
|
@ -18,8 +18,8 @@
|
|||
|
||||
import {
|
||||
AbortException,
|
||||
createPromiseCapability,
|
||||
FeatureTest,
|
||||
PromiseCapability,
|
||||
Util,
|
||||
} from "../shared/util.js";
|
||||
import { deprecated, setLayerDimensions } from "./display_utils.js";
|
||||
|
@ -317,7 +317,7 @@ class TextLayerRenderTask {
|
|||
this._reader = null;
|
||||
this._textDivProperties = textDivProperties || new WeakMap();
|
||||
this._canceled = false;
|
||||
this._capability = createPromiseCapability();
|
||||
this._capability = new PromiseCapability();
|
||||
this._layoutTextParams = {
|
||||
prevFontSize: null,
|
||||
prevFontFamily: null,
|
||||
|
@ -417,7 +417,7 @@ class TextLayerRenderTask {
|
|||
* @private
|
||||
*/
|
||||
_render() {
|
||||
const capability = createPromiseCapability();
|
||||
const capability = new PromiseCapability();
|
||||
let styleCache = Object.create(null);
|
||||
|
||||
if (this._isReadableStream) {
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { assert, createPromiseCapability } from "../shared/util.js";
|
||||
import { assert, PromiseCapability } from "../shared/util.js";
|
||||
import { isPdfFile } from "./display_utils.js";
|
||||
|
||||
/** @implements {IPDFStream} */
|
||||
|
@ -235,7 +235,7 @@ class PDFDataTransportStreamReader {
|
|||
if (this._done) {
|
||||
return { value: undefined, done: true };
|
||||
}
|
||||
const requestCapability = createPromiseCapability();
|
||||
const requestCapability = new PromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
return requestCapability.promise;
|
||||
}
|
||||
|
@ -300,7 +300,7 @@ class PDFDataTransportStreamRangeReader {
|
|||
if (this._done) {
|
||||
return { value: undefined, done: true };
|
||||
}
|
||||
const requestCapability = createPromiseCapability();
|
||||
const requestCapability = new PromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
return requestCapability.promise;
|
||||
}
|
||||
|
|
|
@ -30,7 +30,6 @@ import {
|
|||
AnnotationEditorType,
|
||||
AnnotationMode,
|
||||
CMapCompressionType,
|
||||
createPromiseCapability,
|
||||
createValidAbsoluteUrl,
|
||||
FeatureTest,
|
||||
InvalidPDFException,
|
||||
|
@ -39,6 +38,7 @@ import {
|
|||
OPS,
|
||||
PasswordResponses,
|
||||
PermissionFlag,
|
||||
PromiseCapability,
|
||||
shadow,
|
||||
UnexpectedResponseException,
|
||||
Util,
|
||||
|
@ -88,7 +88,6 @@ export {
|
|||
AnnotationMode,
|
||||
build,
|
||||
CMapCompressionType,
|
||||
createPromiseCapability,
|
||||
createValidAbsoluteUrl,
|
||||
FeatureTest,
|
||||
getDocument,
|
||||
|
@ -109,6 +108,7 @@ export {
|
|||
PDFWorker,
|
||||
PermissionFlag,
|
||||
PixelsPerInch,
|
||||
PromiseCapability,
|
||||
RenderingCancelledException,
|
||||
renderTextLayer,
|
||||
setLayerDimensions,
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
import {
|
||||
AbortException,
|
||||
assert,
|
||||
createPromiseCapability,
|
||||
MissingPDFException,
|
||||
PasswordException,
|
||||
PromiseCapability,
|
||||
UnexpectedResponseException,
|
||||
UnknownErrorException,
|
||||
unreachable,
|
||||
|
@ -190,7 +190,7 @@ class MessageHandler {
|
|||
*/
|
||||
sendWithPromise(actionName, data, transfers) {
|
||||
const callbackId = this.callbackId++;
|
||||
const capability = createPromiseCapability();
|
||||
const capability = new PromiseCapability();
|
||||
this.callbackCapabilities[callbackId] = capability;
|
||||
try {
|
||||
this.comObj.postMessage(
|
||||
|
@ -228,7 +228,7 @@ class MessageHandler {
|
|||
return new ReadableStream(
|
||||
{
|
||||
start: controller => {
|
||||
const startCapability = createPromiseCapability();
|
||||
const startCapability = new PromiseCapability();
|
||||
this.streamControllers[streamId] = {
|
||||
controller,
|
||||
startCall: startCapability,
|
||||
|
@ -252,7 +252,7 @@ class MessageHandler {
|
|||
},
|
||||
|
||||
pull: controller => {
|
||||
const pullCapability = createPromiseCapability();
|
||||
const pullCapability = new PromiseCapability();
|
||||
this.streamControllers[streamId].pullCall = pullCapability;
|
||||
comObj.postMessage({
|
||||
sourceName,
|
||||
|
@ -268,7 +268,7 @@ class MessageHandler {
|
|||
|
||||
cancel: reason => {
|
||||
assert(reason instanceof Error, "cancel must have a valid reason");
|
||||
const cancelCapability = createPromiseCapability();
|
||||
const cancelCapability = new PromiseCapability();
|
||||
this.streamControllers[streamId].cancelCall = cancelCapability;
|
||||
this.streamControllers[streamId].isClosed = true;
|
||||
comObj.postMessage({
|
||||
|
@ -305,7 +305,7 @@ class MessageHandler {
|
|||
// so when it changes from positive to negative,
|
||||
// set ready as unresolved promise.
|
||||
if (lastDesiredSize > 0 && this.desiredSize <= 0) {
|
||||
this.sinkCapability = createPromiseCapability();
|
||||
this.sinkCapability = new PromiseCapability();
|
||||
this.ready = this.sinkCapability.promise;
|
||||
}
|
||||
comObj.postMessage(
|
||||
|
@ -349,7 +349,7 @@ class MessageHandler {
|
|||
});
|
||||
},
|
||||
|
||||
sinkCapability: createPromiseCapability(),
|
||||
sinkCapability: new PromiseCapability(),
|
||||
onPull: null,
|
||||
onCancel: null,
|
||||
isCancelled: false,
|
||||
|
|
|
@ -975,42 +975,41 @@ function getModificationDate(date = new Date()) {
|
|||
return buffer.join("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Promise Capability object.
|
||||
*
|
||||
* @typedef {Object} PromiseCapability
|
||||
* @property {Promise<any>} promise - A Promise object.
|
||||
* @property {boolean} settled - If the Promise has been fulfilled/rejected.
|
||||
* @property {function} resolve - Fulfills the Promise.
|
||||
* @property {function} reject - Rejects the Promise.
|
||||
*/
|
||||
class PromiseCapability {
|
||||
#settled = false;
|
||||
|
||||
/**
|
||||
* Creates a promise capability object.
|
||||
* @alias createPromiseCapability
|
||||
*
|
||||
* @returns {PromiseCapability}
|
||||
*/
|
||||
function createPromiseCapability() {
|
||||
const capability = Object.create(null);
|
||||
let isSettled = false;
|
||||
constructor() {
|
||||
/**
|
||||
* @type {Promise<any>} The Promise object.
|
||||
*/
|
||||
this.promise = new Promise((resolve, reject) => {
|
||||
/**
|
||||
* @type {function} Fulfills the Promise.
|
||||
*/
|
||||
this.resolve = data => {
|
||||
this.#settled = true;
|
||||
resolve(data);
|
||||
};
|
||||
|
||||
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;
|
||||
/**
|
||||
* @type {function} Rejects the Promise.
|
||||
*/
|
||||
this.reject = reason => {
|
||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
||||
assert(reason instanceof Error, 'Expected valid "reason" argument.');
|
||||
}
|
||||
this.#settled = true;
|
||||
reject(reason);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {boolean} If the Promise has been fulfilled/rejected.
|
||||
*/
|
||||
get settled() {
|
||||
return this.#settled;
|
||||
}
|
||||
}
|
||||
|
||||
let NormalizeRegex = null;
|
||||
|
@ -1052,7 +1051,6 @@ export {
|
|||
BASELINE_FACTOR,
|
||||
bytesToString,
|
||||
CMapCompressionType,
|
||||
createPromiseCapability,
|
||||
createValidAbsoluteUrl,
|
||||
DocumentActionEventType,
|
||||
FeatureTest,
|
||||
|
@ -1078,6 +1076,7 @@ export {
|
|||
PasswordException,
|
||||
PasswordResponses,
|
||||
PermissionFlag,
|
||||
PromiseCapability,
|
||||
RenderingIntentFlag,
|
||||
setVerbosityLevel,
|
||||
shadow,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue