mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Add support for async
/await
using Babel
For proof-of-concept, this patch converts a couple of `Promise` returning methods to use `async` instead. Please note that the `generic` build, based on this patch, has been successfully testing in IE11 (i.e. the viewer loads and nothing is obviously broken). Being able to use modern JavaScript features like `async`/`await` is a huge plus, but there's one (obvious) side-effect: The size of the built files will increase slightly (unless `SKIP_BABEL == true`). That's unavoidable, but seems like a small price to pay in the grand scheme of things. Finally, note that the `chromium` build target was changed to no longer skip Babel translation, since the Chrome extension still supports version `49` of the browser (where native `async` support isn't available).
This commit is contained in:
parent
4ea663aa8a
commit
099ed08852
13 changed files with 159 additions and 179 deletions
|
@ -131,19 +131,17 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
this.options = options || DefaultPartialEvaluatorOptions;
|
||||
this.pdfFunctionFactory = pdfFunctionFactory;
|
||||
|
||||
this.fetchBuiltInCMap = (name) => {
|
||||
this.fetchBuiltInCMap = async (name) => {
|
||||
if (this.builtInCMapCache.has(name)) {
|
||||
return Promise.resolve(this.builtInCMapCache.get(name));
|
||||
return this.builtInCMapCache.get(name);
|
||||
}
|
||||
return this.handler.sendWithPromise('FetchBuiltInCMap', {
|
||||
name,
|
||||
}).then((data) => {
|
||||
if (data.compressionType !== CMapCompressionType.NONE) {
|
||||
// Given the size of uncompressed CMaps, only cache compressed ones.
|
||||
this.builtInCMapCache.set(name, data);
|
||||
}
|
||||
return data;
|
||||
});
|
||||
const data = await this.handler.sendWithPromise('FetchBuiltInCMap',
|
||||
{ name, });
|
||||
if (data.compressionType !== CMapCompressionType.NONE) {
|
||||
// Given the size of uncompressed CMaps, only cache compressed ones.
|
||||
this.builtInCMapCache.set(name, data);
|
||||
}
|
||||
return data;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1497,29 +1497,23 @@ var XRef = (function XRefClosure() {
|
|||
return xrefEntry;
|
||||
},
|
||||
|
||||
fetchIfRefAsync: function XRef_fetchIfRefAsync(obj, suppressEncryption) {
|
||||
async fetchIfRefAsync(obj, suppressEncryption) {
|
||||
if (!isRef(obj)) {
|
||||
return Promise.resolve(obj);
|
||||
return obj;
|
||||
}
|
||||
return this.fetchAsync(obj, suppressEncryption);
|
||||
},
|
||||
|
||||
fetchAsync: function XRef_fetchAsync(ref, suppressEncryption) {
|
||||
var streamManager = this.stream.manager;
|
||||
var xref = this;
|
||||
return new Promise(function tryFetch(resolve, reject) {
|
||||
try {
|
||||
resolve(xref.fetch(ref, suppressEncryption));
|
||||
} catch (e) {
|
||||
if (e instanceof MissingDataException) {
|
||||
streamManager.requestRange(e.begin, e.end).then(function () {
|
||||
tryFetch(resolve, reject);
|
||||
}, reject);
|
||||
return;
|
||||
}
|
||||
reject(e);
|
||||
async fetchAsync(ref, suppressEncryption) {
|
||||
try {
|
||||
return this.fetch(ref, suppressEncryption);
|
||||
} catch (ex) {
|
||||
if (!(ex instanceof MissingDataException)) {
|
||||
throw ex;
|
||||
}
|
||||
});
|
||||
await this.pdfManager.requestRange(ex.begin, ex.end);
|
||||
return this.fetchAsync(ref, suppressEncryption);
|
||||
}
|
||||
},
|
||||
|
||||
getCatalogObj: function XRef_getCatalogObj() {
|
||||
|
|
|
@ -72,7 +72,7 @@ class BasePdfManager {
|
|||
return this.pdfDocument.cleanup();
|
||||
}
|
||||
|
||||
ensure(obj, prop, args) {
|
||||
async ensure(obj, prop, args) {
|
||||
unreachable('Abstract method `ensure` called');
|
||||
}
|
||||
|
||||
|
@ -111,15 +111,12 @@ class LocalPdfManager extends BasePdfManager {
|
|||
this._loadedStreamPromise = Promise.resolve(stream);
|
||||
}
|
||||
|
||||
ensure(obj, prop, args) {
|
||||
return new Promise(function(resolve) {
|
||||
const value = obj[prop];
|
||||
if (typeof value === 'function') {
|
||||
resolve(value.apply(obj, args));
|
||||
} else {
|
||||
resolve(value);
|
||||
}
|
||||
});
|
||||
async ensure(obj, prop, args) {
|
||||
const value = obj[prop];
|
||||
if (typeof value === 'function') {
|
||||
return value.apply(obj, args);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
requestRange(begin, end) {
|
||||
|
@ -155,30 +152,20 @@ class NetworkPdfManager extends BasePdfManager {
|
|||
this.pdfDocument = new PDFDocument(this, this.streamManager.getStream());
|
||||
}
|
||||
|
||||
ensure(obj, prop, args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let ensureHelper = () => {
|
||||
try {
|
||||
const value = obj[prop];
|
||||
let result;
|
||||
if (typeof value === 'function') {
|
||||
result = value.apply(obj, args);
|
||||
} else {
|
||||
result = value;
|
||||
}
|
||||
resolve(result);
|
||||
} catch (ex) {
|
||||
if (!(ex instanceof MissingDataException)) {
|
||||
reject(ex);
|
||||
return;
|
||||
}
|
||||
this.streamManager.requestRange(ex.begin, ex.end)
|
||||
.then(ensureHelper, reject);
|
||||
}
|
||||
};
|
||||
|
||||
ensureHelper();
|
||||
});
|
||||
async ensure(obj, prop, args) {
|
||||
try {
|
||||
const value = obj[prop];
|
||||
if (typeof value === 'function') {
|
||||
return value.apply(obj, args);
|
||||
}
|
||||
return value;
|
||||
} catch (ex) {
|
||||
if (!(ex instanceof MissingDataException)) {
|
||||
throw ex;
|
||||
}
|
||||
await this.requestRange(ex.begin, ex.end);
|
||||
return this.ensure(obj, prop, args);
|
||||
}
|
||||
}
|
||||
|
||||
requestRange(begin, end) {
|
||||
|
|
|
@ -152,23 +152,21 @@ class PDFFetchStreamReader {
|
|||
return this._isStreamingSupported;
|
||||
}
|
||||
|
||||
read() {
|
||||
return this._headersCapability.promise.then(() => {
|
||||
return this._reader.read().then(({ value, done, }) => {
|
||||
if (done) {
|
||||
return Promise.resolve({ value, done, });
|
||||
}
|
||||
this._loaded += value.byteLength;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({
|
||||
loaded: this._loaded,
|
||||
total: this._contentLength,
|
||||
});
|
||||
}
|
||||
let buffer = new Uint8Array(value).buffer;
|
||||
return Promise.resolve({ value: buffer, done: false, });
|
||||
async read() {
|
||||
await this._headersCapability.promise;
|
||||
const { value, done, } = await this._reader.read();
|
||||
if (done) {
|
||||
return { value, done, };
|
||||
}
|
||||
this._loaded += value.byteLength;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({
|
||||
loaded: this._loaded,
|
||||
total: this._contentLength,
|
||||
});
|
||||
});
|
||||
}
|
||||
let buffer = new Uint8Array(value).buffer;
|
||||
return { value: buffer, done: false, };
|
||||
}
|
||||
|
||||
cancel(reason) {
|
||||
|
@ -223,20 +221,18 @@ class PDFFetchStreamRangeReader {
|
|||
return this._isStreamingSupported;
|
||||
}
|
||||
|
||||
read() {
|
||||
return this._readCapability.promise.then(() => {
|
||||
return this._reader.read().then(({ value, done, }) => {
|
||||
if (done) {
|
||||
return Promise.resolve({ value, done, });
|
||||
}
|
||||
this._loaded += value.byteLength;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({ loaded: this._loaded, });
|
||||
}
|
||||
let buffer = new Uint8Array(value).buffer;
|
||||
return Promise.resolve({ value: buffer, done: false, });
|
||||
});
|
||||
});
|
||||
async read() {
|
||||
await this._readCapability.promise;
|
||||
const { value, done, } = await this._reader.read();
|
||||
if (done) {
|
||||
return { value, done, };
|
||||
}
|
||||
this._loaded += value.byteLength;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({ loaded: this._loaded, });
|
||||
}
|
||||
let buffer = new Uint8Array(value).buffer;
|
||||
return { value: buffer, done: false, };
|
||||
}
|
||||
|
||||
cancel(reason) {
|
||||
|
|
|
@ -453,16 +453,16 @@ PDFNetworkStreamFullRequestReader.prototype = {
|
|||
return this._headersReceivedCapability.promise;
|
||||
},
|
||||
|
||||
read: function PDFNetworkStreamFullRequestReader_read() {
|
||||
async read() {
|
||||
if (this._storedError) {
|
||||
return Promise.reject(this._storedError);
|
||||
throw this._storedError;
|
||||
}
|
||||
if (this._cachedChunks.length > 0) {
|
||||
var chunk = this._cachedChunks.shift();
|
||||
return Promise.resolve({ value: chunk, done: false, });
|
||||
return { value: chunk, done: false, };
|
||||
}
|
||||
if (this._done) {
|
||||
return Promise.resolve({ value: undefined, done: true, });
|
||||
return { value: undefined, done: true, };
|
||||
}
|
||||
var requestCapability = createPromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
|
@ -534,14 +534,14 @@ PDFNetworkStreamRangeRequestReader.prototype = {
|
|||
return false; // TODO allow progressive range bytes loading
|
||||
},
|
||||
|
||||
read: function PDFNetworkStreamRangeRequestReader_read() {
|
||||
async read() {
|
||||
if (this._queuedChunk !== null) {
|
||||
var chunk = this._queuedChunk;
|
||||
this._queuedChunk = null;
|
||||
return Promise.resolve({ value: chunk, done: false, });
|
||||
return { value: chunk, done: false, };
|
||||
}
|
||||
if (this._done) {
|
||||
return Promise.resolve({ value: undefined, done: true, });
|
||||
return { value: undefined, done: true, };
|
||||
}
|
||||
var requestCapability = createPromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
|
|
|
@ -131,31 +131,30 @@ class BaseFullReader {
|
|||
return this._isStreamingSupported;
|
||||
}
|
||||
|
||||
read() {
|
||||
return this._readCapability.promise.then(() => {
|
||||
if (this._done) {
|
||||
return Promise.resolve({ value: undefined, done: true, });
|
||||
}
|
||||
if (this._storedError) {
|
||||
return Promise.reject(this._storedError);
|
||||
}
|
||||
async read() {
|
||||
await this._readCapability.promise;
|
||||
if (this._done) {
|
||||
return { value: undefined, done: true, };
|
||||
}
|
||||
if (this._storedError) {
|
||||
throw this._storedError;
|
||||
}
|
||||
|
||||
let chunk = this._readableStream.read();
|
||||
if (chunk === null) {
|
||||
this._readCapability = createPromiseCapability();
|
||||
return this.read();
|
||||
}
|
||||
this._loaded += chunk.length;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({
|
||||
loaded: this._loaded,
|
||||
total: this._contentLength,
|
||||
});
|
||||
}
|
||||
// Ensure that `read()` method returns ArrayBuffer.
|
||||
let buffer = new Uint8Array(chunk).buffer;
|
||||
return Promise.resolve({ value: buffer, done: false, });
|
||||
});
|
||||
let chunk = this._readableStream.read();
|
||||
if (chunk === null) {
|
||||
this._readCapability = createPromiseCapability();
|
||||
return this.read();
|
||||
}
|
||||
this._loaded += chunk.length;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({
|
||||
loaded: this._loaded,
|
||||
total: this._contentLength,
|
||||
});
|
||||
}
|
||||
// Ensure that `read()` method returns ArrayBuffer.
|
||||
let buffer = new Uint8Array(chunk).buffer;
|
||||
return { value: buffer, done: false, };
|
||||
}
|
||||
|
||||
cancel(reason) {
|
||||
|
@ -220,28 +219,27 @@ class BaseRangeReader {
|
|||
return this._isStreamingSupported;
|
||||
}
|
||||
|
||||
read() {
|
||||
return this._readCapability.promise.then(() => {
|
||||
if (this._done) {
|
||||
return Promise.resolve({ value: undefined, done: true, });
|
||||
}
|
||||
if (this._storedError) {
|
||||
return Promise.reject(this._storedError);
|
||||
}
|
||||
async read() {
|
||||
await this._readCapability.promise;
|
||||
if (this._done) {
|
||||
return { value: undefined, done: true, };
|
||||
}
|
||||
if (this._storedError) {
|
||||
throw this._storedError;
|
||||
}
|
||||
|
||||
let chunk = this._readableStream.read();
|
||||
if (chunk === null) {
|
||||
this._readCapability = createPromiseCapability();
|
||||
return this.read();
|
||||
}
|
||||
this._loaded += chunk.length;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({ loaded: this._loaded, });
|
||||
}
|
||||
// Ensure that `read()` method returns ArrayBuffer.
|
||||
let buffer = new Uint8Array(chunk).buffer;
|
||||
return Promise.resolve({ value: buffer, done: false, });
|
||||
});
|
||||
let chunk = this._readableStream.read();
|
||||
if (chunk === null) {
|
||||
this._readCapability = createPromiseCapability();
|
||||
return this.read();
|
||||
}
|
||||
this._loaded += chunk.length;
|
||||
if (this.onProgress) {
|
||||
this.onProgress({ loaded: this._loaded, });
|
||||
}
|
||||
// Ensure that `read()` method returns ArrayBuffer.
|
||||
let buffer = new Uint8Array(chunk).buffer;
|
||||
return { value: buffer, done: false, };
|
||||
}
|
||||
|
||||
cancel(reason) {
|
||||
|
|
|
@ -160,13 +160,13 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
|||
return this._stream._contentLength;
|
||||
},
|
||||
|
||||
read: function PDFDataTransportStreamReader_read() {
|
||||
async read() {
|
||||
if (this._queuedChunks.length > 0) {
|
||||
var chunk = this._queuedChunks.shift();
|
||||
return Promise.resolve({ value: chunk, done: false, });
|
||||
return { value: chunk, done: false, };
|
||||
}
|
||||
if (this._done) {
|
||||
return Promise.resolve({ value: undefined, done: true, });
|
||||
return { value: undefined, done: true, };
|
||||
}
|
||||
var requestCapability = createPromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
|
@ -216,14 +216,14 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
|
|||
return false;
|
||||
},
|
||||
|
||||
read: function PDFDataTransportStreamRangeReader_read() {
|
||||
async read() {
|
||||
if (this._queuedChunk) {
|
||||
let chunk = this._queuedChunk;
|
||||
this._queuedChunk = null;
|
||||
return Promise.resolve({ value: chunk, done: false, });
|
||||
return { value: chunk, done: false, };
|
||||
}
|
||||
if (this._done) {
|
||||
return Promise.resolve({ value: undefined, done: true, });
|
||||
return { value: undefined, done: true, };
|
||||
}
|
||||
var requestCapability = createPromiseCapability();
|
||||
this._requests.push(requestCapability);
|
||||
|
|
|
@ -18,13 +18,11 @@ import {
|
|||
ReadableStream, UnexpectedResponseException, UnknownErrorException
|
||||
} from './util';
|
||||
|
||||
function resolveCall(fn, args, thisArg = null) {
|
||||
async function resolveCall(fn, args, thisArg = null) {
|
||||
if (!fn) {
|
||||
return Promise.resolve(undefined);
|
||||
return;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve(fn.apply(thisArg, args));
|
||||
});
|
||||
return fn.apply(thisArg, args);
|
||||
}
|
||||
|
||||
function wrapReason(reason) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue