Merge pull request #11169 from Snuffleupagus/Dict-inline-Ref-checks

Reduce the number of function calls in the `Dict` class
This commit is contained in:
Tim van der Meij 2019-09-24 23:33:37 +02:00 committed by GitHub
commit cd909c531f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,59 +81,47 @@ var Dict = (function DictClosure() {
},
// automatically dereferences Ref objects
get: function Dict_get(key1, key2, key3) {
var value;
var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (typeof (value = this._map[key1]) !== 'undefined' ||
key1 in this._map || typeof key2 === 'undefined') {
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
get(key1, key2, key3) {
let value = this._map[key1];
if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
value = this._map[key3];
}
}
if (typeof (value = this._map[key2]) !== 'undefined' ||
key2 in this._map || typeof key3 === 'undefined') {
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
if (value instanceof Ref && this.xref) {
return this.xref.fetch(value, this.suppressEncryption);
}
value = this._map[key3];
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
return value;
},
// Same as get(), but returns a promise and uses fetchIfRefAsync().
getAsync: function Dict_getAsync(key1, key2, key3) {
var value;
var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (typeof (value = this._map[key1]) !== 'undefined' ||
key1 in this._map || typeof key2 === 'undefined') {
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
async getAsync(key1, key2, key3) {
let value = this._map[key1];
if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
value = this._map[key3];
}
return Promise.resolve(value);
}
if (typeof (value = this._map[key2]) !== 'undefined' ||
key2 in this._map || typeof key3 === 'undefined') {
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
}
return Promise.resolve(value);
if (value instanceof Ref && this.xref) {
return this.xref.fetchAsync(value, this.suppressEncryption);
}
value = this._map[key3];
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
}
return Promise.resolve(value);
return value;
},
// Same as get(), but dereferences all elements if the result is an Array.
getArray: function Dict_getArray(key1, key2, key3) {
var value = this.get(key1, key2, key3);
var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (!Array.isArray(value) || !xref) {
getArray(key1, key2, key3) {
let value = this.get(key1, key2, key3);
if (!Array.isArray(value) || !this.xref) {
return value;
}
value = value.slice(); // Ensure that we don't modify the Dict data.
for (var i = 0, ii = value.length; i < ii; i++) {
if (!isRef(value[i])) {
for (let i = 0, ii = value.length; i < ii; i++) {
if (!(value[i] instanceof Ref)) {
continue;
}
value[i] = xref.fetch(value[i], suppressEncryption);
value[i] = this.xref.fetch(value[i], this.suppressEncryption);
}
return value;
},