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:
Jonas Jenwald 2018-07-30 13:58:09 +02:00
parent 4ea663aa8a
commit 099ed08852
13 changed files with 159 additions and 179 deletions

View file

@ -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) {